1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
use std::any::Any;
use std::convert::Infallible;
use std::fmt::Debug;
use crate::component::Sequence as ComponentSequence;
use crate::{ComponentStorage, Query};
pub mod stateful;
pub trait System<Impl>: 'static
{
type Query<'a>;
type Input;
#[must_use]
fn initialize(self, input: Self::Input) -> Self;
fn run(&mut self, component_storage: &mut ComponentStorage);
fn into_type_erased(self) -> TypeErased;
}
impl<Func, Comps> System<fn(Query<Comps>)> for Func
where
Func: Fn(Query<Comps>) + 'static,
Comps: ComponentSequence,
{
type Input = Infallible;
type Query<'a> = Query<'a, Comps>;
fn initialize(self, _input: Self::Input) -> Self
{
self
}
fn run(&mut self, component_storage: &mut ComponentStorage)
{
self(Query::new(component_storage));
}
fn into_type_erased(self) -> TypeErased
{
TypeErased {
data: Box::new(self),
func: Box::new(|data, component_storage| {
let me = data.downcast_mut::<Func>().unwrap();
me.run(component_storage);
}),
}
}
}
pub trait Into<Impl>
{
type System;
fn into_system(self) -> Self::System;
}
pub struct TypeErased
{
data: Box<dyn Any>,
func: Box<TypeErasedFunc>,
}
impl TypeErased
{
pub fn run(&mut self, component_storage: &mut ComponentStorage)
{
(self.func)(self.data.as_mut(), component_storage);
}
}
impl Debug for TypeErased
{
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
{
formatter.debug_struct("TypeErased").finish_non_exhaustive()
}
}
/// Function in [`TypeErased`] used to run the system.
type TypeErasedFunc = dyn Fn(&mut dyn Any, &mut ComponentStorage);
|