rustfmt librbml

This commit is contained in:
Nick Cameron 2016-01-19 14:30:48 +13:00
parent 08886499cf
commit 7c2bfd12a0
3 changed files with 506 additions and 464 deletions

View File

@ -9,8 +9,7 @@
// except according to those terms.
#[inline]
pub fn write_to_vec(vec: &mut Vec<u8>, position: &mut usize, byte: u8)
{
pub fn write_to_vec(vec: &mut Vec<u8>, position: &mut usize, byte: u8) {
if *position == vec.len() {
vec.push(byte);
} else {
@ -20,13 +19,9 @@ pub fn write_to_vec(vec: &mut Vec<u8>, position: &mut usize, byte: u8)
*position += 1;
}
pub fn write_unsigned_leb128(out: &mut Vec<u8>,
start_position: usize,
mut value: u64)
-> usize {
pub fn write_unsigned_leb128(out: &mut Vec<u8>, start_position: usize, mut value: u64) -> usize {
let mut position = start_position;
loop
{
loop {
let mut byte = (value & 0x7F) as u8;
value >>= 7;
if value != 0 {
@ -43,9 +38,7 @@ pub fn write_unsigned_leb128(out: &mut Vec<u8>,
return position - start_position;
}
pub fn read_unsigned_leb128(data: &[u8],
start_position: usize)
-> (u64, usize) {
pub fn read_unsigned_leb128(data: &[u8], start_position: usize) -> (u64, usize) {
let mut result = 0;
let mut shift = 0;
let mut position = start_position;
@ -63,9 +56,7 @@ pub fn read_unsigned_leb128(data: &[u8],
}
pub fn write_signed_leb128(out: &mut Vec<u8>,
start_position: usize,
mut value: i64) -> usize {
pub fn write_signed_leb128(out: &mut Vec<u8>, start_position: usize, mut value: i64) -> usize {
let mut position = start_position;
loop {
@ -87,9 +78,7 @@ pub fn write_signed_leb128(out: &mut Vec<u8>,
return position - start_position;
}
pub fn read_signed_leb128(data: &[u8],
start_position: usize)
-> (i64, usize) {
pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i64, usize) {
let mut result = 0;
let mut shift = 0;
let mut position = start_position;
@ -107,7 +96,7 @@ pub fn read_signed_leb128(data: &[u8],
}
if (shift < 64) && ((byte & 0x40) != 0) {
/* sign extend */
// sign extend
result |= -(1i64 << shift);
}

View File

@ -131,9 +131,11 @@ extern crate serialize;
#[cfg(test)]
extern crate serialize as rustc_serialize; // Used by RustcEncodable
#[macro_use] extern crate log;
#[macro_use]
extern crate log;
#[cfg(test)] extern crate test;
#[cfg(test)]
extern crate test;
pub mod opaque;
pub mod leb128;
@ -154,7 +156,11 @@ pub struct Doc<'a> {
impl<'doc> Doc<'doc> {
pub fn new(data: &'doc [u8]) -> Doc<'doc> {
Doc { data: data, start: 0, end: data.len() }
Doc {
data: data,
start: 0,
end: data.len(),
}
}
pub fn get<'a>(&'a self, tag: usize) -> Doc<'a> {
@ -183,7 +189,6 @@ pub struct TaggedDoc<'a> {
pub enum EbmlEncoderTag {
// tags 00..1f are reserved for auto-serialization.
// first NUM_IMPLICIT_TAGS tags are implicitly sized and lengths are not encoded.
EsU8 = 0x00, // + 1 byte
EsU16 = 0x01, // + 2 bytes
EsU32 = 0x02, // + 4 bytes
@ -199,7 +204,6 @@ pub enum EbmlEncoderTag {
EsSub8 = 0x0c, // + 1 byte
EsSub32 = 0x0d, // + 4 bytes
// 0x0e and 0x0f are reserved
EsStr = 0x10,
EsEnum = 0x11, // encodes the variant id as the first EsSub*
EsVec = 0x12, // encodes the # of elements as the first EsSub*
@ -213,14 +217,10 @@ pub enum EbmlEncoderTag {
const NUM_TAGS: usize = 0x1000;
const NUM_IMPLICIT_TAGS: usize = 0x0e;
static TAG_IMPLICIT_LEN: [i8; NUM_IMPLICIT_TAGS] = [
1, 2, 4, 8, // EsU*
1, 2, 4, 8, // ESI*
1, // EsBool
4, // EsChar
4, 8, // EsF*
1, 4, // EsSub*
];
static TAG_IMPLICIT_LEN: [i8; NUM_IMPLICIT_TAGS] = [1, 2, 4, 8 /* EsU* */, 1, 2, 4,
8 /* ESI* */, 1 /* EsBool */,
4 /* EsChar */, 4, 8 /* EsF* */, 1,
4 /* EsSub* */];
#[derive(Debug)]
pub enum Error {
@ -228,7 +228,7 @@ pub enum Error {
InvalidTag(usize),
Expected(String),
IoError(std::io::Error),
ApplicationError(String)
ApplicationError(String),
}
impl fmt::Display for Error {
@ -248,11 +248,10 @@ pub mod reader {
use serialize;
use super::opaque;
use super::{ ApplicationError, EsVec, EsMap, EsEnum, EsSub8, EsSub32,
EsVecElt, EsMapKey, EsU64, EsU32, EsU16, EsU8, EsI64,
EsI32, EsI16, EsI8, EsBool, EsF64, EsF32, EsChar, EsStr, EsMapVal,
EsOpaque, EbmlEncoderTag, Doc, TaggedDoc,
Error, IntTooBig, InvalidTag, Expected, NUM_IMPLICIT_TAGS, TAG_IMPLICIT_LEN };
use super::{ApplicationError, EsVec, EsMap, EsEnum, EsSub8, EsSub32, EsVecElt, EsMapKey,
EsU64, EsU32, EsU16, EsU8, EsI64, EsI32, EsI16, EsI8, EsBool, EsF64, EsF32,
EsChar, EsStr, EsMapVal, EsOpaque, EbmlEncoderTag, Doc, TaggedDoc, Error,
IntTooBig, InvalidTag, Expected, NUM_IMPLICIT_TAGS, TAG_IMPLICIT_LEN};
pub type DecodeResult<T> = Result<T, Error>;
// rbml reading
@ -272,15 +271,21 @@ pub mod reader {
#[derive(Copy, Clone)]
pub struct Res {
pub val: usize,
pub next: usize
pub next: usize,
}
pub fn tag_at(data: &[u8], start: usize) -> DecodeResult<Res> {
let v = data[start] as usize;
if v < 0xf0 {
Ok(Res { val: v, next: start + 1 })
Ok(Res {
val: v,
next: start + 1,
})
} else if v > 0xf0 {
Ok(Res { val: ((v & 0xf) << 8) | data[start + 1] as usize, next: start + 2 })
Ok(Res {
val: ((v & 0xf) << 8) | data[start + 1] as usize,
next: start + 2,
})
} else {
// every tag starting with byte 0xf0 is an overlong form, which is prohibited.
Err(InvalidTag(v))
@ -291,25 +296,31 @@ pub mod reader {
fn vuint_at_slow(data: &[u8], start: usize) -> DecodeResult<Res> {
let a = data[start];
if a & 0x80 != 0 {
return Ok(Res {val: (a & 0x7f) as usize, next: start + 1});
return Ok(Res {
val: (a & 0x7f) as usize,
next: start + 1,
});
}
if a & 0x40 != 0 {
return Ok(Res {val: ((a & 0x3f) as usize) << 8 |
(data[start + 1] as usize),
next: start + 2});
return Ok(Res {
val: ((a & 0x3f) as usize) << 8 | (data[start + 1] as usize),
next: start + 2,
});
}
if a & 0x20 != 0 {
return Ok(Res {val: ((a & 0x1f) as usize) << 16 |
(data[start + 1] as usize) << 8 |
return Ok(Res {
val: ((a & 0x1f) as usize) << 16 | (data[start + 1] as usize) << 8 |
(data[start + 2] as usize),
next: start + 3});
next: start + 3,
});
}
if a & 0x10 != 0 {
return Ok(Res {val: ((a & 0x0f) as usize) << 24 |
(data[start + 1] as usize) << 16 |
return Ok(Res {
val: ((a & 0x0f) as usize) << 24 | (data[start + 1] as usize) << 16 |
(data[start + 2] as usize) << 8 |
(data[start + 3] as usize),
next: start + 4});
next: start + 4,
});
}
Err(IntTooBig(a as usize))
}
@ -338,13 +349,22 @@ pub mod reader {
// most significant bit is set etc. we can replace up to three
// "and+branch" with a single table lookup which gives us a measured
// speedup of around 2x on x86_64.
static SHIFT_MASK_TABLE: [(usize, u32); 16] = [
(0, 0x0), (0, 0x0fffffff),
(8, 0x1fffff), (8, 0x1fffff),
(16, 0x3fff), (16, 0x3fff), (16, 0x3fff), (16, 0x3fff),
(24, 0x7f), (24, 0x7f), (24, 0x7f), (24, 0x7f),
(24, 0x7f), (24, 0x7f), (24, 0x7f), (24, 0x7f)
];
static SHIFT_MASK_TABLE: [(usize, u32); 16] = [(0, 0x0),
(0, 0x0fffffff),
(8, 0x1fffff),
(8, 0x1fffff),
(16, 0x3fff),
(16, 0x3fff),
(16, 0x3fff),
(16, 0x3fff),
(24, 0x7f),
(24, 0x7f),
(24, 0x7f),
(24, 0x7f),
(24, 0x7f),
(24, 0x7f),
(24, 0x7f),
(24, 0x7f)];
unsafe {
let ptr = data.as_ptr().offset(start as isize) as *const u32;
@ -361,7 +381,10 @@ pub mod reader {
pub fn tag_len_at(data: &[u8], tag: Res) -> DecodeResult<Res> {
if tag.val < NUM_IMPLICIT_TAGS && TAG_IMPLICIT_LEN[tag.val] >= 0 {
Ok(Res { val: TAG_IMPLICIT_LEN[tag.val] as usize, next: tag.next })
Ok(Res {
val: TAG_IMPLICIT_LEN[tag.val] as usize,
next: tag.next,
})
} else {
vuint_at(data, tag.next)
}
@ -373,7 +396,11 @@ pub mod reader {
let end = elt_size.next + elt_size.val;
Ok(TaggedDoc {
tag: elt_tag.val,
doc: Doc { data: data, start: elt_size.next, end: end }
doc: Doc {
data: data,
start: elt_size.next,
end: end,
},
})
}
@ -384,8 +411,11 @@ pub mod reader {
let elt_size = try_or!(tag_len_at(d.data, elt_tag), None);
pos = elt_size.next + elt_size.val;
if elt_tag.val == tg {
return Some(Doc { data: d.data, start: elt_size.next,
end: pos });
return Some(Doc {
data: d.data,
start: elt_size.next,
end: pos,
});
}
}
None
@ -402,9 +432,7 @@ pub mod reader {
}
pub fn docs<'a>(d: Doc<'a>) -> DocsIterator<'a> {
DocsIterator {
d: d
}
DocsIterator { d: d }
}
pub struct DocsIterator<'a> {
@ -465,8 +493,8 @@ pub mod reader {
}
}
pub fn with_doc_data<T, F>(d: Doc, f: F) -> T where
F: FnOnce(&[u8]) -> T,
pub fn with_doc_data<T, F>(d: Doc, f: F) -> T
where F: FnOnce(&[u8]) -> T
{
f(&d.data[d.start..d.end])
}
@ -502,13 +530,31 @@ pub mod reader {
}
}
#[inline] pub fn doc_as_u16(d: Doc) -> u16 { doc_as_u64(d) as u16 }
#[inline] pub fn doc_as_u32(d: Doc) -> u32 { doc_as_u64(d) as u32 }
#[inline]
pub fn doc_as_u16(d: Doc) -> u16 {
doc_as_u64(d) as u16
}
#[inline]
pub fn doc_as_u32(d: Doc) -> u32 {
doc_as_u64(d) as u32
}
#[inline] pub fn doc_as_i8(d: Doc) -> i8 { doc_as_u8(d) as i8 }
#[inline] pub fn doc_as_i16(d: Doc) -> i16 { doc_as_u16(d) as i16 }
#[inline] pub fn doc_as_i32(d: Doc) -> i32 { doc_as_u32(d) as i32 }
#[inline] pub fn doc_as_i64(d: Doc) -> i64 { doc_as_u64(d) as i64 }
#[inline]
pub fn doc_as_i8(d: Doc) -> i8 {
doc_as_u8(d) as i8
}
#[inline]
pub fn doc_as_i16(d: Doc) -> i16 {
doc_as_u16(d) as i16
}
#[inline]
pub fn doc_as_i32(d: Doc) -> i32 {
doc_as_u32(d) as i32
}
#[inline]
pub fn doc_as_i64(d: Doc) -> i64 {
doc_as_u64(d) as i64
}
pub struct Decoder<'a> {
parent: Doc<'a>,
@ -519,18 +565,16 @@ pub mod reader {
pub fn new(d: Doc<'doc>) -> Decoder<'doc> {
Decoder {
parent: d,
pos: d.start
pos: d.start,
}
}
fn next_doc(&mut self, exp_tag: EbmlEncoderTag) -> DecodeResult<Doc<'doc>> {
debug!(". next_doc(exp_tag={:?})", exp_tag);
if self.pos >= self.parent.end {
return Err(Expected(format!("no more documents in \
current node!")));
return Err(Expected(format!("no more documents in current node!")));
}
let TaggedDoc { tag: r_tag, doc: r_doc } =
try!(doc_at(self.parent.data, self.pos));
let TaggedDoc { tag: r_tag, doc: r_doc } = try!(doc_at(self.parent.data, self.pos));
debug!("self.parent={:?}-{:?} self.pos={:?} r_tag={:?} r_doc={:?}-{:?}",
self.parent.start,
self.parent.end,
@ -539,20 +583,22 @@ pub mod reader {
r_doc.start,
r_doc.end);
if r_tag != (exp_tag as usize) {
return Err(Expected(format!("expected EBML doc with tag {:?} but \
found tag {:?}", exp_tag, r_tag)));
return Err(Expected(format!("expected EBML doc with tag {:?} but found tag {:?}",
exp_tag,
r_tag)));
}
if r_doc.end > self.parent.end {
return Err(Expected(format!("invalid EBML, child extends to \
{:#x}, parent to {:#x}",
r_doc.end, self.parent.end)));
return Err(Expected(format!("invalid EBML, child extends to {:#x}, parent to \
{:#x}",
r_doc.end,
self.parent.end)));
}
self.pos = r_doc.end;
Ok(r_doc)
}
fn push_doc<T, F>(&mut self, exp_tag: EbmlEncoderTag, f: F) -> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>,
fn push_doc<T, F>(&mut self, exp_tag: EbmlEncoderTag, f: F) -> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>
{
let d = try!(self.next_doc(exp_tag));
let old_parent = self.parent;
@ -571,20 +617,23 @@ pub mod reader {
return Ok(0);
}
let TaggedDoc { tag: r_tag, doc: r_doc } =
try!(doc_at(self.parent.data, self.pos));
let TaggedDoc { tag: r_tag, doc: r_doc } = try!(doc_at(self.parent.data, self.pos));
let r = if r_tag == (EsSub8 as usize) {
doc_as_u8(r_doc) as usize
} else if r_tag == (EsSub32 as usize) {
doc_as_u32(r_doc) as usize
} else {
return Err(Expected(format!("expected EBML doc with tag {:?} or {:?} but \
found tag {:?}", EsSub8, EsSub32, r_tag)));
return Err(Expected(format!("expected EBML doc with tag {:?} or {:?} but found \
tag {:?}",
EsSub8,
EsSub32,
r_tag)));
};
if r_doc.end > self.parent.end {
return Err(Expected(format!("invalid EBML, child extends to \
{:#x}, parent to {:#x}",
r_doc.end, self.parent.end)));
return Err(Expected(format!("invalid EBML, child extends to {:#x}, parent to \
{:#x}",
r_doc.end,
self.parent.end)));
}
self.pos = r_doc.end;
debug!("_next_sub result={:?}", r);
@ -597,14 +646,13 @@ pub mod reader {
// all tags between them should be valid, in the order of u8, u16, u32 and u64.
fn _next_int(&mut self,
first_tag: EbmlEncoderTag,
last_tag: EbmlEncoderTag) -> DecodeResult<u64> {
last_tag: EbmlEncoderTag)
-> DecodeResult<u64> {
if self.pos >= self.parent.end {
return Err(Expected(format!("no more documents in \
current node!")));
return Err(Expected(format!("no more documents in current node!")));
}
let TaggedDoc { tag: r_tag, doc: r_doc } =
try!(doc_at(self.parent.data, self.pos));
let TaggedDoc { tag: r_tag, doc: r_doc } = try!(doc_at(self.parent.data, self.pos));
let r = if first_tag as usize <= r_tag && r_tag <= last_tag as usize {
match r_tag - first_tag as usize {
0 => doc_as_u8(r_doc) as u64,
@ -615,26 +663,29 @@ pub mod reader {
}
} else {
return Err(Expected(format!("expected EBML doc with tag {:?} through {:?} but \
found tag {:?}", first_tag, last_tag, r_tag)));
found tag {:?}",
first_tag,
last_tag,
r_tag)));
};
if r_doc.end > self.parent.end {
return Err(Expected(format!("invalid EBML, child extends to \
{:#x}, parent to {:#x}",
r_doc.end, self.parent.end)));
return Err(Expected(format!("invalid EBML, child extends to {:#x}, parent to \
{:#x}",
r_doc.end,
self.parent.end)));
}
self.pos = r_doc.end;
debug!("_next_int({:?}, {:?}) result={:?}", first_tag, last_tag, r);
Ok(r)
}
pub fn read_opaque<R, F>(&mut self, op: F) -> DecodeResult<R> where
F: FnOnce(&mut opaque::Decoder, Doc) -> DecodeResult<R>,
pub fn read_opaque<R, F>(&mut self, op: F) -> DecodeResult<R>
where F: FnOnce(&mut opaque::Decoder, Doc) -> DecodeResult<R>
{
let doc = try!(self.next_doc(EsOpaque));
let result = {
let mut opaque_decoder = opaque::Decoder::new(doc.data,
doc.start);
let mut opaque_decoder = opaque::Decoder::new(doc.data, doc.start);
try!(op(&mut opaque_decoder, doc))
};
@ -652,12 +703,22 @@ pub mod reader {
impl<'doc> serialize::Decoder for Decoder<'doc> {
type Error = Error;
fn read_nil(&mut self) -> DecodeResult<()> { Ok(()) }
fn read_nil(&mut self) -> DecodeResult<()> {
Ok(())
}
fn read_u64(&mut self) -> DecodeResult<u64> { self._next_int(EsU8, EsU64) }
fn read_u32(&mut self) -> DecodeResult<u32> { Ok(try!(self._next_int(EsU8, EsU32)) as u32) }
fn read_u16(&mut self) -> DecodeResult<u16> { Ok(try!(self._next_int(EsU8, EsU16)) as u16) }
fn read_u8(&mut self) -> DecodeResult<u8> { Ok(doc_as_u8(try!(self.next_doc(EsU8)))) }
fn read_u64(&mut self) -> DecodeResult<u64> {
self._next_int(EsU8, EsU64)
}
fn read_u32(&mut self) -> DecodeResult<u32> {
Ok(try!(self._next_int(EsU8, EsU32)) as u32)
}
fn read_u16(&mut self) -> DecodeResult<u16> {
Ok(try!(self._next_int(EsU8, EsU16)) as u16)
}
fn read_u8(&mut self) -> DecodeResult<u8> {
Ok(doc_as_u8(try!(self.next_doc(EsU8))))
}
fn read_uint(&mut self) -> DecodeResult<usize> {
let v = try!(self._next_int(EsU8, EsU64));
if v > (::std::usize::MAX as u64) {
@ -667,10 +728,18 @@ pub mod reader {
}
}
fn read_i64(&mut self) -> DecodeResult<i64> { Ok(try!(self._next_int(EsI8, EsI64)) as i64) }
fn read_i32(&mut self) -> DecodeResult<i32> { Ok(try!(self._next_int(EsI8, EsI32)) as i32) }
fn read_i16(&mut self) -> DecodeResult<i16> { Ok(try!(self._next_int(EsI8, EsI16)) as i16) }
fn read_i8(&mut self) -> DecodeResult<i8> { Ok(doc_as_u8(try!(self.next_doc(EsI8))) as i8) }
fn read_i64(&mut self) -> DecodeResult<i64> {
Ok(try!(self._next_int(EsI8, EsI64)) as i64)
}
fn read_i32(&mut self) -> DecodeResult<i32> {
Ok(try!(self._next_int(EsI8, EsI32)) as i32)
}
fn read_i16(&mut self) -> DecodeResult<i16> {
Ok(try!(self._next_int(EsI8, EsI16)) as i16)
}
fn read_i8(&mut self) -> DecodeResult<i8> {
Ok(doc_as_u8(try!(self.next_doc(EsI8))) as i8)
}
fn read_int(&mut self) -> DecodeResult<isize> {
let v = try!(self._next_int(EsI8, EsI64)) as i64;
if v > (isize::MAX as i64) || v < (isize::MIN as i64) {
@ -701,8 +770,8 @@ pub mod reader {
}
// Compound types:
fn read_enum<T, F>(&mut self, name: &str, f: F) -> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>,
fn read_enum<T, F>(&mut self, name: &str, f: F) -> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>
{
debug!("read_enum({})", name);
@ -719,9 +788,8 @@ pub mod reader {
Ok(result)
}
fn read_enum_variant<T, F>(&mut self, _: &[&str],
mut f: F) -> DecodeResult<T>
where F: FnMut(&mut Decoder<'doc>, usize) -> DecodeResult<T>,
fn read_enum_variant<T, F>(&mut self, _: &[&str], mut f: F) -> DecodeResult<T>
where F: FnMut(&mut Decoder<'doc>, usize) -> DecodeResult<T>
{
debug!("read_enum_variant()");
let idx = try!(self._next_sub());
@ -730,16 +798,15 @@ pub mod reader {
f(self, idx)
}
fn read_enum_variant_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>,
fn read_enum_variant_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>
{
debug!("read_enum_variant_arg(idx={})", idx);
f(self)
}
fn read_enum_struct_variant<T, F>(&mut self, _: &[&str],
mut f: F) -> DecodeResult<T>
where F: FnMut(&mut Decoder<'doc>, usize) -> DecodeResult<T>,
fn read_enum_struct_variant<T, F>(&mut self, _: &[&str], mut f: F) -> DecodeResult<T>
where F: FnMut(&mut Decoder<'doc>, usize) -> DecodeResult<T>
{
debug!("read_enum_struct_variant()");
let idx = try!(self._next_sub());
@ -752,67 +819,66 @@ pub mod reader {
name: &str,
idx: usize,
f: F)
-> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>,
-> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>
{
debug!("read_enum_struct_variant_arg(name={}, idx={})", name, idx);
f(self)
}
fn read_struct<T, F>(&mut self, name: &str, _: usize, f: F) -> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>,
fn read_struct<T, F>(&mut self, name: &str, _: usize, f: F) -> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>
{
debug!("read_struct(name={})", name);
f(self)
}
fn read_struct_field<T, F>(&mut self, name: &str, idx: usize, f: F) -> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>,
fn read_struct_field<T, F>(&mut self, name: &str, idx: usize, f: F) -> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>
{
debug!("read_struct_field(name={}, idx={})", name, idx);
f(self)
}
fn read_tuple<T, F>(&mut self, tuple_len: usize, f: F) -> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>,
fn read_tuple<T, F>(&mut self, tuple_len: usize, f: F) -> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>
{
debug!("read_tuple()");
self.read_seq(move |d, len| {
if len == tuple_len {
f(d)
} else {
Err(Expected(format!("Expected tuple of length `{}`, \
found tuple of length `{}`", tuple_len, len)))
Err(Expected(format!("Expected tuple of length `{}`, found tuple of length \
`{}`",
tuple_len,
len)))
}
})
}
fn read_tuple_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>,
fn read_tuple_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>
{
debug!("read_tuple_arg(idx={})", idx);
self.read_seq_elt(idx, f)
}
fn read_tuple_struct<T, F>(&mut self, name: &str, len: usize, f: F) -> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>,
fn read_tuple_struct<T, F>(&mut self, name: &str, len: usize, f: F) -> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>
{
debug!("read_tuple_struct(name={})", name);
self.read_tuple(len, f)
}
fn read_tuple_struct_arg<T, F>(&mut self,
idx: usize,
f: F)
-> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>,
fn read_tuple_struct_arg<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>
{
debug!("read_tuple_struct_arg(idx={})", idx);
self.read_tuple_arg(idx, f)
}
fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T> where
F: FnMut(&mut Decoder<'doc>, bool) -> DecodeResult<T>,
fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T>
where F: FnMut(&mut Decoder<'doc>, bool) -> DecodeResult<T>
{
debug!("read_option()");
self.read_enum("Option", move |this| {
@ -820,16 +886,14 @@ pub mod reader {
match idx {
0 => f(this, false),
1 => f(this, true),
_ => {
Err(Expected(format!("Expected None or Some")))
}
_ => Err(Expected(format!("Expected None or Some"))),
}
})
})
}
fn read_seq<T, F>(&mut self, f: F) -> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>, usize) -> DecodeResult<T>,
fn read_seq<T, F>(&mut self, f: F) -> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>, usize) -> DecodeResult<T>
{
debug!("read_seq()");
self.push_doc(EsVec, move |d| {
@ -839,15 +903,15 @@ pub mod reader {
})
}
fn read_seq_elt<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>,
fn read_seq_elt<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>
{
debug!("read_seq_elt(idx={})", idx);
self.push_doc(EsVecElt, f)
}
fn read_map<T, F>(&mut self, f: F) -> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>, usize) -> DecodeResult<T>,
fn read_map<T, F>(&mut self, f: F) -> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>, usize) -> DecodeResult<T>
{
debug!("read_map()");
self.push_doc(EsMap, move |d| {
@ -857,15 +921,15 @@ pub mod reader {
})
}
fn read_map_elt_key<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>,
fn read_map_elt_key<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>
{
debug!("read_map_elt_key(idx={})", idx);
self.push_doc(EsMapKey, f)
}
fn read_map_elt_val<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T> where
F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>,
fn read_map_elt_val<T, F>(&mut self, idx: usize, f: F) -> DecodeResult<T>
where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult<T>
{
debug!("read_map_elt_val(idx={})", idx);
self.push_doc(EsMapVal, f)
@ -883,9 +947,8 @@ pub mod writer {
use std::io::{self, SeekFrom, Cursor};
use super::opaque;
use super::{ EsVec, EsMap, EsEnum, EsSub8, EsSub32, EsVecElt, EsMapKey,
EsU64, EsU32, EsU16, EsU8, EsI64, EsI32, EsI16, EsI8,
EsBool, EsF64, EsF32, EsChar, EsStr, EsMapVal,
use super::{EsVec, EsMap, EsEnum, EsSub8, EsSub32, EsVecElt, EsMapKey, EsU64, EsU32, EsU16,
EsU8, EsI64, EsI32, EsI16, EsI8, EsBool, EsF64, EsF32, EsChar, EsStr, EsMapVal,
EsOpaque, NUM_IMPLICIT_TAGS, NUM_TAGS};
use serialize;
@ -906,8 +969,7 @@ pub mod writer {
} else if 0x100 <= n && n < NUM_TAGS {
w.write_all(&[0xf0 | (n >> 8) as u8, n as u8])
} else {
Err(io::Error::new(io::ErrorKind::Other,
&format!("invalid tag: {}", n)[..]))
Err(io::Error::new(io::ErrorKind::Other, &format!("invalid tag: {}", n)[..]))
}
}
@ -915,29 +977,33 @@ pub mod writer {
match size {
1 => w.write_all(&[0x80 | (n as u8)]),
2 => w.write_all(&[0x40 | ((n >> 8) as u8), n as u8]),
3 => w.write_all(&[0x20 | ((n >> 16) as u8), (n >> 8) as u8,
n as u8]),
4 => w.write_all(&[0x10 | ((n >> 24) as u8), (n >> 16) as u8,
(n >> 8) as u8, n as u8]),
_ => Err(io::Error::new(io::ErrorKind::Other,
&format!("isize too big: {}", n)[..]))
3 => w.write_all(&[0x20 | ((n >> 16) as u8), (n >> 8) as u8, n as u8]),
4 => w.write_all(&[0x10 | ((n >> 24) as u8), (n >> 16) as u8, (n >> 8) as u8, n as u8]),
_ => Err(io::Error::new(io::ErrorKind::Other, &format!("isize too big: {}", n)[..])),
}
}
pub fn write_vuint<W: Write>(w: &mut W, n: usize) -> EncodeResult {
if n < 0x7f { return write_sized_vuint(w, n, 1); }
if n < 0x4000 { return write_sized_vuint(w, n, 2); }
if n < 0x200000 { return write_sized_vuint(w, n, 3); }
if n < 0x10000000 { return write_sized_vuint(w, n, 4); }
Err(io::Error::new(io::ErrorKind::Other,
&format!("isize too big: {}", n)[..]))
if n < 0x7f {
return write_sized_vuint(w, n, 1);
}
if n < 0x4000 {
return write_sized_vuint(w, n, 2);
}
if n < 0x200000 {
return write_sized_vuint(w, n, 3);
}
if n < 0x10000000 {
return write_sized_vuint(w, n, 4);
}
Err(io::Error::new(io::ErrorKind::Other, &format!("isize too big: {}", n)[..]))
}
impl<'a> Encoder<'a> {
pub fn new(w: &'a mut Cursor<Vec<u8>>) -> Encoder<'a> {
Encoder {
writer: w,
size_positions: vec!(),
size_positions: vec![],
relax_limit: 0,
}
}
@ -987,8 +1053,8 @@ pub mod writer {
Ok(())
}
pub fn wr_tag<F>(&mut self, tag_id: usize, blk: F) -> EncodeResult where
F: FnOnce() -> EncodeResult,
pub fn wr_tag<F>(&mut self, tag_id: usize, blk: F) -> EncodeResult
where F: FnOnce() -> EncodeResult
{
try!(self.start_tag(tag_id));
try!(blk());
@ -1120,13 +1186,12 @@ pub mod writer {
self.wr_tagged_raw_u32(EsSub32 as usize, v as u32)
} else {
Err(io::Error::new(io::ErrorKind::Other,
&format!("length or variant id too big: {}",
v)[..]))
&format!("length or variant id too big: {}", v)[..]))
}
}
pub fn emit_opaque<F>(&mut self, f: F) -> EncodeResult where
F: FnOnce(&mut opaque::Encoder) -> EncodeResult,
pub fn emit_opaque<F>(&mut self, f: F) -> EncodeResult
where F: FnOnce(&mut opaque::Encoder) -> EncodeResult
{
try!(self.start_tag(EsOpaque as usize));
@ -1223,27 +1288,23 @@ pub mod writer {
self.wr_tagged_str(EsStr as usize, v)
}
fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
try!(self.start_tag(EsEnum as usize));
try!(f(self));
self.end_tag()
}
fn emit_enum_variant<F>(&mut self,
_: &str,
v_id: usize,
_: usize,
f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_enum_variant<F>(&mut self, _: &str, v_id: usize, _: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
try!(self._emit_tagged_sub(v_id));
f(self)
}
fn emit_enum_variant_arg<F>(&mut self, _: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_enum_variant_arg<F>(&mut self, _: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
f(self)
}
@ -1252,72 +1313,70 @@ pub mod writer {
v_name: &str,
v_id: usize,
cnt: usize,
f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
f: F)
-> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_enum_variant(v_name, v_id, cnt, f)
}
fn emit_enum_struct_variant_field<F>(&mut self,
_: &str,
idx: usize,
f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_enum_variant_arg(idx, f)
}
fn emit_struct<F>(&mut self, _: &str, _len: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_struct<F>(&mut self, _: &str, _len: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
f(self)
}
fn emit_struct_field<F>(&mut self, _name: &str, _: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_struct_field<F>(&mut self, _name: &str, _: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
f(self)
}
fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_seq(len, f)
}
fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_seq_elt(idx, f)
}
fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_seq(len, f)
}
fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_seq_elt(idx, f)
}
fn emit_option<F>(&mut self, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_option<F>(&mut self, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_enum("Option", f)
}
fn emit_option_none(&mut self) -> EncodeResult {
self.emit_enum_variant("None", 0, 0, |_| Ok(()))
}
fn emit_option_some<F>(&mut self, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_option_some<F>(&mut self, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_enum_variant("Some", 1, 1, f)
}
fn emit_seq<F>(&mut self, len: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_seq<F>(&mut self, len: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
if len == 0 {
// empty vector optimization
@ -1330,8 +1389,8 @@ pub mod writer {
self.end_tag()
}
fn emit_seq_elt<F>(&mut self, _idx: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_seq_elt<F>(&mut self, _idx: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
try!(self.start_tag(EsVecElt as usize));
@ -1339,8 +1398,8 @@ pub mod writer {
self.end_tag()
}
fn emit_map<F>(&mut self, len: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_map<F>(&mut self, len: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
if len == 0 {
// empty map optimization
@ -1353,8 +1412,8 @@ pub mod writer {
self.end_tag()
}
fn emit_map_elt_key<F>(&mut self, _idx: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_map_elt_key<F>(&mut self, _idx: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
try!(self.start_tag(EsMapKey as usize));
@ -1362,8 +1421,8 @@ pub mod writer {
self.end_tag()
}
fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
try!(self.start_tag(EsMapVal as usize));
try!(f(self));
@ -1385,16 +1444,8 @@ mod tests {
#[test]
fn test_vuint_at() {
let data = &[
0x80,
0xff,
0x40, 0x00,
0x7f, 0xff,
0x20, 0x00, 0x00,
0x3f, 0xff, 0xff,
0x10, 0x00, 0x00, 0x00,
0x1f, 0xff, 0xff, 0xff
];
let data = &[0x80, 0xff, 0x40, 0x00, 0x7f, 0xff, 0x20, 0x00, 0x00, 0x3f, 0xff, 0xff, 0x10,
0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff];
let mut res: reader::Res;
@ -1461,12 +1512,14 @@ mod bench {
#[bench]
pub fn vuint_at_A_aligned(b: &mut Bencher) {
let data = (0..4*100).map(|i| {
let data = (0..4 * 100)
.map(|i| {
match i % 2 {
0 => 0x80,
_ => i as u8,
}
}).collect::<Vec<_>>();
})
.collect::<Vec<_>>();
let mut sum = 0;
b.iter(|| {
let mut i = 0;
@ -1479,12 +1532,14 @@ mod bench {
#[bench]
pub fn vuint_at_A_unaligned(b: &mut Bencher) {
let data = (0..4*100+1).map(|i| {
let data = (0..4 * 100 + 1)
.map(|i| {
match i % 2 {
1 => 0x80,
_ => i as u8
_ => i as u8,
}
}).collect::<Vec<_>>();
})
.collect::<Vec<_>>();
let mut sum = 0;
b.iter(|| {
let mut i = 1;
@ -1497,13 +1552,15 @@ mod bench {
#[bench]
pub fn vuint_at_D_aligned(b: &mut Bencher) {
let data = (0..4*100).map(|i| {
let data = (0..4 * 100)
.map(|i| {
match i % 4 {
0 => 0x10,
3 => i as u8,
_ => 0
_ => 0,
}
}).collect::<Vec<_>>();
})
.collect::<Vec<_>>();
let mut sum = 0;
b.iter(|| {
let mut i = 0;
@ -1516,13 +1573,15 @@ mod bench {
#[bench]
pub fn vuint_at_D_unaligned(b: &mut Bencher) {
let data = (0..4*100+1).map(|i| {
let data = (0..4 * 100 + 1)
.map(|i| {
match i % 4 {
1 => 0x10,
0 => i as u8,
_ => 0
_ => 0,
}
}).collect::<Vec<_>>();
})
.collect::<Vec<_>>();
let mut sum = 0;
b.iter(|| {
let mut i = 1;

View File

@ -10,8 +10,7 @@
use Error as DecodeError;
use writer::EncodeResult;
use leb128::{read_signed_leb128, read_unsigned_leb128, write_signed_leb128,
write_unsigned_leb128};
use leb128::{read_signed_leb128, read_unsigned_leb128, write_signed_leb128, write_unsigned_leb128};
use std::io::{self, Write};
use serialize;
@ -25,9 +24,7 @@ pub struct Encoder<'a> {
impl<'a> Encoder<'a> {
pub fn new(cursor: &'a mut io::Cursor<Vec<u8>>) -> Encoder<'a> {
Encoder {
cursor: cursor
}
Encoder { cursor: cursor }
}
}
@ -101,7 +98,11 @@ impl<'a> serialize::Encoder for Encoder<'a> {
}
fn emit_bool(&mut self, v: bool) -> EncodeResult {
self.emit_u8(if v { 1 } else { 0 })
self.emit_u8(if v {
1
} else {
0
})
}
fn emit_f64(&mut self, v: f64) -> EncodeResult {
@ -125,7 +126,8 @@ impl<'a> serialize::Encoder for Encoder<'a> {
}
fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult
where F: FnOnce(&mut Self) -> EncodeResult {
where F: FnOnce(&mut Self) -> EncodeResult
{
f(self)
}
@ -133,15 +135,16 @@ impl<'a> serialize::Encoder for Encoder<'a> {
_v_name: &str,
v_id: usize,
_len: usize,
f: F) -> EncodeResult
f: F)
-> EncodeResult
where F: FnOnce(&mut Self) -> EncodeResult
{
try!(self.emit_uint(v_id));
f(self)
}
fn emit_enum_variant_arg<F>(&mut self, _: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_enum_variant_arg<F>(&mut self, _: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
f(self)
}
@ -150,59 +153,57 @@ impl<'a> serialize::Encoder for Encoder<'a> {
v_name: &str,
v_id: usize,
cnt: usize,
f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
f: F)
-> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_enum_variant(v_name, v_id, cnt, f)
}
fn emit_enum_struct_variant_field<F>(&mut self,
_: &str,
idx: usize,
f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_enum_struct_variant_field<F>(&mut self, _: &str, idx: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_enum_variant_arg(idx, f)
}
fn emit_struct<F>(&mut self, _: &str, _len: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_struct<F>(&mut self, _: &str, _len: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
f(self)
}
fn emit_struct_field<F>(&mut self, _name: &str, _: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_struct_field<F>(&mut self, _name: &str, _: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
f(self)
}
fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_tuple<F>(&mut self, len: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_seq(len, f)
}
fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_tuple_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_seq_elt(idx, f)
}
fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_seq(len, f)
}
fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_tuple_struct_arg<F>(&mut self, idx: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_seq_elt(idx, f)
}
fn emit_option<F>(&mut self, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_option<F>(&mut self, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_enum("Option", f)
}
@ -211,40 +212,40 @@ impl<'a> serialize::Encoder for Encoder<'a> {
self.emit_enum_variant("None", 0, 0, |_| Ok(()))
}
fn emit_option_some<F>(&mut self, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_option_some<F>(&mut self, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
self.emit_enum_variant("Some", 1, 1, f)
}
fn emit_seq<F>(&mut self, len: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_seq<F>(&mut self, len: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
try!(self.emit_uint(len));
f(self)
}
fn emit_seq_elt<F>(&mut self, _idx: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_seq_elt<F>(&mut self, _idx: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
f(self)
}
fn emit_map<F>(&mut self, len: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_map<F>(&mut self, len: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
try!(self.emit_uint(len));
f(self)
}
fn emit_map_elt_key<F>(&mut self, _idx: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_map_elt_key<F>(&mut self, _idx: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
f(self)
}
fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult where
F: FnOnce(&mut Encoder<'a>) -> EncodeResult,
fn emit_map_elt_val<F>(&mut self, _idx: usize, f: F) -> EncodeResult
where F: FnOnce(&mut Encoder<'a>) -> EncodeResult
{
f(self)
}
@ -256,9 +257,7 @@ impl<'a> Encoder<'a> {
}
pub fn from_rbml<'b: 'c, 'c>(rbml: &'c mut ::writer::Encoder<'b>) -> Encoder<'c> {
Encoder {
cursor: rbml.writer
}
Encoder { cursor: rbml.writer }
}
}
@ -275,7 +274,7 @@ impl<'a> Decoder<'a> {
pub fn new(data: &'a [u8], position: usize) -> Decoder<'a> {
Decoder {
data: data,
position: position
position: position,
}
}
@ -349,9 +348,7 @@ impl<'a> serialize::Decoder for Decoder<'a> {
fn read_i8(&mut self) -> Result<i8, Self::Error> {
let as_u8 = self.data[self.position];
self.position += 1;
unsafe {
Ok(::std::mem::transmute(as_u8))
}
unsafe { Ok(::std::mem::transmute(as_u8)) }
}
fn read_int(&mut self) -> Result<isize, Self::Error> {
@ -385,32 +382,27 @@ impl<'a> serialize::Decoder for Decoder<'a> {
Ok(s.to_string())
}
fn read_enum<T, F>(&mut self, _name: &str, f: F) -> Result<T, Self::Error> where
F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>,
fn read_enum<T, F>(&mut self, _name: &str, f: F) -> Result<T, Self::Error>
where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>
{
f(self)
}
fn read_enum_variant<T, F>(&mut self,
_: &[&str],
mut f: F)
-> Result<T, Self::Error>
where F: FnMut(&mut Decoder<'a>, usize) -> Result<T, Self::Error>,
fn read_enum_variant<T, F>(&mut self, _: &[&str], mut f: F) -> Result<T, Self::Error>
where F: FnMut(&mut Decoder<'a>, usize) -> Result<T, Self::Error>
{
let disr = try!(self.read_uint());
f(self, disr)
}
fn read_enum_variant_arg<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error> where
F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>,
fn read_enum_variant_arg<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error>
where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>
{
f(self)
}
fn read_enum_struct_variant<T, F>(&mut self,
_: &[&str],
mut f: F) -> Result<T, Self::Error>
where F: FnMut(&mut Decoder<'a>, usize) -> Result<T, Self::Error>,
fn read_enum_struct_variant<T, F>(&mut self, _: &[&str], mut f: F) -> Result<T, Self::Error>
where F: FnMut(&mut Decoder<'a>, usize) -> Result<T, Self::Error>
{
let disr = try!(self.read_uint());
f(self, disr)
@ -420,29 +412,26 @@ impl<'a> serialize::Decoder for Decoder<'a> {
_name: &str,
_idx: usize,
f: F)
-> Result<T, Self::Error> where
F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>,
-> Result<T, Self::Error>
where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>
{
f(self)
}
fn read_struct<T, F>(&mut self, _name: &str, _: usize, f: F) -> Result<T, Self::Error> where
F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>,
fn read_struct<T, F>(&mut self, _name: &str, _: usize, f: F) -> Result<T, Self::Error>
where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>
{
f(self)
}
fn read_struct_field<T, F>(&mut self,
_name: &str,
_idx: usize, f: F)
-> Result<T, Self::Error> where
F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>,
fn read_struct_field<T, F>(&mut self, _name: &str, _idx: usize, f: F) -> Result<T, Self::Error>
where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>
{
f(self)
}
fn read_tuple<T, F>(&mut self, tuple_len: usize, f: F) -> Result<T, Self::Error> where
F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>,
fn read_tuple<T, F>(&mut self, tuple_len: usize, f: F) -> Result<T, Self::Error>
where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>
{
self.read_seq(move |d, len| {
if len == tuple_len {
@ -456,32 +445,26 @@ impl<'a> serialize::Decoder for Decoder<'a> {
})
}
fn read_tuple_arg<T, F>(&mut self, idx: usize, f: F) -> Result<T, Self::Error> where
F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>,
fn read_tuple_arg<T, F>(&mut self, idx: usize, f: F) -> Result<T, Self::Error>
where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>
{
self.read_seq_elt(idx, f)
}
fn read_tuple_struct<T, F>(&mut self,
_name: &str,
len: usize, f: F)
-> Result<T, Self::Error> where
F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>,
fn read_tuple_struct<T, F>(&mut self, _name: &str, len: usize, f: F) -> Result<T, Self::Error>
where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>
{
self.read_tuple(len, f)
}
fn read_tuple_struct_arg<T, F>(&mut self,
idx: usize,
f: F)
-> Result<T, Self::Error> where
F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>,
fn read_tuple_struct_arg<T, F>(&mut self, idx: usize, f: F) -> Result<T, Self::Error>
where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>
{
self.read_tuple_arg(idx, f)
}
fn read_option<T, F>(&mut self, mut f: F) -> Result<T, Self::Error> where
F: FnMut(&mut Decoder<'a>, bool) -> Result<T, Self::Error>,
fn read_option<T, F>(&mut self, mut f: F) -> Result<T, Self::Error>
where F: FnMut(&mut Decoder<'a>, bool) -> Result<T, Self::Error>
{
self.read_enum("Option", move |this| {
this.read_enum_variant(&["None", "Some"], move |this, idx| {
@ -497,34 +480,34 @@ impl<'a> serialize::Decoder for Decoder<'a> {
})
}
fn read_seq<T, F>(&mut self, f: F) -> Result<T, Self::Error> where
F: FnOnce(&mut Decoder<'a>, usize) -> Result<T, Self::Error>,
fn read_seq<T, F>(&mut self, f: F) -> Result<T, Self::Error>
where F: FnOnce(&mut Decoder<'a>, usize) -> Result<T, Self::Error>
{
let len = try!(self.read_uint());
f(self, len)
}
fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error> where
F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>,
fn read_seq_elt<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error>
where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>
{
f(self)
}
fn read_map<T, F>(&mut self, f: F) -> Result<T, Self::Error> where
F: FnOnce(&mut Decoder<'a>, usize) -> Result<T, Self::Error>,
fn read_map<T, F>(&mut self, f: F) -> Result<T, Self::Error>
where F: FnOnce(&mut Decoder<'a>, usize) -> Result<T, Self::Error>
{
let len = try!(self.read_uint());
f(self, len)
}
fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error> where
F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>,
fn read_map_elt_key<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error>
where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>
{
f(self)
}
fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error> where
F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>,
fn read_map_elt_val<T, F>(&mut self, _idx: usize, f: F) -> Result<T, Self::Error>
where F: FnOnce(&mut Decoder<'a>) -> Result<T, Self::Error>
{
f(self)
}
@ -538,7 +521,7 @@ impl<'a> serialize::Decoder for Decoder<'a> {
#[cfg(test)]
mod tests {
use serialize::{Encodable, Decodable};
use std::io::{Cursor};
use std::io::Cursor;
use std::fmt::Debug;
use super::{Encoder, Decoder};
@ -681,8 +664,7 @@ mod tests {
#[test]
fn test_string() {
let vec = vec![
"abcbuÖeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
let vec = vec!["abcbuÖeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
"abcbuÖganeiovÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(),
"abcbuÖganeiovÄnameÜavmpßvmea€µsbpapmaebn".to_string(),
"abcbuÖganeiovÄnameÜavmpßvmeabpnvapeapmaebn".to_string(),
@ -768,15 +750,27 @@ mod tests {
enum Enum {
Variant1,
Variant2(usize, f32),
Variant3 { a: i32, b: char, c: bool }
Variant3 {
a: i32,
b: char,
c: bool,
},
}
#[test]
fn test_enum() {
check_round_trip(vec![Enum::Variant1,
Enum::Variant2(1, 2.5),
Enum::Variant3 { a: 3, b: 'b', c: false },
Enum::Variant3 { a: -4, b: 'f', c: true }]);
Enum::Variant3 {
a: 3,
b: 'b',
c: false,
},
Enum::Variant3 {
a: -4,
b: 'f',
c: true,
}]);
}
#[test]