Skip to content

Commit

Permalink
System-yeet for .run and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-i-cecile committed Jul 30, 2021
1 parent 82c0443 commit c0a1801
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
52 changes: 51 additions & 1 deletion crates/bevy_ecs/src/system/function_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,52 @@ where
}
}

/// Allows end users to call system-running methods from the [`System`](crate::system::System) trait without .system().
pub trait RunnableSystem<In, Out, Param: SystemParam, Marker>:
IntoSystem<In, Out, (IsFunctionSystem, Param, Marker)>
{
fn apply_buffers(self, world: &mut World);

fn initialize(self, _world: &mut World);

fn run(self, input: In, world: &mut World) -> Out;

fn run_direct(self, input: In, world: &mut World) -> Out;
}

impl<In, Out, Param: SystemParam, Marker, F> RunnableSystem<In, Out, Param, Marker> for F
where
In: 'static,
Out: 'static,
Param: SystemParam + 'static,
Marker: 'static,
F: SystemParamFunction<In, Out, Param, Marker>
+ IntoSystem<
In,
Out,
(IsFunctionSystem, Param, Marker),
System = FunctionSystem<In, Out, Param, Marker, F>,
> + Send
+ Sync
+ 'static,
{
fn apply_buffers(self, world: &mut World) {
self.system().apply_buffers(world);
}

fn initialize(self, world: &mut World) {
self.system().initialize(world);
}

fn run(self, input: In, world: &mut World) -> Out {
self.system().run(input, world)
}

fn run_direct(self, input: In, world: &mut World) -> Out {
self.system().run_direct(input, world)
}
}

pub struct IsFunctionSystem;

impl<In, Out, Param, Marker, F> IntoSystem<In, Out, (IsFunctionSystem, Param, Marker)> for F
Expand Down Expand Up @@ -366,13 +412,17 @@ where
#[inline]
unsafe fn run_unsafe(&mut self, input: Self::In, world: &World) -> Self::Out {
let change_tick = world.increment_change_tick();
let out = self.func.run(
// Trait disambiguation required here to disambiguate .run call
// the .run found in the `RunnableSystem` trait
let out = <F as SystemParamFunction<In, Out, Param, Marker>>::run(
&mut self.func,
input,
self.param_state.as_mut().unwrap(),
&self.system_meta,
world,
change_tick,
);

self.system_meta.last_change_tick = change_tick;
out
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/system/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub trait System: Send + Sync + 'static {
/// }
///
/// world.insert_resource::<Counter>(Counter(0));
/// count_up.system().run_direct((), &mut world);
/// count_up.run_direct((), &mut world);
/// let counter = world.get_resource::<Counter>().unwrap();
/// assert_eq!(counter.0, 1);
/// ```
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::{
},
entity::{Entities, Entity},
query::{FilterFetch, QueryState, WorldQuery},
schedule::{IntoSystemDescriptor, SystemDescriptor},
storage::{Column, SparseSet, Storages},
};
use std::{
Expand Down

0 comments on commit c0a1801

Please sign in to comment.