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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*!
The `ToBytes` and `IterBytes` traits for converting to raw bytes
*/
use cast;
use container::Container;
use iter::Iterator;
use option::{None, Option, Some};
use rc::Rc;
use str::{Str, StrSlice};
use vec::{Vector, ImmutableVector};
use vec_ng::Vec;
pub type Cb<'a> = 'a |buf: &[u8]| -> bool;
///
/// A trait to implement in order to make a type hashable;
/// This works in combination with the trait `std::hash::Hash`, and
/// may in the future be merged with that trait or otherwise
/// modified when default methods and trait inheritance are
/// completed.
///
/// IterBytes should be implemented so that the extent of the
/// produced byte stream can be discovered, given the original
/// type.
/// For example, the IterBytes implementation for vectors emits
/// its length first, and enums should emit their discriminant.
///
pub trait IterBytes {
/// Call the provided callback `f` one or more times with
/// byte-slices that should be used when computing a hash
/// value or otherwise "flattening" the structure into
/// a sequence of bytes. The `lsb0` parameter conveys
/// whether the caller is asking for little-endian bytes
/// (`true`) or big-endian (`false`); this should only be
/// relevant in implementations that represent a single
/// multi-byte datum such as a 32 bit integer or 64 bit
/// floating-point value. It can be safely ignored for
/// larger structured types as they are usually processed
/// left-to-right in declaration order, regardless of
/// underlying memory endianness.
///
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool;
}
impl IterBytes for bool {
#[inline]
fn iter_bytes(&self, _lsb0: bool, f: Cb) -> bool {
f([
*self as u8
])
}
}
impl IterBytes for u8 {
#[inline]
fn iter_bytes(&self, _lsb0: bool, f: Cb) -> bool {
f([
*self
])
}
}
impl IterBytes for u16 {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
if lsb0 {
f([
*self as u8,
(*self >> 8) as u8
])
} else {
f([
(*self >> 8) as u8,
*self as u8
])
}
}
}
impl IterBytes for u32 {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
if lsb0 {
f([
*self as u8,
(*self >> 8) as u8,
(*self >> 16) as u8,
(*self >> 24) as u8,
])
} else {
f([
(*self >> 24) as u8,
(*self >> 16) as u8,
(*self >> 8) as u8,
*self as u8
])
}
}
}
impl IterBytes for u64 {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
if lsb0 {
f([
*self as u8,
(*self >> 8) as u8,
(*self >> 16) as u8,
(*self >> 24) as u8,
(*self >> 32) as u8,
(*self >> 40) as u8,
(*self >> 48) as u8,
(*self >> 56) as u8
])
} else {
f([
(*self >> 56) as u8,
(*self >> 48) as u8,
(*self >> 40) as u8,
(*self >> 32) as u8,
(*self >> 24) as u8,
(*self >> 16) as u8,
(*self >> 8) as u8,
*self as u8
])
}
}
}
impl IterBytes for i8 {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
(*self as u8).iter_bytes(lsb0, f)
}
}
impl IterBytes for i16 {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
(*self as u16).iter_bytes(lsb0, f)
}
}
impl IterBytes for i32 {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
(*self as u32).iter_bytes(lsb0, f)
}
}
impl IterBytes for i64 {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
(*self as u64).iter_bytes(lsb0, f)
}
}
impl IterBytes for char {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
(*self as u32).iter_bytes(lsb0, f)
}
}
#[cfg(target_word_size = "32")]
impl IterBytes for uint {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
(*self as u32).iter_bytes(lsb0, f)
}
}
#[cfg(target_word_size = "64")]
impl IterBytes for uint {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
(*self as u64).iter_bytes(lsb0, f)
}
}
impl IterBytes for int {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
(*self as uint).iter_bytes(lsb0, f)
}
}
impl IterBytes for f32 {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
let i: u32 = unsafe {
// 0.0 == -0.0 so they should also have the same hashcode
cast::transmute(if *self == -0.0 { 0.0 } else { *self })
};
i.iter_bytes(lsb0, f)
}
}
impl IterBytes for f64 {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
let i: u64 = unsafe {
// 0.0 == -0.0 so they should also have the same hashcode
cast::transmute(if *self == -0.0 { 0.0 } else { *self })
};
i.iter_bytes(lsb0, f)
}
}
impl<'a,A:IterBytes> IterBytes for &'a [A] {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
self.len().iter_bytes(lsb0, |b| f(b)) &&
self.iter().advance(|elt| elt.iter_bytes(lsb0, |b| f(b)))
}
}
impl<A: IterBytes> IterBytes for (A, ) {
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
match *self {
(ref a, ) => a.iter_bytes(lsb0, |b| f(b))
}
}
}
macro_rules! iter_bytes_tuple(
($($A:ident),+) => (
impl<$($A: IterBytes),+> IterBytes for ($($A),+) {
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
match *self {
($(ref $A),+) => {
$(
$A .iter_bytes(lsb0, |b| f(b))
)&&+
}
}
}
}
)
)
iter_bytes_tuple!(A, B)
iter_bytes_tuple!(A, B, C)
iter_bytes_tuple!(A, B, C, D)
iter_bytes_tuple!(A, B, C, D, E)
iter_bytes_tuple!(A, B, C, D, E, F)
iter_bytes_tuple!(A, B, C, D, E, F, G)
iter_bytes_tuple!(A, B, C, D, E, F, G, H)
impl<A:IterBytes> IterBytes for ~[A] {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
self.as_slice().iter_bytes(lsb0, f)
}
}
impl<A:IterBytes> IterBytes for Vec<A> {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
self.as_slice().iter_bytes(lsb0, f)
}
}
impl<'a> IterBytes for &'a str {
#[inline]
fn iter_bytes(&self, _lsb0: bool, f: Cb) -> bool {
// Terminate the string with a byte that does not appear in UTF-8
f(self.as_bytes()) && f([0xFF])
}
}
impl IterBytes for ~str {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
self.as_slice().iter_bytes(lsb0, f)
}
}
impl<A:IterBytes> IterBytes for Option<A> {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
match *self {
Some(ref a) => 0u8.iter_bytes(lsb0, |b| f(b)) && a.iter_bytes(lsb0, |b| f(b)),
None => 1u8.iter_bytes(lsb0, f)
}
}
}
impl<'a,A:IterBytes> IterBytes for &'a A {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
(**self).iter_bytes(lsb0, f)
}
}
impl<A:IterBytes> IterBytes for @A {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
(**self).iter_bytes(lsb0, f)
}
}
impl<A:IterBytes> IterBytes for Rc<A> {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
self.borrow().iter_bytes(lsb0, f)
}
}
impl<A:IterBytes> IterBytes for ~A {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
(**self).iter_bytes(lsb0, f)
}
}
// NB: raw-pointer IterBytes does _not_ dereference
// to the target; it just gives you the pointer-bytes.
impl<A> IterBytes for *A {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
(*self as uint).iter_bytes(lsb0, f)
}
}
impl<A> IterBytes for *mut A {
#[inline]
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
(*self as uint).iter_bytes(lsb0, f)
}
}
/// A trait for converting a value to a list of bytes.
pub trait ToBytes {
/// Converts the current value to a list of bytes. This is equivalent to
/// invoking iter_bytes on a type and collecting all yielded values in an
/// array
fn to_bytes(&self, lsb0: bool) -> ~[u8];
}
impl<A:IterBytes> ToBytes for A {
fn to_bytes(&self, lsb0: bool) -> ~[u8] {
use io::Writer;
let mut m = ::io::MemWriter::new();
self.iter_bytes(lsb0, |bytes| {
m.write(bytes).unwrap();
true
});
m.unwrap()
}
}
#[cfg(test)]
mod test {
use super::*;
// just test to see if it compiles:
#[test] fn iterbytes_compiles () {
takes_iterbytes((3,4,5,false));
}
fn takes_iterbytes<T : IterBytes>(_x : T) {}
}
|