diff options
author | HampusM <hampus@hampusmat.com> | 2024-11-16 18:35:08 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-11-16 18:35:08 +0100 |
commit | 3e33964aa800376e4c60e71c735b8eef7788c4e5 (patch) | |
tree | 587c2bd8a6e041449cd1514434db9ce0814c9f46 /ecs | |
parent | c9a07ff61b607478e264fc0581076643c750fe98 (diff) |
feat(ecs): check comp metadata list before creating archetype ID
Diffstat (limited to 'ecs')
-rw-r--r-- | ecs/src/archetype.rs | 17 | ||||
-rw-r--r-- | ecs/src/component/storage.rs | 16 |
2 files changed, 22 insertions, 11 deletions
diff --git a/ecs/src/archetype.rs b/ecs/src/archetype.rs index 5c104b7..d2ee36a 100644 --- a/ecs/src/archetype.rs +++ b/ecs/src/archetype.rs @@ -16,12 +16,25 @@ pub struct Id impl Id { pub fn from_components_metadata( - components_metadata: impl IntoIterator<Item = ComponentMetadata>, + components_metadata: &impl AsRef<[ComponentMetadata]>, ) -> Self { + debug_assert!( + components_metadata.as_ref().len() > 0, + "Cannot create a archetype ID from a empty component metadata list" + ); + + debug_assert!( + components_metadata + .as_ref() + .is_sorted_by_key(|comp_metadata| comp_metadata.id), + "Cannot create archetype ID from a unsorted component metadata list" + ); + Self::new( components_metadata - .into_iter() + .as_ref() + .iter() .filter_map(|component_metadata| { if component_metadata.is_optional == ComponentIsOptional::Yes { return None; diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index 5c32e1e..c407192 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -32,20 +32,18 @@ impl Storage ) -> ArchetypeRefIter<'_> where CompMetadataList: Sortable<Item = ComponentMetadata>, - CompMetadataList: Borrow<[ComponentMetadata]>, + CompMetadataList: AsRef<[ComponentMetadata]>, { components_metadata.sort_by_key_b(|component_metadata| component_metadata.id); - let archetype_id = ArchetypeId::from_components_metadata( - components_metadata.borrow().iter().cloned(), - ); + let archetype_id = ArchetypeId::from_components_metadata(&components_metadata); // This looks stupid but the borrow checker complains otherwise if self.archetype_lookup.borrow().contains_key(&archetype_id) { return self.iter_archetypes_by_lookup(archetype_id); } - let comp_ids_set = create_non_opt_component_id_set(components_metadata.borrow()); + let comp_ids_set = create_non_opt_component_id_set(components_metadata.as_ref()); let matching_archetype_indices = self .archetypes @@ -105,9 +103,10 @@ impl Storage ); let archetype_id = ArchetypeId::from_components_metadata( - components + &components .iter() - .map(|component| ComponentMetadata::of(&**component)), + .map(|component| ComponentMetadata::of(&**component)) + .collect::<Vec<_>>(), ); let comp_ids_set = create_non_opt_component_id_set( @@ -529,7 +528,6 @@ mod tests Metadata as ComponentMetadata, }; use crate::uid::{Kind as UidKind, Uid}; - use crate::{self as ecs}; #[derive(Debug, Component)] struct HealthPotion @@ -610,7 +608,7 @@ mod tests let archetype_lookup = component_storage.archetype_lookup.borrow(); let lookup_entry = archetype_lookup - .get(&ArchetypeId::from_components_metadata(components_metadata)) + .get(&ArchetypeId::from_components_metadata(&components_metadata)) .expect("Expected entry in archetype lookup map"); let first_archetype_index = lookup_entry |