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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#![allow(raw_pointer_derive)]
#![crate_name="capnp"]
#![crate_type = "lib"]
extern crate byteorder;
#[cfg(test)]
extern crate quickcheck;
pub mod any_pointer;
pub mod capability;
pub mod data;
pub mod data_list;
pub mod enum_list;
pub mod list_list;
pub mod message;
pub mod primitive_list;
pub mod private;
pub mod serialize;
pub mod serialize_packed;
pub mod struct_list;
pub mod text;
pub mod text_list;
pub mod traits;
mod util;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct Word(u64);
impl Word {
pub fn allocate_zeroed_vec(length: usize) -> Vec<Word> {
let mut result : Vec<Word> = Vec::with_capacity(length);
unsafe {
result.set_len(length);
let p : *mut u8 = result.as_mut_ptr() as *mut u8;
::std::ptr::write_bytes(p, 0u8, length * ::std::mem::size_of::<Word>());
}
result
}
pub fn bytes_to_words<'a>(bytes: &'a [u8]) -> &'a [Word] {
unsafe {
::std::slice::from_raw_parts(bytes.as_ptr() as *const Word, bytes.len() / 8)
}
}
pub fn words_to_bytes<'a>(words: &'a [Word]) -> &'a [u8] {
unsafe {
::std::slice::from_raw_parts(words.as_ptr() as *const u8, words.len() * 8)
}
}
pub fn words_to_bytes_mut<'a>(words: &'a mut [Word]) -> &'a mut [u8] {
unsafe {
::std::slice::from_raw_parts_mut(words.as_mut_ptr() as *mut u8, words.len() * 8)
}
}
#[cfg(test)]
pub fn from(n: u64) -> Word {
Word(n)
}
}
#[cfg(test)]
impl quickcheck::Arbitrary for Word {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Word {
Word(quickcheck::Arbitrary::arbitrary(g))
}
fn shrink(&self) -> Box<Iterator<Item=Word>+'static> {
Box::new(quickcheck::Arbitrary::shrink(&self.0).map(|value| Word(value)))
}
}
#[derive(Clone, Copy, PartialEq)]
pub struct MessageSize {
pub word_count : u64,
pub cap_count : u32
}
impl MessageSize {
pub fn plus_eq(&mut self, other : MessageSize) {
self.word_count += other.word_count;
self.cap_count += other.cap_count;
}
}
#[derive(PartialEq, Clone, Copy, Debug)]
pub struct NotInSchema(pub u16);
impl ::std::fmt::Display for NotInSchema {
fn fmt(&self, fmt : &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
write!(fmt, "Enum value or union discriminant {} was not present in the schema.", self.0)
}
}
impl ::std::error::Error for NotInSchema {
fn description(&self) -> &str {
"Enum value or union disriminant was not present in schema."
}
}
pub type Result<T> = ::std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Decode { description : &'static str,
detail : Option<String> },
Io(std::io::Error),
}
impl Error {
pub fn new_decode_error(description : &'static str, detail : Option<String>) -> Error {
Error::Decode { description : description, detail : detail}
}
}
impl ::std::convert::From<::std::io::Error> for Error {
fn from(err : ::std::io::Error) -> Error {
Error::Io(err)
}
}
impl ::std::convert::From<NotInSchema> for Error {
fn from(e : NotInSchema) -> Error {
Error::new_decode_error("Enum value or union discriminant was not present in schema.",
Some(format!("value : {}", e.0)))
}
}
impl ::std::fmt::Display for Error {
fn fmt(&self, fmt : &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
match *self {
Error::Decode { ref description, detail : Some(ref detail) } => {
write!(fmt, "{} {}", description, detail)
},
Error::Decode { ref description, .. } => write!(fmt, "{}", description),
Error::Io(ref io) => io.fmt(fmt),
}
}
}
impl ::std::error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::Decode { ref description, .. } => description,
Error::Io(ref io) => ::std::error::Error::description(io),
}
}
fn cause(&self) -> Option<&::std::error::Error> {
match *self {
Error::Decode { .. } => None,
Error::Io(ref io) => io.cause(),
}
}
}
pub enum OutputSegments<'a> {
#[doc(hidden)]
SingleSegment([&'a [Word]; 1]),
#[doc(hidden)]
MultiSegment(Vec<&'a [Word]>),
}
impl <'a> ::std::ops::Deref for OutputSegments<'a> {
type Target = [&'a [Word]];
fn deref<'b>(&'b self) -> &'b [&'a [Word]] {
match self {
&OutputSegments::SingleSegment(ref s) => {
s
}
&OutputSegments::MultiSegment(ref v) => {
&*v
}
}
}
}