2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2012-09-19 18:52:32 -05:00
|
|
|
/*!
|
|
|
|
|
|
|
|
The `ToBytes` and `IterBytes` traits
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use io;
|
2012-09-04 13:12:17 -05:00
|
|
|
use io::Writer;
|
2013-01-08 21:37:25 -06:00
|
|
|
use option::{None, Option, Some};
|
2012-12-23 16:41:37 -06:00
|
|
|
use str;
|
2012-08-30 13:10:01 -05:00
|
|
|
|
2013-03-25 15:21:04 -05:00
|
|
|
pub type Cb<'self> = &'self fn(buf: &const [u8]) -> bool;
|
2012-08-30 13:10:01 -05:00
|
|
|
|
2012-09-12 19:07:46 -05:00
|
|
|
/**
|
|
|
|
* A trait to implement in order to make a type hashable;
|
|
|
|
* This works in combination with the trait `Hash::Hash`, and
|
|
|
|
* may in the future be merged with that trait or otherwise
|
|
|
|
* modified when default methods and trait inheritence are
|
|
|
|
* completed.
|
|
|
|
*/
|
2012-09-28 19:41:45 -05:00
|
|
|
pub trait IterBytes {
|
2012-09-12 19:07:46 -05:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb);
|
2012-11-28 13:36:04 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl IterBytes for bool {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, _lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
f([
|
|
|
|
*self as u8
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl IterBytes for u8 {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, _lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
f([
|
|
|
|
*self
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl IterBytes for u16 {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
if lsb0 {
|
|
|
|
f([
|
|
|
|
*self as u8,
|
|
|
|
(*self >> 8) as u8
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
f([
|
|
|
|
(*self >> 8) as u8,
|
|
|
|
*self as u8
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl IterBytes for u32 {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
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
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl IterBytes for u64 {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
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
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl IterBytes for i8 {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
(*self as u8).iter_bytes(lsb0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl IterBytes for i16 {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
(*self as u16).iter_bytes(lsb0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl IterBytes for i32 {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
(*self as u32).iter_bytes(lsb0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl IterBytes for i64 {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
(*self as u64).iter_bytes(lsb0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl IterBytes for char {
|
2012-08-30 13:10:01 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
(*self as u32).iter_bytes(lsb0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_word_size = "32")]
|
|
|
|
pub mod x32 {
|
2013-01-09 11:26:40 -06:00
|
|
|
use to_bytes::{Cb, IterBytes};
|
|
|
|
|
2013-02-26 19:12:00 -06:00
|
|
|
impl IterBytes for uint {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
(*self as u32).iter_bytes(lsb0, f)
|
|
|
|
}
|
2012-09-07 19:24:02 -05:00
|
|
|
}
|
2012-08-30 13:10:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_word_size = "64")]
|
2012-11-28 13:36:04 -06:00
|
|
|
pub mod x64 {
|
2013-01-08 21:37:25 -06:00
|
|
|
use to_bytes::{Cb, IterBytes};
|
|
|
|
|
2013-02-26 19:12:00 -06:00
|
|
|
impl IterBytes for uint {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
(*self as u64).iter_bytes(lsb0, f)
|
|
|
|
}
|
2012-09-07 19:24:02 -05:00
|
|
|
}
|
2012-08-30 13:10:01 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl IterBytes for int {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
(*self as uint).iter_bytes(lsb0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-25 15:21:04 -05:00
|
|
|
impl<'self,A:IterBytes> IterBytes for &'self [A] {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
for (*self).each |elt| {
|
|
|
|
do elt.iter_bytes(lsb0) |bytes| {
|
|
|
|
f(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<A:IterBytes,B:IterBytes> IterBytes for (A,B) {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-12-05 13:58:35 -06:00
|
|
|
match *self {
|
2012-11-28 21:55:51 -06:00
|
|
|
(ref a, ref b) => {
|
2012-12-02 19:01:28 -06:00
|
|
|
iter_bytes_2(a, b, lsb0, f);
|
2012-11-28 21:55:51 -06:00
|
|
|
}
|
|
|
|
}
|
2012-11-28 13:36:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<A:IterBytes,B:IterBytes,C:IterBytes> IterBytes for (A,B,C) {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-12-05 13:58:35 -06:00
|
|
|
match *self {
|
2012-11-28 21:55:51 -06:00
|
|
|
(ref a, ref b, ref c) => {
|
2012-12-02 19:01:28 -06:00
|
|
|
iter_bytes_3(a, b, c, lsb0, f);
|
2012-11-28 21:55:51 -06:00
|
|
|
}
|
|
|
|
}
|
2012-11-28 13:36:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 19:24:02 -05:00
|
|
|
// Move this to vec, probably.
|
2013-03-25 15:21:04 -05:00
|
|
|
fn borrow<'x,A>(a: &'x [A]) -> &'x [A] {
|
2012-09-07 19:24:02 -05:00
|
|
|
a
|
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<A:IterBytes> IterBytes for ~[A] {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
borrow(*self).iter_bytes(lsb0, f)
|
|
|
|
}
|
|
|
|
}
|
2012-09-07 19:24:02 -05:00
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<A:IterBytes> IterBytes for @[A] {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
borrow(*self).iter_bytes(lsb0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn iter_bytes_2<A:IterBytes,B:IterBytes>(a: &A, b: &B,
|
2012-09-07 19:24:02 -05:00
|
|
|
lsb0: bool, z: Cb) {
|
|
|
|
let mut flag = true;
|
|
|
|
a.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
b.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn iter_bytes_3<A: IterBytes,
|
2012-09-07 19:24:02 -05:00
|
|
|
B: IterBytes,
|
|
|
|
C: IterBytes>(a: &A, b: &B, c: &C,
|
|
|
|
lsb0: bool, z: Cb) {
|
|
|
|
let mut flag = true;
|
|
|
|
a.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
b.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
c.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn iter_bytes_4<A: IterBytes,
|
2012-09-07 19:24:02 -05:00
|
|
|
B: IterBytes,
|
|
|
|
C: IterBytes,
|
|
|
|
D: IterBytes>(a: &A, b: &B, c: &C,
|
|
|
|
d: &D,
|
|
|
|
lsb0: bool, z: Cb) {
|
2012-08-30 13:10:01 -05:00
|
|
|
let mut flag = true;
|
2012-09-07 19:24:02 -05:00
|
|
|
a.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
2012-08-30 13:10:01 -05:00
|
|
|
if !flag { return; }
|
2012-09-07 19:24:02 -05:00
|
|
|
b.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
c.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
d.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
2012-08-30 13:10:01 -05:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn iter_bytes_5<A: IterBytes,
|
2012-09-07 19:24:02 -05:00
|
|
|
B: IterBytes,
|
|
|
|
C: IterBytes,
|
|
|
|
D: IterBytes,
|
|
|
|
E: IterBytes>(a: &A, b: &B, c: &C,
|
|
|
|
d: &D, e: &E,
|
|
|
|
lsb0: bool, z: Cb) {
|
2012-08-30 13:10:01 -05:00
|
|
|
let mut flag = true;
|
2012-09-07 19:24:02 -05:00
|
|
|
a.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
b.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
c.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
2012-08-30 13:10:01 -05:00
|
|
|
if !flag { return; }
|
2012-09-07 19:24:02 -05:00
|
|
|
d.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
e.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
2012-08-30 13:10:01 -05:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn iter_bytes_6<A: IterBytes,
|
2012-09-07 19:24:02 -05:00
|
|
|
B: IterBytes,
|
|
|
|
C: IterBytes,
|
|
|
|
D: IterBytes,
|
|
|
|
E: IterBytes,
|
|
|
|
F: IterBytes>(a: &A, b: &B, c: &C,
|
|
|
|
d: &D, e: &E, f: &F,
|
|
|
|
lsb0: bool, z: Cb) {
|
2012-08-30 13:10:01 -05:00
|
|
|
let mut flag = true;
|
2012-09-07 19:24:02 -05:00
|
|
|
a.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
b.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
c.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
2012-08-30 13:10:01 -05:00
|
|
|
if !flag { return; }
|
2012-09-07 19:24:02 -05:00
|
|
|
d.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
2012-08-30 13:10:01 -05:00
|
|
|
if !flag { return; }
|
2012-09-07 19:24:02 -05:00
|
|
|
e.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
f.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
2012-08-30 13:10:01 -05:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn iter_bytes_7<A: IterBytes,
|
2012-09-07 19:24:02 -05:00
|
|
|
B: IterBytes,
|
|
|
|
C: IterBytes,
|
|
|
|
D: IterBytes,
|
|
|
|
E: IterBytes,
|
|
|
|
F: IterBytes,
|
|
|
|
G: IterBytes>(a: &A, b: &B, c: &C,
|
|
|
|
d: &D, e: &E, f: &F,
|
|
|
|
g: &G,
|
|
|
|
lsb0: bool, z: Cb) {
|
2012-08-30 13:10:01 -05:00
|
|
|
let mut flag = true;
|
2012-09-07 19:24:02 -05:00
|
|
|
a.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
b.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
c.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
d.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
2012-08-30 13:10:01 -05:00
|
|
|
if !flag { return; }
|
2012-09-07 19:24:02 -05:00
|
|
|
e.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
2012-08-30 13:10:01 -05:00
|
|
|
if !flag { return; }
|
2012-09-07 19:24:02 -05:00
|
|
|
f.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
|
|
|
if !flag { return; }
|
|
|
|
g.iter_bytes(lsb0, |bytes| {flag = z(bytes); flag});
|
2012-08-30 13:10:01 -05:00
|
|
|
}
|
|
|
|
|
2013-03-25 15:21:04 -05:00
|
|
|
impl<'self> IterBytes for &'self str {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, _lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
do str::byte_slice(*self) |bytes| {
|
|
|
|
f(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl IterBytes for ~str {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, _lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
do str::byte_slice(*self) |bytes| {
|
|
|
|
f(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl IterBytes for @str {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, _lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
do str::byte_slice(*self) |bytes| {
|
|
|
|
f(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<A:IterBytes> IterBytes for Option<A> {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
match *self {
|
|
|
|
Some(ref a) => iter_bytes_2(&0u8, a, lsb0, f),
|
|
|
|
None => 1u8.iter_bytes(lsb0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-25 15:21:04 -05:00
|
|
|
impl<'self,A:IterBytes> IterBytes for &'self A {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
(**self).iter_bytes(lsb0, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<A:IterBytes> IterBytes for @A {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
(**self).iter_bytes(lsb0, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<A:IterBytes> IterBytes for ~A {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
(**self).iter_bytes(lsb0, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: raw-pointer IterBytes does _not_ dereference
|
|
|
|
// to the target; it just gives you the pointer-bytes.
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<A> IterBytes for *const A {
|
2012-11-28 13:36:04 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: Cb) {
|
2012-11-28 13:36:04 -06:00
|
|
|
(*self as uint).iter_bytes(lsb0, f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-30 13:10:01 -05:00
|
|
|
|
|
|
|
trait ToBytes {
|
2012-11-28 13:36:04 -06:00
|
|
|
fn to_bytes(&self, lsb0: bool) -> ~[u8];
|
2012-08-30 13:10:01 -05:00
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<A:IterBytes> ToBytes for A {
|
2012-11-28 13:36:04 -06:00
|
|
|
fn to_bytes(&self, lsb0: bool) -> ~[u8] {
|
2012-09-14 11:40:28 -05:00
|
|
|
do io::with_bytes_writer |wr| {
|
|
|
|
for self.iter_bytes(lsb0) |bytes| {
|
|
|
|
wr.write(bytes)
|
|
|
|
}
|
2012-08-30 13:10:01 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-03 23:30:09 -05:00
|
|
|
}
|