From 2e19fa07a407286113ff3671473f32b38af1f345 Mon Sep 17 00:00:00 2001 From: Freja Roberts Date: Tue, 19 Nov 2024 23:02:11 +0100 Subject: [PATCH] chore: rename Mutable to ComponentMut to avoid conflicts with other libraries --- benches/common/serialize_binary.rs | 6 +++--- benches/common/serialize_text.rs | 4 ++-- examples/guide/query.rs | 6 +++--- examples/guide/serialize.rs | 4 ++-- examples/guide/systems.rs | 8 ++++---- examples/query/transform.rs | 2 +- src/archetypes.rs | 2 +- src/component.rs | 6 +++--- src/events.rs | 29 ++++++++++++++++++++++++----- src/fetch/component_mut.rs | 6 +++--- src/fetch/transform.rs | 16 ++++++++-------- src/lib.rs | 4 ++-- tests/merge.rs | 2 +- tests/schedule.rs | 8 +++++--- 14 files changed, 62 insertions(+), 41 deletions(-) diff --git a/benches/common/serialize_binary.rs b/benches/common/serialize_binary.rs index e2697e57..0ce2e9e2 100644 --- a/benches/common/serialize_binary.rs +++ b/benches/common/serialize_binary.rs @@ -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}; @@ -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()) @@ -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()) diff --git a/benches/common/serialize_text.rs b/benches/common/serialize_text.rs index 327bed7e..dda1dbfd 100644 --- a/benches/common/serialize_text.rs +++ b/benches/common/serialize_text.rs @@ -2,7 +2,7 @@ use std::iter::repeat; use flax::{ component, - serialize::{SerdeBuilder, SerializeFormat}, + serialize::{SerializationContextBuilder, SerializeFormat}, BatchSpawn, World, }; @@ -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()) diff --git a/examples/guide/query.rs b/examples/guide/query.rs index 2c26e554..3f5beb6c 100644 --- a/examples/guide/query.rs +++ b/examples/guide/query.rs @@ -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}; @@ -99,7 +99,7 @@ fn main() -> anyhow::Result<()> { .with_name("update_distance") .with_query(query) .build( - |mut query: QueryBorrow<(_, Component, Mutable), _>| { + |mut query: QueryBorrow<(_, Component, ComponentMut), _>| { for (id, pos, dist) in &mut query { println!("Updating distance for {id} with position: {pos:?}"); *dist = pos.length(); diff --git a/examples/guide/serialize.rs b/examples/guide/serialize.rs index 385336bc..b7da8aaa 100644 --- a/examples/guide/serialize.rs +++ b/examples/guide/serialize.rs @@ -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(); @@ -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()) diff --git a/examples/guide/systems.rs b/examples/guide/systems.rs index 30a60bdf..71796a42 100644 --- a/examples/guide/systems.rs +++ b/examples/guide/systems.rs @@ -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}; @@ -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, Mutable), _>| { + |mut query: QueryBorrow<(_, Component, ComponentMut), _>| { for (id, pos, dist) in &mut query { println!("Updating distance for {id} with position: {pos:?}"); *dist = pos.length(); @@ -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, Mutable), _>| { + |mut query: QueryBorrow<(_, Component, ComponentMut), _>| { for (id, pos, dist) in &mut query { println!("Updating distance for {id} with position: {pos:?}"); *dist = pos.length(); diff --git a/examples/query/transform.rs b/examples/query/transform.rs index b9f09124..375f27bd 100644 --- a/examples/query/transform.rs +++ b/examples/query/transform.rs @@ -64,7 +64,7 @@ fn main() { Query::new((world_position().as_mut(), position())).with_strategy(Dfs::new(child_of)), ) .build( - |mut query: DfsBorrow<(Mutable, Component), All, ()>| { + |mut query: DfsBorrow<(ComponentMut, Component), All, ()>| { query.traverse(&Vec3::ZERO, |(world_pos, &pos), _, &parent_pos| { *world_pos = pos + parent_pos; *world_pos diff --git a/src/archetypes.rs b/src/archetypes.rs index 20e4bcd3..058f2dff 100644 --- a/src/archetypes.rs +++ b/src/archetypes.rs @@ -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() diff --git a/src/component.rs b/src/component.rs index 266fe465..850e5f56 100644 --- a/src/component.rs +++ b/src/component.rs @@ -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 @@ -260,8 +260,8 @@ impl Component { } /// Transform this into a mutable fetch - pub const fn as_mut(self) -> Mutable { - Mutable(self) + pub const fn as_mut(self) -> ComponentMut { + ComponentMut(self) } /// Transform this into a (maybe) mutable fetch diff --git a/src/events.rs b/src/events.rs index a12cec13..8b84d295 100644 --- a/src/events.rs +++ b/src/events.rs @@ -1,4 +1,5 @@ use alloc::vec::Vec; +use bitflags::bitflags; use itertools::Itertools; use crate::{ @@ -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 { @@ -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 + fn filter_event_kind(self, event_kind: EventKindFilter) -> FilterEventKind where Self: Sized, { @@ -426,7 +440,7 @@ where /// Filter a subscriber to only receive events of a specific kind pub struct FilterEventKind { - event_kind: EventKind, + event_kind: EventKindFilter, subscriber: S, } @@ -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() } diff --git a/src/fetch/component_mut.rs b/src/fetch/component_mut.rs index 6e8fe87b..13afd464 100644 --- a/src/fetch/component_mut.rs +++ b/src/fetch/component_mut.rs @@ -15,9 +15,9 @@ use super::{FetchAccessData, FetchPrepareData, PreparedFetch}; #[derive(Debug, Clone)] /// Mutable component fetch /// See [crate::Component::as_mut] -pub struct Mutable(pub(crate) Component); +pub struct ComponentMut(pub(crate) Component); -impl<'w, T> Fetch<'w> for Mutable +impl<'w, T> Fetch<'w> for ComponentMut where T: ComponentValue, { @@ -64,7 +64,7 @@ where } } -impl<'q, T: ComponentValue> FetchItem<'q> for Mutable { +impl<'q, T: ComponentValue> FetchItem<'q> for ComponentMut { type Item = &'q mut T; } diff --git a/src/fetch/transform.rs b/src/fetch/transform.rs index 433e27a8..f422ad92 100644 --- a/src/fetch/transform.rs +++ b/src/fetch/transform.rs @@ -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. @@ -32,14 +32,14 @@ impl TransformFetch for Component { } } -impl TransformFetch for Mutable { +impl TransformFetch for ComponentMut { type Output = Filtered; fn transform_fetch(self, _: Modified) -> Self::Output { self.filtered(NoEntities) } } -impl TransformFetch for Mutable { +impl TransformFetch for ComponentMut { type Output = Filtered; fn transform_fetch(self, _: Added) -> Self::Output { self.filtered(NoEntities) @@ -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, @@ -193,8 +193,8 @@ mod tests { struct MyFetch { a: Component, b: Cloned>, - c: Mutable, - other: Opt>, + c: ComponentMut, + other: Opt>, } let mut world = World::new(); @@ -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; @@ -294,7 +294,7 @@ mod tests { a: Component, b: Cloned>, #[fetch(ignore)] - c: Mutable, + c: ComponentMut, } let mut world = World::new(); diff --git a/src/lib.rs b/src/lib.rs index 7e7b83f3..7d3e6686 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, cmd: &mut CommandBuffer| { //! for id in &mut q { @@ -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}; diff --git a/tests/merge.rs b/tests/merge.rs index 7b112b93..a09f3e4d 100644 --- a/tests/merge.rs +++ b/tests/merge.rs @@ -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()) diff --git a/tests/schedule.rs b/tests/schedule.rs index 52d7dc85..cc48de8f 100644 --- a/tests/schedule.rs +++ b/tests/schedule.rs @@ -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 { @@ -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>| { + .build(|mut q: QueryBorrow>| { q.iter().for_each(|h| { if *h > 0.0 { *h += 1.0 @@ -421,7 +423,7 @@ fn schedule_par() { struct BattleTarget { id: EntityIds, pos: Component, - health: Mutable, + health: ComponentMut, } let battle = System::builder()