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
#![deny(missing_docs)]
pub use storage::RBError as EventError;
pub use storage::ReadData as EventReadData;
pub use storage::ReaderId;
pub use storage::StorageIterator as EventIterator;
use std::any::TypeId;
use storage::RingBufferStorage;
mod storage;
pub trait Event: Send + Sync + 'static {}
impl<T> Event for T
where
T: Send + Sync + 'static,
{
}
const DEFAULT_MAX_SIZE: usize = 200;
pub struct EventChannel<E> {
storage: RingBufferStorage<E>,
}
impl<E> EventChannel<E>
where
E: Event,
{
pub fn new() -> Self {
Self::with_capacity(DEFAULT_MAX_SIZE)
}
pub fn with_capacity(size: usize) -> Self {
Self {
storage: RingBufferStorage::new(size),
}
}
pub fn register_reader(&self) -> ReaderId {
self.storage.new_reader_id()
}
pub fn slice_write(&mut self, events: &[E]) -> Result<(), EventError>
where
E: Clone,
{
self.storage.slice_write(events)
}
pub fn drain_vec_write(&mut self, events: &mut Vec<E>) -> Result<(), EventError> {
self.storage.drain_vec_write(events)
}
pub fn single_write(&mut self, event: E) {
self.storage.single_write(event);
}
pub fn read(&self, reader_id: &mut ReaderId) -> Result<EventReadData<E>, EventError> {
self.storage.read(reader_id)
}
pub fn lossy_read(&self, reader_id: &mut ReaderId) -> Result<EventIterator<E>, EventError> {
self.read(reader_id).map(|read_data| match read_data {
EventReadData::Data(data) => data,
EventReadData::Overflow(data, overflow) => {
eprintln!(
"EventChannel/{:?} overflowed, {} events missed.",
TypeId::of::<E>(),
overflow
);
data
}
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::any::TypeId;
#[derive(Debug, Clone, PartialEq)]
struct Test {
pub id: u32,
}
#[test]
fn test_register_reader() {
let channel = EventChannel::<Test>::with_capacity(14);
let reader_id = channel.register_reader();
assert_eq!(ReaderId::new(TypeId::of::<Test>(), 0, 0), reader_id);
}
#[test]
fn test_read_write() {
let mut channel = EventChannel::with_capacity(14);
let mut reader_id = channel.register_reader();
let mut reader_id_extra = channel.register_reader();
channel.single_write(Test { id: 1 });
match channel.read(&mut reader_id) {
Ok(EventReadData::Data(data)) => {
assert_eq!(vec![Test { id: 1 }], data.cloned().collect::<Vec<_>>())
}
_ => panic!(),
}
channel.single_write(Test { id: 2 });
match channel.read(&mut reader_id) {
Ok(EventReadData::Data(data)) => {
assert_eq!(vec![Test { id: 2 }], data.cloned().collect::<Vec<_>>())
}
_ => panic!(),
}
match channel.read(&mut reader_id_extra) {
Ok(EventReadData::Data(data)) => assert_eq!(
vec![Test { id: 1 }, Test { id: 2 }],
data.cloned().collect::<Vec<_>>()
),
_ => panic!(),
}
channel.single_write(Test { id: 3 });
match channel.read(&mut reader_id) {
Ok(EventReadData::Data(data)) => {
assert_eq!(vec![Test { id: 3 }], data.cloned().collect::<Vec<_>>())
}
_ => panic!(),
}
match channel.read(&mut reader_id_extra) {
Ok(EventReadData::Data(data)) => {
assert_eq!(vec![Test { id: 3 }], data.cloned().collect::<Vec<_>>())
}
_ => panic!(),
}
}
}