use std::{marker::PhantomData, ops::Deref};
use crate::{ResourceId, World};
pub trait Accessor: Sized {
fn try_new() -> Option<Self>;
fn reads(&self) -> Vec<ResourceId>;
fn writes(&self) -> Vec<ResourceId>;
}
impl Accessor for () {
fn try_new() -> Option<Self> {
None
}
fn reads(&self) -> Vec<ResourceId> {
Vec::new()
}
fn writes(&self) -> Vec<ResourceId> {
Vec::new()
}
}
impl<T: ?Sized> Accessor for PhantomData<T> {
fn try_new() -> Option<Self> {
None
}
fn reads(&self) -> Vec<ResourceId> {
Vec::new()
}
fn writes(&self) -> Vec<ResourceId> {
Vec::new()
}
}
pub enum AccessorCow<'a, 'b, T>
where
AccessorTy<'a, T>: 'b,
T: System<'a> + ?Sized,
'a: 'b,
{
Ref(&'b AccessorTy<'a, T>),
Owned(AccessorTy<'a, T>),
}
impl<'a, 'b, T> Deref for AccessorCow<'a, 'b, T>
where
AccessorTy<'a, T>: 'b,
T: System<'a> + ?Sized + 'b,
'a: 'b,
{
type Target = AccessorTy<'a, T>;
fn deref(&self) -> &AccessorTy<'a, T> {
match self {
AccessorCow::Ref(r) => r,
AccessorCow::Owned(ref o) => o,
}
}
}
type AccessorTy<'a, T> = <<T as System<'a>>::SystemData as DynamicSystemData<'a>>::Accessor;
pub trait RunNow<'a> {
fn run_now(&mut self, world: &'a World);
fn setup(&mut self, world: &mut World);
#[allow(clippy::boxed_local)]
fn dispose(self: Box<Self>, world: &mut World) {
let _ = world;
}
}
impl<'a, T> RunNow<'a> for T
where
T: System<'a>,
{
fn run_now(&mut self, world: &'a World) {
let data = T::SystemData::fetch(&self.accessor(), world);
self.run(data);
}
fn setup(&mut self, world: &mut World) {
T::setup(self, world);
}
fn dispose(self: Box<Self>, world: &mut World) {
T::dispose(*self, world);
}
}
#[repr(u8)]
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug)]
pub enum RunningTime {
VeryShort = 1,
Short = 2,
Average = 3,
Long = 4,
VeryLong = 5,
}
pub trait System<'a> {
type SystemData: DynamicSystemData<'a>;
fn run(&mut self, data: Self::SystemData);
fn running_time(&self) -> RunningTime {
RunningTime::Average
}
fn accessor<'b>(&'b self) -> AccessorCow<'a, 'b, Self> {
AccessorCow::Owned(
AccessorTy::<'a, Self>::try_new().expect("Missing implementation for `accessor`"),
)
}
fn setup(&mut self, world: &mut World) {
<Self::SystemData as DynamicSystemData>::setup(&self.accessor(), world)
}
fn dispose(self, world: &mut World)
where
Self: Sized,
{
let _ = world;
}
}
pub trait SystemData<'a> {
fn setup(world: &mut World);
fn fetch(world: &'a World) -> Self;
fn reads() -> Vec<ResourceId>;
fn writes() -> Vec<ResourceId>;
}
impl<'a, T> DynamicSystemData<'a> for T
where
T: SystemData<'a>,
{
type Accessor = StaticAccessor<T>;
fn setup(_: &StaticAccessor<T>, world: &mut World) {
T::setup(world);
}
fn fetch(_: &StaticAccessor<T>, world: &'a World) -> Self {
T::fetch(world)
}
}
impl<'a> SystemData<'a> for () {
fn setup(_: &mut World) {}
fn fetch(_: &'a World) -> Self {}
fn reads() -> Vec<ResourceId> {
Vec::new()
}
fn writes() -> Vec<ResourceId> {
Vec::new()
}
}
#[derive(Default)]
pub struct StaticAccessor<T> {
marker: PhantomData<fn() -> T>,
}
impl<'a, T> Accessor for StaticAccessor<T>
where
T: SystemData<'a>,
{
fn try_new() -> Option<Self> {
Some(StaticAccessor {
marker: PhantomData,
})
}
fn reads(&self) -> Vec<ResourceId> {
T::reads()
}
fn writes(&self) -> Vec<ResourceId> {
T::writes()
}
}
pub trait DynamicSystemData<'a> {
type Accessor: Accessor;
fn setup(accessor: &Self::Accessor, world: &mut World);
fn fetch(access: &Self::Accessor, world: &'a World) -> Self;
}
impl<'a, T: ?Sized> SystemData<'a> for PhantomData<T> {
fn setup(_: &mut World) {}
fn fetch(_: &World) -> Self {
PhantomData
}
fn reads() -> Vec<ResourceId> {
vec![]
}
fn writes() -> Vec<ResourceId> {
vec![]
}
}
macro_rules! impl_data {
( $($ty:ident),* ) => {
impl<'a, $($ty),*> SystemData<'a> for ( $( $ty , )* )
where $( $ty : SystemData<'a> ),*
{
fn setup(world: &mut World) {
#![allow(unused_variables)]
$(
<$ty as SystemData>::setup(&mut *world);
)*
}
fn fetch(world: &'a World) -> Self {
#![allow(unused_variables)]
( $( <$ty as SystemData<'a>>::fetch(world), )* )
}
fn reads() -> Vec<ResourceId> {
#![allow(unused_mut)]
let mut r = Vec::new();
$( {
let mut reads = <$ty as SystemData>::reads();
r.append(&mut reads);
} )*
r
}
fn writes() -> Vec<ResourceId> {
#![allow(unused_mut)]
let mut r = Vec::new();
$( {
let mut writes = <$ty as SystemData>::writes();
r.append(&mut writes);
} )*
r
}
}
};
}
mod impl_data {
#![cfg_attr(rustfmt, rustfmt_skip)]
use super::*;
impl_data!(A);
impl_data!(A, B);
impl_data!(A, B, C);
impl_data!(A, B, C, D);
impl_data!(A, B, C, D, E);
impl_data!(A, B, C, D, E, F);
impl_data!(A, B, C, D, E, F, G);
impl_data!(A, B, C, D, E, F, G, H);
impl_data!(A, B, C, D, E, F, G, H, I);
impl_data!(A, B, C, D, E, F, G, H, I, J);
impl_data!(A, B, C, D, E, F, G, H, I, J, K);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y);
impl_data!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z);
}