diff options
Diffstat (limited to 'ecs/src')
-rw-r--r-- | ecs/src/actions.rs | 2 | ||||
-rw-r--r-- | ecs/src/component.rs | 8 | ||||
-rw-r--r-- | ecs/src/component/local.rs | 4 | ||||
-rw-r--r-- | ecs/src/component/storage.rs | 14 | ||||
-rw-r--r-- | ecs/src/component/storage/archetype.rs | 2 | ||||
-rw-r--r-- | ecs/src/component/storage/graph.rs | 18 | ||||
-rw-r--r-- | ecs/src/lib.rs | 4 | ||||
-rw-r--r-- | ecs/src/lock.rs | 20 | ||||
-rw-r--r-- | ecs/src/query/flexible.rs | 23 | ||||
-rw-r--r-- | ecs/src/query/options.rs | 10 | ||||
-rw-r--r-- | ecs/src/relationship.rs | 8 | ||||
-rw-r--r-- | ecs/src/sole.rs | 6 | ||||
-rw-r--r-- | ecs/src/system.rs | 14 | ||||
-rw-r--r-- | ecs/src/uid.rs | 14 | ||||
-rw-r--r-- | ecs/src/util.rs | 12 |
15 files changed, 89 insertions, 70 deletions
diff --git a/ecs/src/actions.rs b/ecs/src/actions.rs index 89fd84a..184f811 100644 --- a/ecs/src/actions.rs +++ b/ecs/src/actions.rs @@ -150,7 +150,7 @@ pub struct Ref<'weak_ref> _pd: PhantomData<&'weak_ref ()>, } -impl<'weak_ref> Ref<'weak_ref> +impl Ref<'_> { #[must_use] pub fn to_actions(&self) -> Actions<'_> diff --git a/ecs/src/component.rs b/ecs/src/component.rs index f77b55a..77046d0 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -228,11 +228,13 @@ pub struct Metadata impl Metadata { + #[must_use] pub fn new_non_optional(id: Uid) -> Self { Self { id, is_optional: false } } + #[must_use] pub fn get<ComponentT: Component + ?Sized>(component: &ComponentT) -> Self { Self { @@ -241,6 +243,7 @@ impl Metadata } } + #[must_use] pub fn of<ComponentT: Component>() -> Self { Self { @@ -268,6 +271,10 @@ pub trait FromOptional<'comp> pub trait FromLockedOptional<'comp>: Sized { + /// Converts a reference to a optional locked boxed component to a instance of `Self`. + /// + /// # Errors + /// Returns `Err` if taking the lock (in a non-blocking way) fails. fn from_locked_optional_component( optional_component: Option<&'comp Lock<Box<dyn Component>>>, world: &'comp World, @@ -406,6 +413,5 @@ impl RefSequence for () _world: &'component World, ) -> Self::Handles<'component> { - () } } diff --git a/ecs/src/component/local.rs b/ecs/src/component/local.rs index ad79f6f..d4e414e 100644 --- a/ecs/src/component/local.rs +++ b/ecs/src/component/local.rs @@ -38,7 +38,7 @@ where } } -impl<'world, LocalComponent> Deref for Local<'world, LocalComponent> +impl<LocalComponent> Deref for Local<'_, LocalComponent> where LocalComponent: Component, { @@ -50,7 +50,7 @@ where } } -impl<'world, LocalComponent> DerefMut for Local<'world, LocalComponent> +impl<LocalComponent> DerefMut for Local<'_, LocalComponent> where LocalComponent: Component, { diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index b68f89d..f2306b7 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -161,7 +161,7 @@ impl Storage .archetype() .has_component_with_id(component.self_id()) { - return Err(Error::EntityAlreadyHasComponent { + return Err(Error::ComponentAlreadyInEntity { entity: entity_uid, component: component.self_id(), }); @@ -254,7 +254,7 @@ impl Storage .archetype() .has_component_with_id(component_id) { - return Err(Error::EntityDoesNotHaveComponent { + return Err(Error::ComponentNotFoundInEntity { entity: entity_uid, component: component_id, }); @@ -452,7 +452,7 @@ impl<'component_storage> Iterator for ArchetypeRefIter<'component_storage> self.dfs_iter.push(( BorrowedOrOwned::Owned(Archetype::new( add_edge_archetype_id, - &add_edge_archetype_comps + add_edge_archetype_comps .iter() .map(|metadata| metadata.id) .collect::<Vec<_>>(), @@ -476,7 +476,7 @@ impl<'component_storage> Iterator for ArchetypeRefIter<'component_storage> } } -impl<'storage> ArchetypeRefIter<'storage> +impl ArchetypeRefIter<'_> { fn find_edges_of_imaginary_archetype( &self, @@ -484,7 +484,7 @@ impl<'storage> ArchetypeRefIter<'storage> ) -> Vec<(Uid, ArchetypeEdges)> { self.storage - .find_all_archetype_with_comps(&imaginary_archetype_comps) + .find_all_archetype_with_comps(imaginary_archetype_comps) .into_iter() .filter_map(|found_id| { let found_archetype = self.storage.get_archetype_by_id(found_id).unwrap(); @@ -531,13 +531,13 @@ pub enum Error EntityDoesNotExist(Uid), #[error("Entity with ID {entity:?} already has component with ID {component:?}")] - EntityAlreadyHasComponent + ComponentAlreadyInEntity { entity: Uid, component: Uid }, #[error("Entity with ID {entity:?} does not have component with ID {component:?}")] - EntityDoesNotHaveComponent + ComponentNotFoundInEntity { entity: Uid, component: Uid }, diff --git a/ecs/src/component/storage/archetype.rs b/ecs/src/component/storage/archetype.rs index 9bf0feb..f6f8132 100644 --- a/ecs/src/component/storage/archetype.rs +++ b/ecs/src/component/storage/archetype.rs @@ -173,7 +173,7 @@ impl Id { pub fn new(component_ids: &impl AsRef<[Uid]>) -> Self { - if component_ids.as_ref().len() == 0 { + if component_ids.as_ref().is_empty() { return Self { hash: 0 }; } diff --git a/ecs/src/component/storage/graph.rs b/ecs/src/component/storage/graph.rs index a8e3f17..feac16c 100644 --- a/ecs/src/component/storage/graph.rs +++ b/ecs/src/component/storage/graph.rs @@ -123,7 +123,7 @@ impl Graph if other_archetype_node .archetype() - .is_superset(&archetype_node.archetype()) + .is_superset(archetype_node.archetype()) { Self::create_missing_superset_node_edges( archetype_node, @@ -155,7 +155,7 @@ impl Graph ) { if superset_node.archetype().component_cnt() - >= target_node.archetype().component_cnt() + 1 + > target_node.archetype().component_cnt() { let first_unique_comp_id = superset_node .archetype() @@ -278,14 +278,16 @@ pub struct ArchetypeEdges pub remove: Option<ArchetypeId>, } +type ArchetypeAddEdgeDfsIterStackElem<'graph> = ( + BorrowedOrOwned<'graph, Archetype>, + VecIntoIter<(Uid, ArchetypeEdges)>, +); + #[derive(Debug)] pub struct ArchetypeAddEdgeDfsIter<'graph> { graph: &'graph Graph, - stack: Vec<( - BorrowedOrOwned<'graph, Archetype>, - VecIntoIter<(Uid, ArchetypeEdges)>, - )>, + stack: Vec<ArchetypeAddEdgeDfsIterStackElem<'graph>>, visited: HashSet<ArchetypeId>, } @@ -296,7 +298,7 @@ impl<'graph> ArchetypeAddEdgeDfsIter<'graph> Self { graph, stack: start_nodes - .into_iter() + .iter() .map(|start_node_id| { let start_node = graph .get_node_by_id(*start_node_id) @@ -313,7 +315,7 @@ impl<'graph> ArchetypeAddEdgeDfsIter<'graph> ) }) .collect(), - visited: HashSet::from_iter(start_nodes.iter().copied()), + visited: start_nodes.iter().copied().collect::<HashSet<_>>(), } } diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index e2ebb21..504a106 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -456,7 +456,7 @@ impl World component_storage: &mut ComponentStorage, ) { - for component in components.into_iter() { + for component in components { if let Err(err) = component_storage.add_entity_component(entity_uid, component) { @@ -471,7 +471,7 @@ impl World component_storage: &mut ComponentStorage, ) { - for component_id in component_ids.into_iter() { + for component_id in component_ids { if let Err(err) = component_storage.remove_entity_component(entity_uid, component_id) { diff --git a/ecs/src/lock.rs b/ecs/src/lock.rs index c700098..1ce13fc 100644 --- a/ecs/src/lock.rs +++ b/ecs/src/lock.rs @@ -114,7 +114,7 @@ where } } -impl<'guard, Value> Deref for ReadGuard<'guard, Value> +impl<Value> Deref for ReadGuard<'_, Value> where Value: TypeName, { @@ -126,7 +126,7 @@ where } } -impl<'guard, Value> Drop for ReadGuard<'guard, Value> +impl<Value> Drop for ReadGuard<'_, Value> where Value: TypeName, { @@ -144,7 +144,7 @@ where inner: MappedRwLockReadGuard<'guard, Value>, } -impl<'guard, Value> Deref for MappedReadGuard<'guard, Value> +impl<Value> Deref for MappedReadGuard<'_, Value> where Value: TypeName, { @@ -156,7 +156,7 @@ where } } -impl<'guard, Value> Drop for MappedReadGuard<'guard, Value> +impl<Value> Drop for MappedReadGuard<'_, Value> where Value: TypeName, { @@ -196,7 +196,7 @@ where } } -impl<'guard, Value> Deref for WriteGuard<'guard, Value> +impl<Value> Deref for WriteGuard<'_, Value> where Value: TypeName, { @@ -208,7 +208,7 @@ where } } -impl<'guard, Value> DerefMut for WriteGuard<'guard, Value> +impl<Value> DerefMut for WriteGuard<'_, Value> where Value: TypeName, { @@ -218,7 +218,7 @@ where } } -impl<'guard, Value> Drop for WriteGuard<'guard, Value> +impl<Value> Drop for WriteGuard<'_, Value> where Value: TypeName, { @@ -236,7 +236,7 @@ where inner: MappedRwLockWriteGuard<'guard, Value>, } -impl<'guard, Value> Deref for MappedWriteGuard<'guard, Value> +impl<Value> Deref for MappedWriteGuard<'_, Value> where Value: TypeName, { @@ -248,7 +248,7 @@ where } } -impl<'guard, Value> DerefMut for MappedWriteGuard<'guard, Value> +impl<Value> DerefMut for MappedWriteGuard<'_, Value> where Value: TypeName, { @@ -258,7 +258,7 @@ where } } -impl<'guard, Value> Drop for MappedWriteGuard<'guard, Value> +impl<Value> Drop for MappedWriteGuard<'_, Value> where Value: TypeName, { diff --git a/ecs/src/query/flexible.rs b/ecs/src/query/flexible.rs index 3bb8fd6..c42ec25 100644 --- a/ecs/src/query/flexible.rs +++ b/ecs/src/query/flexible.rs @@ -1,5 +1,5 @@ //! Low-level querying. -use std::iter::{repeat_n, Filter, Flatten, Map, RepeatN, Zip}; +use std::iter::{repeat_n, Filter, FlatMap, RepeatN, Zip}; use crate::component::storage::archetype::{Archetype, ArchetypeEntity, EntityIter}; use crate::component::storage::{ArchetypeRefIter, Storage as ComponentStorage}; @@ -30,19 +30,18 @@ where { /// Iterates over the entities matching this query. #[must_use] - pub fn iter<'query, OptionsT: Options>(&'query self) -> Iter<'query> + pub fn iter<OptionsT: Options>(&self) -> Iter<'_> { Iter { iter: self .component_storage - .search_archetypes(&self.comp_metadata.as_ref()) - .map( + .search_archetypes(self.comp_metadata.as_ref()) + .flat_map( (|archetype| { repeat_n(archetype, archetype.entity_cnt()) .zip(archetype.entities()) }) as ComponentIterMapFn, ) - .flatten() .filter(|(_, entity)| OptionsT::entity_filter(&entity.components)), } } @@ -110,31 +109,39 @@ impl<'query> EntityHandle<'query> { /// Returns the [`Uid`] of this entity. #[inline] + #[must_use] pub fn uid(&self) -> Uid { self.entity.uid } #[inline] + #[must_use] pub fn components(&self) -> &'query [EntityComponent] { &self.entity.components } #[inline] + #[must_use] pub fn get_component_index(&self, component_uid: Uid) -> Option<usize> { self.archetype.get_index_for_component(component_uid) } } -type ComponentIterMapFn = - for<'a> fn(&'a Archetype) -> Zip<RepeatN<&'a Archetype>, EntityIter<'a>>; +type ComponentIterMapFnOutput<'a> = Zip<RepeatN<&'a Archetype>, EntityIter<'a>>; + +type ComponentIterMapFn = for<'a> fn(&'a Archetype) -> ComponentIterMapFnOutput<'a>; type ComponentIterFilterFn = for<'a, 'b> fn(&'a (&'b Archetype, &'b ArchetypeEntity)) -> bool; type QueryEntityIter<'query> = Filter< - Flatten<Map<ArchetypeRefIter<'query>, ComponentIterMapFn>>, + FlatMap< + ArchetypeRefIter<'query>, + ComponentIterMapFnOutput<'query>, + ComponentIterMapFn, + >, ComponentIterFilterFn, >; diff --git a/ecs/src/query/options.rs b/ecs/src/query/options.rs index 772d091..6318359 100644 --- a/ecs/src/query/options.rs +++ b/ecs/src/query/options.rs @@ -8,12 +8,12 @@ use crate::EntityComponent; /// Query options. pub trait Options { - fn entity_filter<'component>(components: &'component [EntityComponent]) -> bool; + fn entity_filter(components: &[EntityComponent]) -> bool; } impl Options for () { - fn entity_filter<'component>(_components: &'component [EntityComponent]) -> bool + fn entity_filter(_components: &[EntityComponent]) -> bool { true } @@ -30,10 +30,10 @@ impl<ComponentT> Options for With<ComponentT> where ComponentT: Component, { - fn entity_filter<'component>(components: &'component [EntityComponent]) -> bool + fn entity_filter(components: &[EntityComponent]) -> bool { let ids_set = components - .into_iter() + .iter() .map(|component| component.id) .collect::<HashSet<_>>(); @@ -52,7 +52,7 @@ impl<OptionsT> Options for Not<OptionsT> where OptionsT: Options, { - fn entity_filter<'component>(components: &'component [EntityComponent]) -> bool + fn entity_filter(components: &[EntityComponent]) -> bool { !OptionsT::entity_filter(components) } diff --git a/ecs/src/relationship.rs b/ecs/src/relationship.rs index 54dc0cd..a0ccf4d 100644 --- a/ecs/src/relationship.rs +++ b/ecs/src/relationship.rs @@ -131,9 +131,7 @@ where ) -> Result<Self, LockError> { Ok(Self::from_optional_mut_component( - optional_component - .map(|lock| lock.write_nonblock()) - .transpose()?, + optional_component.map(Lock::write_nonblock).transpose()?, world, )) } @@ -388,9 +386,7 @@ where ) -> Result<Self, LockError> { Ok(Self::from_optional_component( - optional_component - .map(|lock| lock.read_nonblock()) - .transpose()?, + optional_component.map(Lock::read_nonblock).transpose()?, world, )) } diff --git a/ecs/src/sole.rs b/ecs/src/sole.rs index a35b520..5af5ce3 100644 --- a/ecs/src/sole.rs +++ b/ecs/src/sole.rs @@ -114,7 +114,7 @@ where } } -impl<'world, SoleT> Deref for Single<'world, SoleT> +impl<SoleT> Deref for Single<'_, SoleT> where SoleT: Sole, { @@ -126,7 +126,7 @@ where } } -impl<'world, SoleT> DerefMut for Single<'world, SoleT> +impl<SoleT> DerefMut for Single<'_, SoleT> where SoleT: Sole, { @@ -173,7 +173,7 @@ where _pd: PhantomData<&'weak_ref SoleT>, } -impl<'weak_ref, SoleT> SingleRef<'weak_ref, SoleT> +impl<SoleT> SingleRef<'_, SoleT> where SoleT: Sole, { diff --git a/ecs/src/system.rs b/ecs/src/system.rs index b410d8f..40eba8d 100644 --- a/ecs/src/system.rs +++ b/ecs/src/system.rs @@ -247,9 +247,7 @@ impl<'component, ComponentT: Component> FromLockedOptionalComponent<'component> ) -> Result<Self, LockError> { Ok(Self::from_optional_mut_component( - optional_component - .map(|lock| lock.write_nonblock()) - .transpose()?, + optional_component.map(Lock::write_nonblock).transpose()?, world, )) } @@ -285,7 +283,7 @@ where } } -impl<'a, ComponentT: Component> Deref for ComponentRefMut<'a, ComponentT> +impl<ComponentT: Component> Deref for ComponentRefMut<'_, ComponentT> { type Target = ComponentT; @@ -295,7 +293,7 @@ impl<'a, ComponentT: Component> Deref for ComponentRefMut<'a, ComponentT> } } -impl<'a, ComponentT: Component> DerefMut for ComponentRefMut<'a, ComponentT> +impl<ComponentT: Component> DerefMut for ComponentRefMut<'_, ComponentT> { fn deref_mut(&mut self) -> &mut Self::Target { @@ -355,9 +353,7 @@ impl<'component, ComponentT: Component> FromLockedOptionalComponent<'component> ) -> Result<Self, LockError> { Ok(Self::from_optional_component( - optional_component - .map(|lock| lock.read_nonblock()) - .transpose()?, + optional_component.map(Lock::read_nonblock).transpose()?, world, )) } @@ -393,7 +389,7 @@ where } } -impl<'a, ComponentT: Component> Deref for ComponentRef<'a, ComponentT> +impl<ComponentT: Component> Deref for ComponentRef<'_, ComponentT> { type Target = ComponentT; diff --git a/ecs/src/uid.rs b/ecs/src/uid.rs index bcef73e..c3ed85b 100644 --- a/ecs/src/uid.rs +++ b/ecs/src/uid.rs @@ -32,22 +32,30 @@ impl Uid let id = NEXT.fetch_add(1, Ordering::Relaxed); Self { - inner: ID_BITS.field_prep(id as u64) | KIND_BITS.field_prep(kind as u64), + inner: ID_BITS.field_prep(u64::from(id)) | KIND_BITS.field_prep(kind as u64), } } #[must_use] pub fn id(&self) -> u32 { - self.inner.field_get(ID_BITS) as u32 + let Ok(id) = u32::try_from(self.inner.field_get(ID_BITS)) else { + unreachable!("Uid id does not fit in u32"); + }; + + id } #[must_use] pub fn kind(&self) -> Kind { + let Ok(kind) = u8::try_from(self.inner.field_get(KIND_BITS)) else { + unreachable!("Uid kind does not fit in u8"); + }; + // SAFETY: The kind bits cannot be invalid since they are set using the Kind enum // in the new_unique function - unsafe { transmute(self.inner.field_get(KIND_BITS) as u8) } + unsafe { transmute::<u8, Kind>(kind) } } } diff --git a/ecs/src/util.rs b/ecs/src/util.rs index fbd33fa..17436ff 100644 --- a/ecs/src/util.rs +++ b/ecs/src/util.rs @@ -55,9 +55,11 @@ pub trait StreamingIterator Self: Sized, Predicate: FnMut(&Self::Item<'this>) -> bool, { - while let Some(item) = - unsafe { transmute::<_, Option<Self::Item<'_>>>(self.streaming_next()) } - { + while let Some(item) = unsafe { + transmute::<Option<Self::Item<'_>>, Option<Self::Item<'_>>>( + self.streaming_next(), + ) + } { if predicate(&item) { return Some(item); } @@ -97,7 +99,7 @@ pub enum BorrowedOrOwned<'a, Value> Owned(Value), } -impl<'a, Value> Deref for BorrowedOrOwned<'a, Value> +impl<Value> Deref for BorrowedOrOwned<'_, Value> { type Target = Value; @@ -240,6 +242,7 @@ impl BitMask<u64> Self { mask } } + #[must_use] pub const fn value(self) -> u64 { self.mask @@ -266,6 +269,7 @@ impl BitAnd<u64> for BitMask<u64> pub trait NumberExt: Sized { /// Returns a range of bits (field) specified by the provided [`BitMask`]. + #[must_use] fn field_get(self, field_mask: BitMask<Self>) -> Self; } |