add support for empty functions as systems
This commit is contained in:
parent
2d3903299b
commit
d88f9a7913
@ -92,8 +92,8 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait IntoSystem<'a, ResourceArgs, ComponentArgs>
|
pub trait IntoSystem<'a, ResourceArgs, ComponentArgs>
|
||||||
where
|
// where
|
||||||
ComponentArgs: IntoQuery + DefaultFilter,
|
// ComponentArgs: IntoQuery + DefaultFilter,
|
||||||
{
|
{
|
||||||
fn into_system(self, name: &'static str) -> Box<dyn Schedulable>;
|
fn into_system(self, name: &'static str) -> Box<dyn Schedulable>;
|
||||||
}
|
}
|
||||||
@ -173,37 +173,35 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! impl_system {
|
macro_rules! impl_system {
|
||||||
($(($view:ident, $filter:ident, $var:ident)),+) => {
|
(($(($view:ident, $filter:ident, $var:ident)),*)) => {
|
||||||
impl<'a,
|
impl<'a,
|
||||||
Func,
|
Func,
|
||||||
$($view: for<'b> View<'b> + DefaultFilter<Filter = $filter> + ViewElement,
|
$($view: for<'b> View<'b> + DefaultFilter<Filter = $filter> + ViewElement,
|
||||||
$filter: EntityFilter + Sync + 'static),+
|
$filter: EntityFilter + Sync + 'static),*
|
||||||
> IntoSystem<'a, (), ($($view,)+)> for Func
|
> IntoSystem<'a, (), ($($view,)*)> for Func
|
||||||
where
|
where
|
||||||
Func: FnMut($($view),+) + Send + Sync + 'static,
|
Func: FnMut($($view),*) + Send + Sync + 'static,
|
||||||
$(<$view as View<'a>>::Iter: Iterator<Item = $view>),+
|
$(<$view as View<'a>>::Iter: Iterator<Item = $view>),*
|
||||||
{
|
{
|
||||||
fn into_system(mut self, name: &'static str) -> Box<dyn Schedulable> {
|
fn into_system(mut self, name: &'static str) -> Box<dyn Schedulable> {
|
||||||
let resource_access: Access<ResourceTypeId> = Access::default();
|
let resource_access: Access<ResourceTypeId> = Access::default();
|
||||||
let component_access: Access<ComponentTypeId> = component_access!(($($view),+));
|
let component_access: Access<ComponentTypeId> = component_access!(($($view),*));
|
||||||
|
|
||||||
let run_fn = FuncSystemFnWrapper(
|
let run_fn = FuncSystemFnWrapper(
|
||||||
move |_,
|
move |_,
|
||||||
world,
|
world,
|
||||||
_: (),
|
_: (),
|
||||||
query: &mut system_query!($($view, $filter),+)
|
query: &mut system_query!($($view, $filter),*)
|
||||||
,
|
,
|
||||||
| {
|
| {
|
||||||
for tuple!($($var),+) in query.iter_mut(world) {
|
run_system!(self, query, world, $($var),*)
|
||||||
self($($var),+);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
PhantomData,
|
PhantomData,
|
||||||
);
|
);
|
||||||
|
|
||||||
Box::new(FuncSystem {
|
Box::new(FuncSystem {
|
||||||
name: name.into(),
|
name: name.into(),
|
||||||
queries: AtomicRefCell::new(query!($($view),+)),
|
queries: AtomicRefCell::new(query!($($view),*)),
|
||||||
access: SystemAccess {
|
access: SystemAccess {
|
||||||
resources: resource_access,
|
resources: resource_access,
|
||||||
components: component_access,
|
components: component_access,
|
||||||
@ -219,7 +217,19 @@ macro_rules! impl_system {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! run_system {
|
||||||
|
($me:ident, $query:ident, $world:ident, ) => {{
|
||||||
|
$me();
|
||||||
|
}};
|
||||||
|
($me:ident, $query:ident, $world:ident, $($var:ident),+) => {{
|
||||||
|
for tuple!($($var),*) in $query.iter_mut($world) {
|
||||||
|
$me($($var),*);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! tuple {
|
macro_rules! tuple {
|
||||||
|
() => { () };
|
||||||
// single value: v1
|
// single value: v1
|
||||||
($value:ident) => { $value };
|
($value:ident) => { $value };
|
||||||
// multiple values: (v1, v2, v3)
|
// multiple values: (v1, v2, v3)
|
||||||
@ -241,6 +251,9 @@ macro_rules! component_access {
|
|||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! system_query {
|
macro_rules! system_query {
|
||||||
|
() => {
|
||||||
|
()
|
||||||
|
};
|
||||||
($view:ident, $filter:ident) => {
|
($view:ident, $filter:ident) => {
|
||||||
SystemQuery<
|
SystemQuery<
|
||||||
$view,
|
$view,
|
||||||
@ -266,36 +279,38 @@ macro_rules! system_query {
|
|||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! query {
|
macro_rules! query {
|
||||||
(()) => { () };
|
() => { () };
|
||||||
($($query:ident),+) => {
|
($($query:ident),+) => {
|
||||||
<tuple!($($query),+)>::query()
|
<tuple!($($query),+)>::query()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
impl_system![(A, AF, a)];
|
impl_system![()];
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
impl_system![(A, AF, a), (B, BF, b)];
|
impl_system![((A, AF, a))];
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
impl_system![(A, AF, a), (B, BF, b), (C, CF, c)];
|
impl_system![((A, AF, a), (B, BF, b))];
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
impl_system![(A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d)];
|
impl_system![((A, AF, a), (B, BF, b), (C, CF, c))];
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
impl_system![(A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e)];
|
impl_system![((A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d))];
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
impl_system![(A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e), (F, FF, f)];
|
impl_system![((A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e))];
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
impl_system![(A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e), (F, FF, f), (G, GF, g)];
|
impl_system![((A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e), (F, FF, f))];
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
impl_system![(A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e), (F, FF, f), (G, GF, g), (H, HF, h)];
|
impl_system![((A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e), (F, FF, f), (G, GF, g))];
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
impl_system![(A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e), (F, FF, f), (G, GF, g), (H, HF, h), (I, IF, i)];
|
impl_system![((A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e), (F, FF, f), (G, GF, g), (H, HF, h))];
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
impl_system![(A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e), (F, FF, f), (G, GF, g), (H, HF, h), (I, IF, i), (J, JF, j)];
|
impl_system![((A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e), (F, FF, f), (G, GF, g), (H, HF, h), (I, IF, i))];
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
impl_system![(A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e), (F, FF, f), (G, GF, g), (H, HF, h), (I, IF, i), (J, JF, j), (K, KF, k)];
|
impl_system![((A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e), (F, FF, f), (G, GF, g), (H, HF, h), (I, IF, i), (J, JF, j))];
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
impl_system![(A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e), (F, FF, f), (G, GF, g), (H, HF, h), (I, IF, i), (J, JF, j), (K, KF, k), (L, LF, l)];
|
impl_system![((A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e), (F, FF, f), (G, GF, g), (H, HF, h), (I, IF, i), (J, JF, j), (K, KF, k))];
|
||||||
|
#[rustfmt::skip]
|
||||||
|
impl_system![((A, AF, a), (B, BF, b), (C, CF, c), (D, DF, d), (E, EF, e), (F, FF, f), (G, GF, g), (H, HF, h), (I, IF, i), (J, JF, j), (K, KF, k), (L, LF, l))];
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
@ -354,14 +369,21 @@ mod tests {
|
|||||||
// let mut system = resource_system.into_system("hi");
|
// let mut system = resource_system.into_system("hi");
|
||||||
// system.run(&mut world, &mut resources);
|
// system.run(&mut world, &mut resources);
|
||||||
|
|
||||||
fn resource_system_mut(mut a: ResourceMut<A>, x: Ref<X>, y: Ref<Y>) {
|
fn empty_system_mut() {
|
||||||
let hi = &mut a;
|
println!("hello world");
|
||||||
a.0 += 1;
|
|
||||||
println!("{} {} {}", a.0, x.0, y.0);
|
|
||||||
}
|
}
|
||||||
let mut system = resource_system_mut.into_system("hi");
|
|
||||||
|
let mut system = empty_system_mut.into_system("hi");
|
||||||
system.run(&mut world, &mut resources);
|
system.run(&mut world, &mut resources);
|
||||||
|
|
||||||
|
// fn resource_system_mut(mut a: ResourceMut<A>, x: Ref<X>, y: Ref<Y>) {
|
||||||
|
// let hi = &mut a;
|
||||||
|
// a.0 += 1;
|
||||||
|
// println!("{} {} {}", a.0, x.0, y.0);
|
||||||
|
// }
|
||||||
|
// let mut system = resource_system_mut.into_system("hi");
|
||||||
|
// system.run(&mut world, &mut resources);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user