summaryrefslogtreecommitdiff
path: root/ecs/src/component/storage.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ecs/src/component/storage.rs')
-rw-r--r--ecs/src/component/storage.rs37
1 files changed, 22 insertions, 15 deletions
diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs
index 86f28fc..42e0e4f 100644
--- a/ecs/src/component/storage.rs
+++ b/ecs/src/component/storage.rs
@@ -9,7 +9,7 @@ use crate::type_name::TypeName;
use crate::EntityComponent;
#[derive(Debug, Default)]
-pub struct ComponentStorage
+pub struct Storage
{
archetypes: Vec<Archetype>,
archetype_lookup: HashMap<ArchetypeComponentsHash, Vec<usize>>,
@@ -20,7 +20,15 @@ pub struct ComponentStorage
pub struct ArchetypeRefIter<'component_storage>
{
inner: SliceIter<'component_storage, usize>,
- archetypes: &'component_storage Vec<Archetype>,
+ archetypes: &'component_storage [Archetype],
+}
+
+impl<'component_storage> ArchetypeRefIter<'component_storage>
+{
+ fn new_empty() -> Self
+ {
+ Self { inner: [].iter(), archetypes: &[] }
+ }
}
impl<'component_storage> Iterator for ArchetypeRefIter<'component_storage>
@@ -39,12 +47,12 @@ impl<'component_storage> Iterator for ArchetypeRefIter<'component_storage>
}
}
-impl ComponentStorage
+impl Storage
{
pub fn find_entities(
&self,
component_ids: &[(ComponentId, ComponentIsOptional)],
- ) -> Option<ArchetypeRefIter<'_>>
+ ) -> ArchetypeRefIter<'_>
{
let ids = component_ids
.iter()
@@ -58,9 +66,11 @@ impl ComponentStorage
self.archetype_lookup
.get(&ArchetypeComponentsHash::new(ids))
- .map(|archetypes_indices| ArchetypeRefIter {
- inner: archetypes_indices.iter(),
- archetypes: &self.archetypes,
+ .map_or_else(ArchetypeRefIter::new_empty, |archetypes_indices| {
+ ArchetypeRefIter {
+ inner: archetypes_indices.iter(),
+ archetypes: &self.archetypes,
+ }
})
}
@@ -136,10 +146,7 @@ impl ComponentStorage
self.archetype_lookup.clear();
for pending_entry in self.pending_archetype_lookup_entries.drain(..) {
- let components_set: HashSet<_> = pending_entry
- .iter()
- .map(|component_id| *component_id)
- .collect();
+ let components_set: HashSet<_> = pending_entry.iter().copied().collect();
let matching_archetype_indices = self
.archetypes
@@ -163,7 +170,7 @@ impl ComponentStorage
}
}
-impl TypeName for ComponentStorage
+impl TypeName for Storage
{
fn type_name(&self) -> &'static str
{
@@ -207,7 +214,7 @@ mod tests
use ecs_macros::Component;
- use super::{Archetype, ArchetypeComponentsHash, ComponentStorage};
+ use super::{Archetype, ArchetypeComponentsHash, Storage};
use crate::component::Id as ComponentId;
use crate::lock::Lock;
use crate::{self as ecs, EntityComponent};
@@ -242,7 +249,7 @@ mod tests
#[test]
fn push_entity_works()
{
- let mut component_storage = ComponentStorage::default();
+ let mut component_storage = Storage::default();
component_storage.push_entity(vec![
Box::new(HealthPotion { _hp_restoration: 12 }),
@@ -288,7 +295,7 @@ mod tests
#[test]
fn lookup_works()
{
- let mut component_storage = ComponentStorage::default();
+ let mut component_storage = Storage::default();
component_storage.archetypes.push(Archetype {
component_ids: HashSet::from([