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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use std::any::type_name;
use crate::resources::{ResourceConflict, Resources};
/// Trait for the (possibly parallel) runner for a `System`.
pub trait Pool {
/// Should run the two functions (potentially in parallel) and return their results.
fn join<A, B, RA, RB>(&self, a: A, b: B) -> (RA, RB)
where
A: FnOnce() -> RA + Send,
B: FnOnce() -> RB + Send,
RA: Send,
RB: Send;
}
/// Trait for error types returned from `System::run`.
///
/// Errors must be combinable because systems may be run in parallel, and thus may result in
/// multiple errors before stopping.
pub trait Error {
fn combine(self, other: Self) -> Self;
}
/// A system that may be run in parallel or in sequence with other such systems in a group.
///
/// This trait is designed so that systems may read or write to resources inside the `source`
/// parameter. Systems report the resources they intend to use abstractly through the `Resources`
/// type, and this provides the ability to check parallel systems for resource conflicts.
//
// TODO: It would be much nicer if our `System` trait could be this:
//
// ```
// pub trait System<'a> {
// type Resources: Resources;
// type Pool: Pool;
// type Args: ?Sized + 'a;
// type Error: Error;
//
// fn check_resources(&self) -> Result<Self::Resources, ResourceConflict>;
//
// fn run(
// &mut self,
// pool: &Self::Pool,
// args: &Self::Args,
// ) -> Result<(), Self::Error>;
// }
// ```
//
// This would allow dropping the `Source` associated type and would be much more general, allowing
// you to pass arbitrary non-'static arguments as parameters to `System::run` if your systems
// implement `for<'a> System<'a>`.
// However, when we implement this `System` trait for `Par` and `Seq` and try to use this with more
// than a few systems, we unfortunately run into quadratic or exponential Rust compiler behavior
// (Maybe this bug: https://github.com/rust-lang/rust/issues/69671).
//
// When this issue is fixed this trait should be changed.
pub trait System {
type Source: ?Sized;
type Resources: Resources;
type Pool: Pool;
type Args: ?Sized;
type Error: Error;
/// Check for any internal resource conficts and if there are none, return a `Resources` that
/// represents the used resources.
///
/// Must be a constant value, this will generally only be called once.
fn check_resources(&self) -> Result<Self::Resources, ResourceConflict>;
fn run(
&mut self,
pool: &Self::Pool,
source: &Self::Source,
args: &Self::Args,
) -> Result<(), Self::Error>;
}
impl<S> System for Box<S>
where
S: ?Sized + System,
{
type Source = S::Source;
type Resources = S::Resources;
type Pool = S::Pool;
type Args = S::Args;
type Error = S::Error;
fn check_resources(&self) -> Result<Self::Resources, ResourceConflict> {
(**self).check_resources()
}
fn run(
&mut self,
pool: &Self::Pool,
source: &Self::Source,
args: &Self::Args,
) -> Result<(), Self::Error> {
(**self).run(pool, source, args)
}
}
pub struct Par<H, T> {
head: H,
tail: T,
}
impl<H, T> Par<H, T> {
pub fn new(head: H, tail: T) -> Par<H, T> {
Par { head, tail }
}
pub fn with<S>(self, sys: S) -> Par<Par<H, T>, S> {
Par {
head: self,
tail: sys,
}
}
}
impl<H, T, S, R, P, A, E> System for Par<H, T>
where
H: System<Source = S, Resources = R, Pool = P, Args = A, Error = E> + Send,
T: System<Source = S, Resources = R, Pool = P, Args = A, Error = E> + Send,
S: ?Sized + Sync,
R: Resources + Send,
P: Pool + Sync,
A: ?Sized + Sync,
E: Error + Send,
{
type Source = S;
type Resources = R;
type Pool = P;
type Args = A;
type Error = E;
fn check_resources(&self) -> Result<Self::Resources, ResourceConflict> {
let hr = self.head.check_resources()?;
let tr = self.tail.check_resources()?;
if hr.conflicts_with(&tr) {
Err(ResourceConflict {
type_name: type_name::<Self>(),
})
} else {
let mut resources = hr;
resources.union(&tr);
Ok(resources)
}
}
fn run(
&mut self,
pool: &Self::Pool,
source: &Self::Source,
args: &Self::Args,
) -> Result<(), Self::Error> {
let Self { head, tail, .. } = self;
match pool.join(
move || head.run(pool, source, args),
move || tail.run(pool, source, args),
) {
(Ok(()), Ok(())) => Ok(()),
(Err(a), Ok(())) => Err(a),
(Ok(()), Err(b)) => Err(b),
(Err(a), Err(b)) => Err(a.combine(b)),
}
}
}
#[macro_export]
macro_rules! par {
($head:expr, $tail:expr $(, $rest:expr)* $(,)?) => {
{
$crate::par_seq::Par::new($head, $tail)
$(.with($rest))*
}
};
}
pub struct Seq<H, T> {
head: H,
tail: T,
}
impl<H, T> Seq<H, T> {
pub fn new(head: H, tail: T) -> Seq<H, T> {
Seq { head, tail }
}
pub fn with<S>(self, sys: S) -> Seq<Seq<H, T>, S> {
Seq {
head: self,
tail: sys,
}
}
}
impl<H, T, S, R, P, A, E> System for Seq<H, T>
where
H: System<Source = S, Resources = R, Pool = P, Args = A, Error = E>,
T: System<Source = S, Resources = R, Pool = P, Args = A, Error = E>,
S: ?Sized,
R: Resources,
P: Pool,
A: ?Sized,
E: Error,
{
type Source = S;
type Resources = R;
type Pool = P;
type Args = A;
type Error = E;
fn check_resources(&self) -> Result<Self::Resources, ResourceConflict> {
let mut r = self.head.check_resources()?;
r.union(&self.tail.check_resources()?);
Ok(r)
}
fn run(
&mut self,
pool: &Self::Pool,
source: &Self::Source,
args: &Self::Args,
) -> Result<(), Self::Error> {
self.head.run(pool, source, args)?;
self.tail.run(pool, source, args)
}
}
#[macro_export]
macro_rules! seq {
($head:expr, $tail:expr $(, $rest:expr)* $(,)?) => {
{
$crate::par_seq::Seq::new($head, $tail)
$( .with($rest) )*
}
};
}
/// A system runner that runs parallel systems single-threaded in the current thread.
#[derive(Default)]
pub struct SeqPool;
impl Pool for SeqPool {
fn join<A, B, RA, RB>(&self, a: A, b: B) -> (RA, RB)
where
A: FnOnce() -> RA + Send,
B: FnOnce() -> RB + Send,
RA: Send,
RB: Send,
{
let ra = a();
let rb = b();
(ra, rb)
}
}