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};

/// Read events generically
pub trait EventReader<'a> {
    /// SystemData needed to read the event(s)
    type SystemData: SystemData<'a>;
    /// The event type produced by the reader
    type Event: Clone + Send + Sync + 'static;

    /// Read events from the linked `SystemData` and append to the given Vec
    fn read(&mut self, _: Self::SystemData, _: &mut Vec<Self::Event>);

    /// Read events from `World` and append to the given `Vec`
    fn read_from_world(&mut self, world: &'a World, events: &mut Vec<Self::Event>) {
        self.read(world.system_data(), events);
    }

    /// Setup event reader
    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));
        }
    }
}