use crate::{ecs::resources::Resources, ecs::system::Res};
pub trait Dependencies<'a>: Sized {
fn try_gather(world: &'a hecs::World, resources: &'a Resources) -> Option<Self>;
}
impl<'a> Dependencies<'a> for () {
fn try_gather(_world: &'a hecs::World, _resources: &'a Resources) -> Option<Self> {
Some(())
}
}
impl<'a, A: 'static + Send + Sync> Dependencies<'a> for Res<'a, A> {
fn try_gather(world: &'a hecs::World, resources: &'a Resources) -> Option<Self> {
if !resources.has_resource::<A>(world) {
return None;
}
Some(Res {
data: resources.get_resource(world),
})
}
}
macro_rules! impl_dependencies_tuple {
($($T:ident),+ $(,)?) => {
impl<'a, $($T),+> Dependencies<'a> for ($(Res<'a, $T>),+,)
where
$($T: 'static + Send + Sync),+
{
fn try_gather(world: &'a hecs::World, resources: &'a Resources) -> Option<Self> {
if $(!resources.has_resource::<$T>(world))||+ {
return None;
}
Some((
$(
Res::<$T> {
data: resources.get_resource(world),
}
),+,
))
}
}
};
}
impl_dependencies_tuple!(A, B);
impl_dependencies_tuple!(A, B, C);
impl_dependencies_tuple!(A, B, C, D);
impl_dependencies_tuple!(A, B, C, D, E);
impl_dependencies_tuple!(A, B, C, D, E, F);
impl_dependencies_tuple!(A, B, C, D, E, F, G);
impl_dependencies_tuple!(A, B, C, D, E, F, G, H);