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
use timely_communication::{Serialize, Push};
use std::ops::{Deref, DerefMut};
use abomonation::{Abomonation, encode, decode, measure};
#[derive(Clone)]
pub struct Message<T, D> {
pub time: T,
pub data: Content<D>,
pub from: usize,
pub seq: usize,
}
impl<T, D> Message<T, D> {
#[inline]
pub fn new(time: T, data: Content<D>, from: usize, seq: usize) -> Message<T, D> {
Message {
time: time,
data: data,
from: from,
seq: seq,
}
}
}
impl<T: Abomonation+Clone, D: Abomonation> Serialize for Message<T, D> {
#[inline]
fn into_bytes(&mut self, bytes: &mut Vec<u8>) {
let bytes_needed = measure(&self.time) + measure(&self.from) + measure(&self.seq) + measure(self.data.deref());
bytes.reserve(bytes_needed);
unsafe { encode(&self.time, bytes).unwrap(); }
unsafe { encode(&self.from, bytes).unwrap(); }
unsafe { encode(&self.seq, bytes).unwrap(); }
let vec: &Vec<D> = self.data.deref();
unsafe { encode(vec, bytes).unwrap(); }
}
#[inline]
fn from_bytes(bytes: &mut Vec<u8>) -> Self {
let mut bytes = ::std::mem::replace(bytes, Vec::new());
let x_len = bytes.len();
let (time, from, seq, offset) = {
let (t,r) = unsafe { decode::<T>(&mut bytes) }.unwrap();
let (&f,r) = unsafe { decode::<usize>(r) }.unwrap();
let (&s,r) = unsafe { decode::<usize>(r) }.unwrap();
let o = x_len - r.len();
((*t).clone(), f, s, o)
};
let length = unsafe { decode::<Vec<D>>(&mut bytes[offset..]) }.unwrap().0.len();
Message::new(time, Content::Bytes(bytes, offset, length), from, seq)
}
}
#[derive(Clone)]
pub enum Content<D> {
Bytes(Vec<u8>, usize, usize),
Typed(Vec<D>),
}
impl<D> Content<D> {
pub fn take(&mut self) -> Content<D> {
::std::mem::replace(self, Content::Typed(Vec::new()))
}
#[inline]
pub fn default_length() -> usize { 1024 }
#[inline]
pub fn len(&self) -> usize {
match *self {
Content::Bytes(_, _, length) => length,
Content::Typed(ref data) => data.len(),
}
}
#[inline]
pub fn from_typed(typed: &mut Vec<D>) -> Content<D> {
Content::Typed(::std::mem::replace(typed, Vec::new()))
}
#[inline]
pub fn into_typed(self) -> Vec<D> {
match self {
Content::Bytes(_,_,_) => Vec::new(),
Content::Typed(mut data) => { data.clear(); data },
}
}
#[inline(always)]
pub fn push_at<T, P: Push<(T, Content<D>)>>(buffer: &mut Vec<D>, time: T, pusher: &mut P) {
let data = Content::from_typed(buffer);
let mut message = Some((time, data));
pusher.push(&mut message);
if let Some((_, Content::Typed(mut typed))) = message {
typed.clear();
*buffer = typed;
}
else {
*buffer = Vec::with_capacity(Content::<D>::default_length());
}
if buffer.capacity() != Content::<D>::default_length() {
*buffer = Vec::with_capacity(Content::<D>::default_length());
}
}
}
impl<D: Clone+Abomonation> Content<D> {
#[inline]
pub fn replace_with(&mut self, other: Vec<D>) -> Vec<D> {
::std::mem::replace(self.deref_mut(), other)
}
}
impl<D: Abomonation> Deref for Content<D> {
type Target = Vec<D>;
#[inline]
fn deref(&self) -> &Vec<D> {
match *self {
Content::Bytes(ref bytes, offset, _length) => {
unsafe { ::std::mem::transmute(bytes.get_unchecked(offset)) }
},
Content::Typed(ref data) => data,
}
}
}
impl<D: Clone+Abomonation> DerefMut for Content<D> {
#[inline]
fn deref_mut(&mut self) -> &mut Vec<D> {
let value = if let Content::Bytes(ref mut bytes, offset, _length) = *self {
let data: &Vec<D> = unsafe { ::std::mem::transmute(bytes.get_unchecked(offset)) };
Some(Content::Typed((*data).clone()))
}
else { None };
if let Some(contents) = value {
*self = contents;
}
if let Content::Typed(ref mut data) = *self {
data
}
else { unreachable!() }
}
}