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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use std::{any::type_name, borrow::Cow};
use crate::{
borrow::{Borrows, ComponentBorrow, ContextBorrow, IntoBorrow},
Context, Result,
};
pub type SystemName = Cow<'static, str>;
pub trait System<Args, Ret> {
fn execute(&mut self, context: &Context) -> Result<()>;
fn name(&self) -> SystemName;
fn borrows() -> Borrows;
}
macro_rules! tuple_impl {
($($name: ident), *) => {
impl<Func, $($name,) *> System<($($name,)*), ()> for Func
where
for<'a, 'b> &'b mut Func:
FnMut($($name,)*) +
FnMut($(<$name::Borrow as ContextBorrow<'a>>::Target),*),
$($name: IntoBorrow + ComponentBorrow,)*
{
fn execute(&mut self, context: &Context) -> Result<()> {
let mut func = self;
(&mut func)($($name::Borrow::borrow(context)?), *);
Ok(())
}
fn name(&self) -> SystemName {
type_name::<Func>().into()
}
fn borrows() -> Borrows {
([].iter()
$(.chain($name::borrows().iter())) *).cloned()
.collect()
}
}
impl<Err, Func, $($name,) *> System<($($name,)*), std::result::Result<(), Err>> for Func
where
Err: Into<anyhow::Error>,
for<'a, 'b> &'b mut Func:
FnMut($($name,)*) -> std::result::Result<(), Err> +
FnMut($(<$name::Borrow as ContextBorrow<'a>>::Target),*) -> std::result::Result<(), Err>,
$($name: IntoBorrow + ComponentBorrow,)*
{
fn execute(&mut self, context: &Context) -> Result<()> {
let mut func = self;
match (&mut func)($($name::Borrow::borrow(context)?), *) {
Ok(()) => Ok(()),
Err(e) => Err(crate::Error::SystemError(<Self as System<($($name,)*), std::result::Result<(), Err>>>::name(func), e.into())),
}
}
fn name(&self) -> SystemName {
type_name::<Func>().into()
}
fn borrows() -> Borrows {
([].iter()
$(.chain($name::borrows().iter())) *).cloned()
.collect()
}
}
};
}
impl<F: FnMut()> System<(), ()> for F {
fn execute(&mut self, _: &Context) -> Result<()> {
(self)();
Ok(())
}
fn name(&self) -> SystemName {
"System<()>".into()
}
fn borrows() -> Borrows {
Borrows::default()
}
}
impl<Err: Into<anyhow::Error>, F: FnMut() -> std::result::Result<(), Err>>
System<(), std::result::Result<(), Err>> for F
{
fn execute(&mut self, _: &Context) -> Result<()> {
(self)().map_err(|e| crate::Error::SystemError(self.name(), e.into()))
}
fn name(&self) -> SystemName {
"System<()>".into()
}
fn borrows() -> Borrows {
Borrows::default()
}
}
impl_for_tuples!(tuple_impl);
#[cfg(test)]
mod tests {
use crate::{system::System, Context, IntoData, Read, SubWorld};
use hecs::World;
use anyhow::{ensure, Result};
fn count_system(val: Read<i32>) {
assert_eq!(*val, 6);
}
#[test]
fn simple_system() {
struct App {
name: &'static str,
}
let mut val = 6_i32;
let mut app = App {
name: "hecs-schedule",
};
let mut world = World::default();
let a = world.spawn(("a", 3));
let b = world.spawn(("b", 42));
let c = world.spawn(("c", 8));
let data = unsafe { (&mut world, &mut app, &mut val).into_data(&mut ()) };
let context = Context::new(&data);
let mut count_closure = |w: SubWorld<&i32>| assert_eq!(w.query::<&i32>().iter().count(), 3);
let mut foo = |_: SubWorld<&i32>| {};
let mut name_query_system = |w: SubWorld<&String>| -> Result<()> {
let name = w.get::<String>(a)?;
eprintln!("Name: {:?}", *name);
Ok(())
};
let mut name_check_system = |w: SubWorld<&&'static str>| -> anyhow::Result<()> {
for (e, n) in [(a, "a"), (b, "b"), (c, "c")] {
let name = w.get::<&str>(e)?;
ensure!(*name == n, "Names did not match");
}
Ok(())
};
let mut rename_system = |w: SubWorld<&mut String>, a: Read<App>| -> anyhow::Result<()> {
ensure!(a.name == "hecs-schedule", "App name did not match");
w.try_query::<&mut String>()?
.iter()
.for_each(|(_, name)| *name = a.name.into());
Ok(())
};
let mut check_rename_system = |w: SubWorld<&String>| -> anyhow::Result<()> {
ensure!(
w.try_query::<&String>()?
.iter()
.all(|(_, name)| name == "hecs-schedule"),
"Names were not properly updated"
);
Ok(())
};
count_system.execute(&context).unwrap();
foo.execute(&context).unwrap();
count_closure.execute(&context).unwrap();
assert!(name_query_system.execute(&context).is_err());
name_check_system.execute(&context).unwrap();
rename_system.execute(&context).unwrap();
check_rename_system.execute(&context).unwrap();
}
}