Skip to content

Commit

Permalink
impl Command for <impl FnOnce(&mut World)>
Browse files Browse the repository at this point in the history
closes bevyengine#2899

Implements Command for F
where F: FnOnce(&mut World) + Send + Sync + 'static

so that within a system, a simple one-off command can be added without
having to define a new type for the command.
  • Loading branch information
sseemayer committed Oct 21, 2021
1 parent 6a8a8c9 commit 4d26bd7
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/bevy_ecs/src/system/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,15 @@ impl<'w, 's, 'a> EntityCommands<'w, 's, 'a> {
}
}

impl<F> Command for F
where
F: FnOnce(&mut World) + Send + Sync + 'static,
{
fn write(self, world: &mut World) {
self(world)
}
}

#[derive(Debug)]
pub struct Spawn<T> {
pub bundle: T,
Expand Down Expand Up @@ -768,6 +777,10 @@ mod tests {
#[derive(Component)]
struct W<T>(T);

fn simple_command(world: &mut World) {
println!("We have {} components", world.components().len());
}

#[test]
fn commands() {
let mut world = World::default();
Expand Down Expand Up @@ -796,6 +809,15 @@ mod tests {
.map(|(a, b)| (a.0, b.0))
.collect::<Vec<_>>();
assert_eq!(results2, vec![]);

{
let mut commands = Commands::new(&mut command_queue, &world);
commands.add(|world: &mut World| {
println!("We have {} entities", world.entities().len());
});

commands.add(simple_command);
}
}

#[test]
Expand Down

0 comments on commit 4d26bd7

Please sign in to comment.