add immutable versions of run_on_hierarchy to save allocations

This commit is contained in:
Carter Anderson 2020-01-17 01:29:01 -08:00
parent e649d4f6e1
commit 3a674394b9
3 changed files with 52 additions and 8 deletions

View File

@ -1,6 +1,28 @@
use crate::prelude::{Children, Entity, SubWorld, World};
pub fn run_on_hierarchy<T>(
world: &World,
entity: Entity,
input: T,
func: &mut dyn FnMut(&World, Entity, T) -> Option<T>,
) where
T: Copy,
{
let result = func(world, entity, input);
if let Some(result) = result {
match world.get_component::<Children>(entity) {
Some(children) => Some(
for child in children.iter() {
run_on_hierarchy(world, *child, result, func);
}
),
None => None,
};
}
}
pub fn run_on_hierarchy_mut<T>(
world: &mut World,
entity: Entity,
input: T,
@ -24,17 +46,39 @@ pub fn run_on_hierarchy<T>(
if let Some(result) = result {
if let Some(children) = children {
for child in children {
run_on_hierarchy(world, child, result, func);
run_on_hierarchy_mut(world, child, result, func);
}
}
}
}
pub fn run_on_hierarchy_subworld<T>(
world: &mut legion::system::SubWorld,
world: &SubWorld,
entity: Entity,
input: T,
func: &dyn Fn(&mut SubWorld, Entity, T) -> Option<T>,
func: &mut dyn FnMut(&SubWorld, Entity, T) -> Option<T>,
) where
T: Copy,
{
let result = func(world, entity, input);
if let Some(result) = result {
match world.get_component::<Children>(entity) {
Some(children) => Some(
for child in children.iter() {
run_on_hierarchy_subworld(world, *child, result, func);
}
),
None => None,
};
}
}
pub fn run_on_hierarchy_subworld_mut<T>(
world: &mut SubWorld,
entity: Entity,
input: T,
func: &mut dyn FnMut(&mut SubWorld, Entity, T) -> Option<T>,
) where
T: Copy,
{
@ -54,8 +98,8 @@ pub fn run_on_hierarchy_subworld<T>(
if let Some(result) = result {
if let Some(children) = children {
for child in children {
run_on_hierarchy_subworld(world, child, result, func);
run_on_hierarchy_subworld_mut(world, child, result, func);
}
}
}
}
}

View File

@ -52,7 +52,7 @@ impl UiPipeline {
// TODO: this probably isn't the best way to handle z-ordering
let mut z = 0.9999;
{
let mut add_data: Box<dyn FnMut(&mut World, Entity, ()) -> Option<()>> =
let mut add_data: Box<dyn FnMut(&World, Entity, ()) -> Option<()>> =
Box::new(|world, entity, _| {
let node = world.get_component::<Node>(entity).unwrap();
data.push(RectData {

View File

@ -12,11 +12,11 @@ pub fn build_ui_update_system() -> Box<dyn Schedulable> {
let parent_size = math::vec2(window_size.width as f32, window_size.height as f32);
let parent_position = math::vec2(0.0, 0.0);
for (entity, _) in node_query.iter_entities_mut(world) {
ecs::run_on_hierarchy_subworld(
ecs::run_on_hierarchy_subworld_mut(
world,
entity,
(parent_size, parent_position),
&update_node_entity,
&mut update_node_entity,
);
}
})