Skip to content

Commit

Permalink
chore: rename Mutable to ComponentMut to avoid conflicts with other l…
Browse files Browse the repository at this point in the history
…ibraries
  • Loading branch information
ten3roberts committed Nov 19, 2024
1 parent bb63cf2 commit 2e19fa0
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 41 deletions.
6 changes: 3 additions & 3 deletions benches/common/serialize_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::iter::repeat;
use bincode::Options;
use flax::{
component,
serialize::{SerdeBuilder, SerializeFormat},
serialize::{SerializationContextBuilder, SerializeFormat},
BatchSpawn, World,
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -58,7 +58,7 @@ impl Benchmark {
pub fn run_col(&mut self) {
let Self(world) = self;

let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(transform())
.with(position())
.with(rotation())
Expand All @@ -80,7 +80,7 @@ impl Benchmark {
pub fn run_row(&mut self) {
let Self(world) = self;

let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(transform())
.with(position())
.with(rotation())
Expand Down
4 changes: 2 additions & 2 deletions benches/common/serialize_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::iter::repeat;

use flax::{
component,
serialize::{SerdeBuilder, SerializeFormat},
serialize::{SerializationContextBuilder, SerializeFormat},
BatchSpawn, World,
};

Expand Down Expand Up @@ -58,7 +58,7 @@ impl Benchmark {
pub fn run_col(&mut self) {
let Self(world) = self;

let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(transform())
.with(position())
.with(rotation())
Expand Down
6 changes: 3 additions & 3 deletions examples/guide/query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use flax::{
component, entity_ids, CommandBuffer, Component, Debuggable, Entity, EntityBorrow, FetchExt,
Mutable, Query, QueryBorrow, Schedule, System, World,
component, entity_ids, CommandBuffer, Component, ComponentMut, Debuggable, Entity,
EntityBorrow, FetchExt, Query, QueryBorrow, Schedule, System, World,
};
use glam::{vec2, Vec2};
use rand::{rngs::StdRng, Rng, SeedableRng};
Expand Down Expand Up @@ -99,7 +99,7 @@ fn main() -> anyhow::Result<()> {
.with_name("update_distance")
.with_query(query)
.build(
|mut query: QueryBorrow<(_, Component<Vec2>, Mutable<f32>), _>| {
|mut query: QueryBorrow<(_, Component<Vec2>, ComponentMut<f32>), _>| {
for (id, pos, dist) in &mut query {
println!("Updating distance for {id} with position: {pos:?}");
*dist = pos.length();
Expand Down
4 changes: 2 additions & 2 deletions examples/guide/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() -> anyhow::Result<()> {

tracing_subscriber::fmt().init();

use flax::serialize::{SerdeBuilder, SerializeFormat};
use flax::serialize::{SerializationContextBuilder, SerializeFormat};
tracing::info!("It works");

let mut world = World::new();
Expand Down Expand Up @@ -45,7 +45,7 @@ fn main() -> anyhow::Result<()> {
// ANCHOR_END: setup

// ANCHOR: serialize
let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(name())
.with(position())
.with(velocity())
Expand Down
8 changes: 4 additions & 4 deletions examples/guide/systems.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use flax::{
component, entity_ids, BoxedSystem, CommandBuffer, Component, Debuggable, Entity, FetchExt,
Mutable, Query, QueryBorrow, Schedule, System, World,
component, entity_ids, BoxedSystem, CommandBuffer, Component, ComponentMut, Debuggable, Entity,
FetchExt, Query, QueryBorrow, Schedule, System, World,
};
use glam::{vec2, Vec2};
use rand::{rngs::StdRng, Rng, SeedableRng};
Expand Down Expand Up @@ -31,7 +31,7 @@ fn main() -> anyhow::Result<()> {
.with_name("update_distance")
.with_query(Query::new((entity_ids(), position(), distance().as_mut())))
.build(
|mut query: QueryBorrow<(_, Component<Vec2>, Mutable<f32>), _>| {
|mut query: QueryBorrow<(_, Component<Vec2>, ComponentMut<f32>), _>| {
for (id, pos, dist) in &mut query {
println!("Updating distance for {id} with position: {pos:?}");
*dist = pos.length();
Expand All @@ -48,7 +48,7 @@ fn main() -> anyhow::Result<()> {
.with_name("update_distance")
.with_query(Query::new((entity_ids(), position(), distance().as_mut())))
.build(
|mut query: QueryBorrow<(_, Component<Vec2>, Mutable<f32>), _>| {
|mut query: QueryBorrow<(_, Component<Vec2>, ComponentMut<f32>), _>| {
for (id, pos, dist) in &mut query {
println!("Updating distance for {id} with position: {pos:?}");
*dist = pos.length();
Expand Down
2 changes: 1 addition & 1 deletion examples/query/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn main() {
Query::new((world_position().as_mut(), position())).with_strategy(Dfs::new(child_of)),
)
.build(
|mut query: DfsBorrow<(Mutable<Vec3>, Component<Vec3>), All, ()>| {
|mut query: DfsBorrow<(ComponentMut<Vec3>, Component<Vec3>), All, ()>| {
query.traverse(&Vec3::ZERO, |(world_pos, &pos), _, &parent_pos| {
*world_pos = pos + parent_pos;
*world_pos
Expand Down
2 changes: 1 addition & 1 deletion src/archetypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ pub(crate) struct ArchetypeIndex {
}

impl Debug for ArchetypeIndex {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ArchetypeIndex")
.field("components", &self.components)
.finish()
Expand Down
6 changes: 3 additions & 3 deletions src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
metadata::Metadata,
relation::RelationExt,
vtable::{ComponentVTable, UntypedVTable},
Entity, Mutable,
Entity, ComponentMut,
};

/// Trait alias for a 'static + Send + Sync type which can be used as a
Expand Down Expand Up @@ -260,8 +260,8 @@ impl<T: ComponentValue> Component<T> {
}

/// Transform this into a mutable fetch
pub const fn as_mut(self) -> Mutable<T> {
Mutable(self)
pub const fn as_mut(self) -> ComponentMut<T> {
ComponentMut(self)
}

/// Transform this into a (maybe) mutable fetch
Expand Down
29 changes: 24 additions & 5 deletions src/events.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloc::vec::Vec;
use bitflags::bitflags;
use itertools::Itertools;

use crate::{
Expand Down Expand Up @@ -37,6 +38,18 @@ impl Event {
}
}

bitflags! {
/// The type of ECS event
pub struct EventKindFilter: u8 {
/// Component was added
const ADDED = 1;
/// Component was removed
const REMOVED = 2;
/// Component was modified
const MODIFIED = 4;
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// The type of ECS event
pub enum EventKind {
Expand Down Expand Up @@ -139,8 +152,9 @@ pub trait EventSubscriber: ComponentValue {
subscriber: self,
}
}

/// Filter a subscriber to only receive events of a specific kind
fn filter_event_kind(self, event_kind: EventKind) -> FilterEventKind<Self>
fn filter_event_kind(self, event_kind: EventKindFilter) -> FilterEventKind<Self>
where
Self: Sized,
{
Expand Down Expand Up @@ -426,7 +440,7 @@ where

/// Filter a subscriber to only receive events of a specific kind
pub struct FilterEventKind<S> {
event_kind: EventKind,
event_kind: EventKindFilter,
subscriber: S,
}

Expand All @@ -435,23 +449,28 @@ where
S: EventSubscriber,
{
fn on_added(&self, storage: &Storage, event: &EventData) {
if self.event_kind == EventKind::Added {
if self.event_kind.contains(EventKindFilter::ADDED) {
self.subscriber.on_added(storage, event)
}
}

fn on_modified(&self, event: &EventData) {
if self.event_kind == EventKind::Modified {
if self.event_kind.contains(EventKindFilter::MODIFIED) {
self.subscriber.on_modified(event)
}
}

fn on_removed(&self, storage: &Storage, event: &EventData) {
if self.event_kind == EventKind::Removed {
if self.event_kind.contains(EventKindFilter::REMOVED) {
self.subscriber.on_removed(storage, event)
}
}

#[inline]
fn matches_component(&self, desc: ComponentDesc) -> bool {
self.subscriber.matches_component(desc)
}

fn is_connected(&self) -> bool {
self.subscriber.is_connected()
}
Expand Down
6 changes: 3 additions & 3 deletions src/fetch/component_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use super::{FetchAccessData, FetchPrepareData, PreparedFetch};
#[derive(Debug, Clone)]
/// Mutable component fetch
/// See [crate::Component::as_mut]
pub struct Mutable<T>(pub(crate) Component<T>);
pub struct ComponentMut<T>(pub(crate) Component<T>);

impl<'w, T> Fetch<'w> for Mutable<T>
impl<'w, T> Fetch<'w> for ComponentMut<T>
where
T: ComponentValue,
{
Expand Down Expand Up @@ -64,7 +64,7 @@ where
}
}

impl<'q, T: ComponentValue> FetchItem<'q> for Mutable<T> {
impl<'q, T: ComponentValue> FetchItem<'q> for ComponentMut<T> {
type Item = &'q mut T;
}

Expand Down
16 changes: 8 additions & 8 deletions src/fetch/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
archetype::ChangeKind,
component::ComponentValue,
filter::{ChangeFilter, Filtered, NoEntities, Union},
Component, EntityIds, FetchExt, Mutable,
Component, EntityIds, FetchExt, ComponentMut,
};

/// Allows transforming a fetch into another.
Expand Down Expand Up @@ -32,14 +32,14 @@ impl<T: ComponentValue> TransformFetch<Added> for Component<T> {
}
}

impl<T: ComponentValue> TransformFetch<Modified> for Mutable<T> {
impl<T: ComponentValue> TransformFetch<Modified> for ComponentMut<T> {
type Output = Filtered<Self, NoEntities>;
fn transform_fetch(self, _: Modified) -> Self::Output {
self.filtered(NoEntities)
}
}

impl<T: ComponentValue> TransformFetch<Added> for Mutable<T> {
impl<T: ComponentValue> TransformFetch<Added> for ComponentMut<T> {
type Output = Filtered<Self, NoEntities>;
fn transform_fetch(self, _: Added) -> Self::Output {
self.filtered(NoEntities)
Expand Down Expand Up @@ -179,7 +179,7 @@ mod tests {
#[test]
#[cfg(feature = "derive")]
fn query_modified_struct() {
use crate::{fetch::Cloned, Component, Fetch, Mutable, Opt};
use crate::{fetch::Cloned, Component, Fetch, ComponentMut, Opt};

component! {
a: i32,
Expand All @@ -193,8 +193,8 @@ mod tests {
struct MyFetch {
a: Component<i32>,
b: Cloned<Component<String>>,
c: Mutable<f32>,
other: Opt<Mutable<()>>,
c: ComponentMut<f32>,
other: Opt<ComponentMut<()>>,
}

let mut world = World::new();
Expand Down Expand Up @@ -273,7 +273,7 @@ mod tests {
#[test]
#[cfg(feature = "derive")]
fn query_inserted_struct() {
use crate::{fetch::Cloned, Component, EntityIds, Fetch, Mutable};
use crate::{fetch::Cloned, Component, EntityIds, Fetch, ComponentMut};

#[derive(Debug)]
struct Custom;
Expand All @@ -294,7 +294,7 @@ mod tests {
a: Component<i32>,
b: Cloned<Component<String>>,
#[fetch(ignore)]
c: Mutable<Custom>,
c: ComponentMut<Custom>,
}

let mut world = World::new();
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
//! .boxed();
//!
//! let despawn_system = System::builder()
//! .with_query(Query::new(entity_ids()).filter(health().le(0.0)))
//! .with_query(Query::new(entity_ids()).with_filter(health().le(0.0)))
//! .with_cmd_mut()
//! .build(|mut q: QueryBorrow<EntityIds, _>, cmd: &mut CommandBuffer| {
//! for id in &mut q {
Expand Down Expand Up @@ -264,7 +264,7 @@ pub use entity_ref::{EntityRef, EntityRefMut};
pub use entry::{Entry, OccupiedEntry, VacantEntry};
pub use error::Error;
pub use fetch::{
relations_like, EntityIds, Fetch, FetchExt, FetchItem, Mutable, Opt, OptOr, Relations,
relations_like, ComponentMut, EntityIds, Fetch, FetchExt, FetchItem, Opt, OptOr, Relations,
};

pub use metadata::{Debuggable, Exclusive};
Expand Down
2 changes: 1 addition & 1 deletion tests/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn merge_empty() -> anyhow::Result<()> {
v.set(name(), format!("world.{i}")).spawn(&mut world);
});

let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with_name("position", position())
.with_name("rotation", rotation())
.with_name("scale", scale())
Expand Down
8 changes: 5 additions & 3 deletions tests/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@ fn schedule_input_tuple() {
fn schedule_par() {
use glam::{vec2, Vec2};

use flax::{components::name, entity_ids, CommandBuffer, Component, EntityIds, Fetch, Mutable};
use flax::{
components::name, entity_ids, CommandBuffer, Component, ComponentMut, EntityIds, Fetch,
};

#[derive(Debug, Clone)]
enum Weapon {
Expand Down Expand Up @@ -390,7 +392,7 @@ fn schedule_par() {
let heal = System::builder()
.with_query(Query::new(health().as_mut()))
.with_name("heal")
.build(|mut q: QueryBorrow<Mutable<f32>>| {
.build(|mut q: QueryBorrow<ComponentMut<f32>>| {
q.iter().for_each(|h| {
if *h > 0.0 {
*h += 1.0
Expand Down Expand Up @@ -421,7 +423,7 @@ fn schedule_par() {
struct BattleTarget {
id: EntityIds,
pos: Component<Vec2>,
health: Mutable<f32>,
health: ComponentMut<f32>,
}

let battle = System::builder()
Expand Down

0 comments on commit 2e19fa0

Please sign in to comment.