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
use crate::ecs::{SystemData, World};
pub trait EventReader<'a> {
type SystemData: SystemData<'a>;
type Event: Clone + Send + Sync + 'static;
fn read(&mut self, _: Self::SystemData, _: &mut Vec<Self::Event>);
fn read_from_world(&mut self, world: &'a World, events: &mut Vec<Self::Event>) {
self.read(world.system_data(), events);
}
fn setup(&mut self, res: &mut World) {
Self::SystemData::setup(res);
}
}
#[cfg(test)]
mod tests {
use crate::ecs::Read;
use crate::shrev::{EventChannel, ReaderId};
use super::*;
#[derive(Clone)]
pub struct TestEvent;
pub struct TestEventReader {
reader: ReaderId<TestEvent>,
}
impl<'a> EventReader<'a> for TestEventReader {
type SystemData = Read<'a, EventChannel<TestEvent>>;
type Event = TestEvent;
fn read(&mut self, events: Self::SystemData, data: &mut Vec<TestEvent>) {
data.extend(events.read(&mut self.reader).cloned());
}
}
#[derive(Clone)]
pub struct OtherEvent;
pub struct OtherEventReader {
reader: ReaderId<OtherEvent>,
}
impl<'a> EventReader<'a> for OtherEventReader {
type SystemData = Read<'a, EventChannel<OtherEvent>>;
type Event = OtherEvent;
fn read(&mut self, events: Self::SystemData, data: &mut Vec<OtherEvent>) {
data.extend(events.read(&mut self.reader).cloned());
}
}
#[derive(Clone)]
pub enum AggregateEvent {
Test(TestEvent),
Other(OtherEvent),
}
impl From<TestEvent> for AggregateEvent {
fn from(event: TestEvent) -> Self {
AggregateEvent::Test(event.clone())
}
}
impl From<OtherEvent> for AggregateEvent {
fn from(event: OtherEvent) -> Self {
AggregateEvent::Other(event)
}
}
#[allow(unused)]
pub struct AggregateEventReader {
test: ReaderId<TestEvent>,
other: ReaderId<OtherEvent>,
}
impl<'a> EventReader<'a> for AggregateEventReader {
type SystemData = (
<TestEventReader as EventReader<'a>>::SystemData,
<OtherEventReader as EventReader<'a>>::SystemData,
);
type Event = AggregateEvent;
fn read(&mut self, system_data: Self::SystemData, data: &mut Vec<AggregateEvent>) {
data.extend(system_data.0.read(&mut self.test).cloned().map(Into::into));
data.extend(system_data.1.read(&mut self.other).cloned().map(Into::into));
}
}
}