diff options
Diffstat (limited to 'ecs/src/component.rs')
-rw-r--r-- | ecs/src/component.rs | 77 |
1 files changed, 32 insertions, 45 deletions
diff --git a/ecs/src/component.rs b/ecs/src/component.rs index 5da510a..85c556f 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -179,19 +179,17 @@ pub trait Sequence fn removed_event_ids() -> Vec<Uid>; fn from_components_mut<'component>( - components: impl Iterator<Item = &'component EntityComponent>, + components: &'component [EntityComponent], + component_index_lookup: impl Fn(Uid) -> Option<usize>, world: &'component World, - lock_component: fn( - entity_component: &EntityComponent, - ) -> WriteGuard<'_, Box<dyn Component>>, + lock_component: fn(&EntityComponent) -> WriteGuard<'_, Box<dyn Component>>, ) -> Self::MutRefs<'component>; fn from_components<'component>( - components: impl Iterator<Item = &'component EntityComponent>, + components: &'component [EntityComponent], + component_index_lookup: impl Fn(Uid) -> Option<usize>, world: &'component World, - lock_component: fn( - entity_component: &EntityComponent, - ) -> ReadGuard<'_, Box<dyn Component>>, + lock_component: fn(&EntityComponent) -> ReadGuard<'_, Box<dyn Component>>, ) -> Self::Refs<'component>; } @@ -304,56 +302,45 @@ macro_rules! inner { ] } + #[inline] fn from_components_mut<'component>( - components: impl Iterator<Item = &'component EntityComponent>, + components: &'component [EntityComponent], + component_index_lookup: impl Fn(Uid) -> Option<usize>, world: &'component World, - lock_component: fn( - entity_component: &EntityComponent, - ) -> WriteGuard<'_, Box<dyn Component>>, + lock_component: + fn(&EntityComponent) -> WriteGuard<'_, Box<dyn Component>>, ) -> Self::MutRefs<'component> { - #( - let mut comp_~I: Option<WriteGuard<Box<dyn Component>>> = None; - )* - - for comp in components { - #( - if comp.id == Comp~I::Component::id() { - comp_~I = Some(lock_component(comp)); - continue; - } - )* - } - (#( - Comp~I::RefMut::from_optional_mut_component(comp_~I, world), + Comp~I::RefMut::from_optional_mut_component( + component_index_lookup(Comp~I::Component::id()) + .and_then(|component_index| { + components.get(component_index) + .map(lock_component) + }), + world + ), )*) } + #[inline] fn from_components<'component>( - components: impl Iterator<Item = &'component EntityComponent>, + components: &'component [EntityComponent], + component_index_lookup: impl Fn(Uid) -> Option<usize>, world: &'component World, - lock_component: fn( - entity_component: &EntityComponent, - ) -> ReadGuard<'_, Box<dyn Component>>, + lock_component: + fn(&EntityComponent) -> ReadGuard<'_, Box<dyn Component>>, ) -> Self::Refs<'component> { - - #( - let mut comp_~I: Option<ReadGuard<Box<dyn Component>>> = None; - )* - - for comp in components { - #( - if comp.id == Comp~I::Component::id() { - comp_~I = Some(lock_component(comp)); - continue; - } - )* - } - (#( - Comp~I::Ref::from_optional_component(comp_~I, world), + Comp~I::Ref::from_optional_component( + component_index_lookup(Comp~I::Component::id()) + .and_then(|component_index| { + components.get(component_index) + .map(lock_component) + }), + world + ), )*) } } |