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
use abomonation::Abomonation;
#[derive(Debug, Clone)]
pub enum Event<T, D> {
Progress(Vec<(T, i64)>),
Messages(T, Vec<D>),
}
impl<T: Abomonation, D: Abomonation> Abomonation for Event<T,D> {
#[inline] unsafe fn entomb<W: ::std::io::Write>(&self, write: &mut W) -> ::std::io::Result<()> {
match *self {
Event::Progress(ref vec) => { vec.entomb(write)?; },
Event::Messages(ref time, ref data) => { time.entomb(write)?; data.entomb(write)?; },
}
Ok(())
}
#[inline] unsafe fn exhume<'a, 'b>(&'a mut self, mut bytes: &'b mut[u8]) -> Option<&'b mut [u8]> {
match *self {
Event::Progress(ref mut vec) => { vec.exhume(bytes) },
Event::Messages(ref mut time, ref mut data) => {
let temp = bytes; bytes = if let Some(bytes) = time.exhume(temp) { bytes } else { return None; };
let temp = bytes; bytes = if let Some(bytes) = data.exhume(temp) { bytes } else { return None; };
Some(bytes)
}
}
}
}
pub trait EventIterator<T, D> {
fn next(&mut self) -> Option<&Event<T, D>>;
}
pub trait EventPusher<T, D> {
fn push(&mut self, event: Event<T, D>);
}
impl<T, D> EventPusher<T, D> for ::std::sync::mpsc::Sender<Event<T, D>> {
fn push(&mut self, event: Event<T, D>) {
self.send(event).unwrap();
}
}
pub mod link {
use std::rc::Rc;
use std::cell::RefCell;
use super::{Event, EventPusher, EventIterator};
pub struct EventLink<T, D> {
pub event: Option<Event<T, D>>,
pub next: RefCell<Option<Rc<EventLink<T, D>>>>,
}
impl<T, D> EventLink<T, D> {
pub fn new() -> EventLink<T, D> {
EventLink { event: None, next: RefCell::new(None) }
}
}
impl<T, D> EventPusher<T, D> for Rc<EventLink<T, D>> {
fn push(&mut self, event: Event<T, D>) {
*self.next.borrow_mut() = Some(Rc::new(EventLink { event: Some(event), next: RefCell::new(None) }));
let next = self.next.borrow().as_ref().unwrap().clone();
*self = next;
}
}
impl<T, D> EventIterator<T, D> for Rc<EventLink<T, D>> {
fn next(&mut self) -> Option<&Event<T, D>> {
let is_some = self.next.borrow().is_some();
if is_some {
let next = self.next.borrow().as_ref().unwrap().clone();
*self = next;
self.event.as_ref()
}
else {
None
}
}
}
}
pub mod binary {
use std::io::Write;
use abomonation::Abomonation;
use super::{Event, EventPusher, EventIterator};
pub struct EventWriter<T, D, W: ::std::io::Write> {
stream: W,
phant: ::std::marker::PhantomData<(T,D)>,
}
impl<T, D, W: ::std::io::Write> EventWriter<T, D, W> {
pub fn new(w: W) -> EventWriter<T, D, W> {
EventWriter {
stream: w,
phant: ::std::marker::PhantomData,
}
}
}
impl<T: Abomonation, D: Abomonation, W: ::std::io::Write> EventPusher<T, D> for EventWriter<T, D, W> {
fn push(&mut self, event: Event<T, D>) {
unsafe { ::abomonation::encode(&event, &mut self.stream).unwrap(); }
}
}
pub struct EventReader<T, D, R: ::std::io::Read> {
reader: R,
bytes: Vec<u8>,
buff1: Vec<u8>,
buff2: Vec<u8>,
consumed: usize,
valid: usize,
phant: ::std::marker::PhantomData<(T,D)>,
}
impl<T, D, R: ::std::io::Read> EventReader<T, D, R> {
pub fn new(r: R) -> EventReader<T, D, R> {
EventReader {
reader: r,
bytes: vec![0u8; 1 << 20],
buff1: vec![],
buff2: vec![],
consumed: 0,
valid: 0,
phant: ::std::marker::PhantomData,
}
}
}
impl<T: Abomonation, D: Abomonation, R: ::std::io::Read> EventIterator<T, D> for EventReader<T, D, R> {
fn next(&mut self) -> Option<&Event<T, D>> {
if unsafe { ::abomonation::decode::<Event<T,D>>(&mut self.buff1[self.consumed..]) }.is_some() {
let (item, rest) = unsafe { ::abomonation::decode::<Event<T,D>>(&mut self.buff1[self.consumed..]) }.unwrap();
self.consumed = self.valid - rest.len();
return Some(item);
}
if self.consumed > 0 {
self.buff2.clear();
self.buff2.write_all(&self.buff1[self.consumed..]).unwrap();
::std::mem::swap(&mut self.buff1, &mut self.buff2);
self.valid = self.buff1.len();
self.consumed = 0;
}
if let Ok(len) = self.reader.read(&mut self.bytes[..]) {
self.buff1.write_all(&self.bytes[..len]).unwrap();
self.valid = self.buff1.len();
}
None
}
}
}