diff --git a/src/librbml/leb128.rs b/src/librbml/leb128.rs index a5e8522623a..0c5356c0222 100644 --- a/src/librbml/leb128.rs +++ b/src/librbml/leb128.rs @@ -9,8 +9,7 @@ // except according to those terms. #[inline] -pub fn write_to_vec(vec: &mut Vec, position: &mut usize, byte: u8) -{ +pub fn write_to_vec(vec: &mut Vec, 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, position: &mut usize, byte: u8) *position += 1; } -pub fn write_unsigned_leb128(out: &mut Vec, - start_position: usize, - mut value: u64) - -> usize { +pub fn write_unsigned_leb128(out: &mut Vec, 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, 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,15 +56,13 @@ pub fn read_unsigned_leb128(data: &[u8], } -pub fn write_signed_leb128(out: &mut Vec, - start_position: usize, - mut value: i64) -> usize { +pub fn write_signed_leb128(out: &mut Vec, start_position: usize, mut value: i64) -> usize { let mut position = start_position; loop { let mut byte = (value as u8) & 0x7f; value >>= 7; - let more = !((((value == 0 ) && ((byte & 0x40) == 0)) || + let more = !((((value == 0) && ((byte & 0x40) == 0)) || ((value == -1) && ((byte & 0x40) != 0)))); if more { byte |= 0x80; // Mark this byte to show that more bytes will follow. @@ -87,9 +78,7 @@ pub fn write_signed_leb128(out: &mut Vec, 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); } diff --git a/src/librbml/lib.rs b/src/librbml/lib.rs index fe15f1c53f9..e9e6d8bf1fb 100644 --- a/src/librbml/lib.rs +++ b/src/librbml/lib.rs @@ -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,44 +189,38 @@ 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 - EsU64 = 0x03, // + 8 bytes - EsI8 = 0x04, // + 1 byte - EsI16 = 0x05, // + 2 bytes - EsI32 = 0x06, // + 4 bytes - EsI64 = 0x07, // + 8 bytes - EsBool = 0x08, // + 1 byte - EsChar = 0x09, // + 4 bytes - EsF32 = 0x0a, // + 4 bytes - EsF64 = 0x0b, // + 8 bytes - EsSub8 = 0x0c, // + 1 byte - EsSub32 = 0x0d, // + 4 bytes + EsU8 = 0x00, // + 1 byte + EsU16 = 0x01, // + 2 bytes + EsU32 = 0x02, // + 4 bytes + EsU64 = 0x03, // + 8 bytes + EsI8 = 0x04, // + 1 byte + EsI16 = 0x05, // + 2 bytes + EsI32 = 0x06, // + 4 bytes + EsI64 = 0x07, // + 8 bytes + EsBool = 0x08, // + 1 byte + EsChar = 0x09, // + 4 bytes + EsF32 = 0x0a, // + 4 bytes + EsF64 = 0x0b, // + 8 bytes + 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* - EsVecElt = 0x13, - EsMap = 0x14, // encodes the # of pairs as the first EsSub* - EsMapKey = 0x15, - EsMapVal = 0x16, - EsOpaque = 0x17, + EsStr = 0x10, + EsEnum = 0x11, // encodes the variant id as the first EsSub* + EsVec = 0x12, // encodes the # of elements as the first EsSub* + EsVecElt = 0x13, + EsMap = 0x14, // encodes the # of pairs as the first EsSub* + EsMapKey = 0x15, + EsMapVal = 0x16, + EsOpaque = 0x17, } 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 = Result; // 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 { 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 { 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 | - (data[start + 2] as usize), - next: start + 3}); + return Ok(Res { + val: ((a & 0x1f) as usize) << 16 | (data[start + 1] as usize) << 8 | + (data[start + 2] as usize), + next: start + 3, + }); } if a & 0x10 != 0 { - 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}); + 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, + }); } 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 { 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(d: Doc, f: F) -> T where - F: FnOnce(&[u8]) -> T, + pub fn with_doc_data(d: Doc, f: F) -> T + where F: FnOnce(&[u8]) -> T { f(&d.data[d.start..d.end]) } @@ -485,30 +513,48 @@ pub mod reader { // of the page and segfault. let mut b = [0; 8]; - b.clone_from_slice(&d.data[d.end-8..d.end]); + b.clone_from_slice(&d.data[d.end - 8..d.end]); let data = unsafe { (*(b.as_ptr() as *const u64)).to_be() }; let len = d.end - d.start; if len < 8 { - data & ((1<<(len*8))-1) + data & ((1 << (len * 8)) - 1) } else { data } } else { let mut result = 0; for b in &d.data[d.start..d.end] { - result = (result<<8) + (*b as u64); + result = (result << 8) + (*b as u64); } result } } - #[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> { 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(&mut self, exp_tag: EbmlEncoderTag, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult, + fn push_doc(&mut self, exp_tag: EbmlEncoderTag, f: F) -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult { 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 { + last_tag: EbmlEncoderTag) + -> DecodeResult { 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(&mut self, op: F) -> DecodeResult where - F: FnOnce(&mut opaque::Decoder, Doc) -> DecodeResult, + pub fn read_opaque(&mut self, op: F) -> DecodeResult + where F: FnOnce(&mut opaque::Decoder, Doc) -> DecodeResult { 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 { self._next_int(EsU8, EsU64) } - fn read_u32(&mut self) -> DecodeResult { Ok(try!(self._next_int(EsU8, EsU32)) as u32) } - fn read_u16(&mut self) -> DecodeResult { Ok(try!(self._next_int(EsU8, EsU16)) as u16) } - fn read_u8(&mut self) -> DecodeResult { Ok(doc_as_u8(try!(self.next_doc(EsU8)))) } + fn read_u64(&mut self) -> DecodeResult { + self._next_int(EsU8, EsU64) + } + fn read_u32(&mut self) -> DecodeResult { + Ok(try!(self._next_int(EsU8, EsU32)) as u32) + } + fn read_u16(&mut self) -> DecodeResult { + Ok(try!(self._next_int(EsU8, EsU16)) as u16) + } + fn read_u8(&mut self) -> DecodeResult { + Ok(doc_as_u8(try!(self.next_doc(EsU8)))) + } fn read_uint(&mut self) -> DecodeResult { 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 { Ok(try!(self._next_int(EsI8, EsI64)) as i64) } - fn read_i32(&mut self) -> DecodeResult { Ok(try!(self._next_int(EsI8, EsI32)) as i32) } - fn read_i16(&mut self) -> DecodeResult { Ok(try!(self._next_int(EsI8, EsI16)) as i16) } - fn read_i8(&mut self) -> DecodeResult { Ok(doc_as_u8(try!(self.next_doc(EsI8))) as i8) } + fn read_i64(&mut self) -> DecodeResult { + Ok(try!(self._next_int(EsI8, EsI64)) as i64) + } + fn read_i32(&mut self) -> DecodeResult { + Ok(try!(self._next_int(EsI8, EsI32)) as i32) + } + fn read_i16(&mut self) -> DecodeResult { + Ok(try!(self._next_int(EsI8, EsI16)) as i16) + } + fn read_i8(&mut self) -> DecodeResult { + Ok(doc_as_u8(try!(self.next_doc(EsI8))) as i8) + } fn read_int(&mut self) -> DecodeResult { 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(&mut self, name: &str, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult, + fn read_enum(&mut self, name: &str, f: F) -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult { debug!("read_enum({})", name); @@ -719,9 +788,8 @@ pub mod reader { Ok(result) } - fn read_enum_variant(&mut self, _: &[&str], - mut f: F) -> DecodeResult - where F: FnMut(&mut Decoder<'doc>, usize) -> DecodeResult, + fn read_enum_variant(&mut self, _: &[&str], mut f: F) -> DecodeResult + where F: FnMut(&mut Decoder<'doc>, usize) -> DecodeResult { 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(&mut self, idx: usize, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult, + fn read_enum_variant_arg(&mut self, idx: usize, f: F) -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult { debug!("read_enum_variant_arg(idx={})", idx); f(self) } - fn read_enum_struct_variant(&mut self, _: &[&str], - mut f: F) -> DecodeResult - where F: FnMut(&mut Decoder<'doc>, usize) -> DecodeResult, + fn read_enum_struct_variant(&mut self, _: &[&str], mut f: F) -> DecodeResult + where F: FnMut(&mut Decoder<'doc>, usize) -> DecodeResult { 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 where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult, + -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult { - debug!("read_enum_struct_variant_arg(name={}, idx={})", name, idx); + debug!("read_enum_struct_variant_arg(name={}, idx={})", name, idx); f(self) } - fn read_struct(&mut self, name: &str, _: usize, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult, + fn read_struct(&mut self, name: &str, _: usize, f: F) -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult { debug!("read_struct(name={})", name); f(self) } - fn read_struct_field(&mut self, name: &str, idx: usize, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult, + fn read_struct_field(&mut self, name: &str, idx: usize, f: F) -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult { debug!("read_struct_field(name={}, idx={})", name, idx); f(self) } - fn read_tuple(&mut self, tuple_len: usize, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult, + fn read_tuple(&mut self, tuple_len: usize, f: F) -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult { 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(&mut self, idx: usize, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult, + fn read_tuple_arg(&mut self, idx: usize, f: F) -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult { debug!("read_tuple_arg(idx={})", idx); self.read_seq_elt(idx, f) } - fn read_tuple_struct(&mut self, name: &str, len: usize, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult, + fn read_tuple_struct(&mut self, name: &str, len: usize, f: F) -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult { debug!("read_tuple_struct(name={})", name); self.read_tuple(len, f) } - fn read_tuple_struct_arg(&mut self, - idx: usize, - f: F) - -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult, + fn read_tuple_struct_arg(&mut self, idx: usize, f: F) -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult { debug!("read_tuple_struct_arg(idx={})", idx); self.read_tuple_arg(idx, f) } - fn read_option(&mut self, mut f: F) -> DecodeResult where - F: FnMut(&mut Decoder<'doc>, bool) -> DecodeResult, + fn read_option(&mut self, mut f: F) -> DecodeResult + where F: FnMut(&mut Decoder<'doc>, bool) -> DecodeResult { 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(&mut self, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>, usize) -> DecodeResult, + fn read_seq(&mut self, f: F) -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>, usize) -> DecodeResult { debug!("read_seq()"); self.push_doc(EsVec, move |d| { @@ -839,15 +903,15 @@ pub mod reader { }) } - fn read_seq_elt(&mut self, idx: usize, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult, + fn read_seq_elt(&mut self, idx: usize, f: F) -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult { debug!("read_seq_elt(idx={})", idx); self.push_doc(EsVecElt, f) } - fn read_map(&mut self, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>, usize) -> DecodeResult, + fn read_map(&mut self, f: F) -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>, usize) -> DecodeResult { debug!("read_map()"); self.push_doc(EsMap, move |d| { @@ -857,15 +921,15 @@ pub mod reader { }) } - fn read_map_elt_key(&mut self, idx: usize, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult, + fn read_map_elt_key(&mut self, idx: usize, f: F) -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult { debug!("read_map_elt_key(idx={})", idx); self.push_doc(EsMapKey, f) } - fn read_map_elt_val(&mut self, idx: usize, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>) -> DecodeResult, + fn read_map_elt_val(&mut self, idx: usize, f: F) -> DecodeResult + where F: FnOnce(&mut Decoder<'doc>) -> DecodeResult { debug!("read_map_elt_val(idx={})", idx); self.push_doc(EsMapVal, f) @@ -883,10 +947,9 @@ 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, - EsOpaque, NUM_IMPLICIT_TAGS, NUM_TAGS }; + 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: &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>) -> Encoder<'a> { Encoder { writer: w, - size_positions: vec!(), + size_positions: vec![], relax_limit: 0, } } @@ -970,7 +1036,7 @@ pub mod writer { let mut buf = [0u8; RELAX_MAX_SIZE]; { let last_size_pos = last_size_pos as usize; - let data = &self.writer.get_ref()[last_size_pos+4..cur_pos as usize]; + let data = &self.writer.get_ref()[last_size_pos + 4..cur_pos as usize]; buf[..size].clone_from_slice(data); } @@ -987,8 +1053,8 @@ pub mod writer { Ok(()) } - pub fn wr_tag(&mut self, tag_id: usize, blk: F) -> EncodeResult where - F: FnOnce() -> EncodeResult, + pub fn wr_tag(&mut self, tag_id: usize, blk: F) -> EncodeResult + where F: FnOnce() -> EncodeResult { try!(self.start_tag(tag_id)); try!(blk()); @@ -1006,12 +1072,12 @@ pub mod writer { let bytes: [u8; 8] = unsafe { mem::transmute(v.to_be()) }; // tagged integers are emitted in big-endian, with no // leading zeros. - let leading_zero_bytes = v.leading_zeros()/8; + let leading_zero_bytes = v.leading_zeros() / 8; self.wr_tagged_bytes(tag_id, &bytes[leading_zero_bytes as usize..]) } #[inline] - pub fn wr_tagged_u32(&mut self, tag_id: usize, v: u32) -> EncodeResult { + pub fn wr_tagged_u32(&mut self, tag_id: usize, v: u32) -> EncodeResult { self.wr_tagged_u64(tag_id, v as u64) } @@ -1060,7 +1126,7 @@ pub mod writer { self.wr_tagged_raw_bytes(tag_id, &bytes) } - fn wr_tagged_raw_u32(&mut self, tag_id: usize, v: u32) -> EncodeResult{ + fn wr_tagged_raw_u32(&mut self, tag_id: usize, v: u32) -> EncodeResult { let bytes: [u8; 4] = unsafe { mem::transmute(v.to_be()) }; self.wr_tagged_raw_bytes(tag_id, &bytes) } @@ -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(&mut self, f: F) -> EncodeResult where - F: FnOnce(&mut opaque::Encoder) -> EncodeResult, + pub fn emit_opaque(&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(&mut self, _name: &str, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_enum(&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(&mut self, - _: &str, - v_id: usize, - _: usize, - f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_enum_variant(&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(&mut self, _: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_enum_variant_arg(&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(&mut self, - _: &str, - idx: usize, - f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_enum_struct_variant_field(&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(&mut self, _: &str, _len: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_struct(&mut self, _: &str, _len: usize, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { f(self) } - fn emit_struct_field(&mut self, _name: &str, _: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_struct_field(&mut self, _name: &str, _: usize, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { f(self) } - fn emit_tuple(&mut self, len: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_tuple(&mut self, len: usize, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { self.emit_seq(len, f) } - fn emit_tuple_arg(&mut self, idx: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_tuple_arg(&mut self, idx: usize, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { self.emit_seq_elt(idx, f) } - fn emit_tuple_struct(&mut self, _: &str, len: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_tuple_struct(&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(&mut self, idx: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_tuple_struct_arg(&mut self, idx: usize, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { self.emit_seq_elt(idx, f) } - fn emit_option(&mut self, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_option(&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(&mut self, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_option_some(&mut self, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { self.emit_enum_variant("Some", 1, 1, f) } - fn emit_seq(&mut self, len: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_seq(&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(&mut self, _idx: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_seq_elt(&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(&mut self, len: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_map(&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(&mut self, _idx: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_map_elt_key(&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(&mut self, _idx: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_map_elt_val(&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| { - match i % 2 { - 0 => 0x80, - _ => i as u8, - } - }).collect::>(); + let data = (0..4 * 100) + .map(|i| { + match i % 2 { + 0 => 0x80, + _ => i as u8, + } + }) + .collect::>(); 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| { - match i % 2 { - 1 => 0x80, - _ => i as u8 - } - }).collect::>(); + let data = (0..4 * 100 + 1) + .map(|i| { + match i % 2 { + 1 => 0x80, + _ => i as u8, + } + }) + .collect::>(); 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| { - match i % 4 { - 0 => 0x10, - 3 => i as u8, - _ => 0 - } - }).collect::>(); + let data = (0..4 * 100) + .map(|i| { + match i % 4 { + 0 => 0x10, + 3 => i as u8, + _ => 0, + } + }) + .collect::>(); 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| { - match i % 4 { - 1 => 0x10, - 0 => i as u8, - _ => 0 - } - }).collect::>(); + let data = (0..4 * 100 + 1) + .map(|i| { + match i % 4 { + 1 => 0x10, + 0 => i as u8, + _ => 0, + } + }) + .collect::>(); let mut sum = 0; b.iter(|| { let mut i = 1; diff --git a/src/librbml/opaque.rs b/src/librbml/opaque.rs index 64756090e8b..bed266ae1cc 100644 --- a/src/librbml/opaque.rs +++ b/src/librbml/opaque.rs @@ -10,14 +10,13 @@ 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; -//=----------------------------------------------------------------------------- +// =----------------------------------------------------------------------------- // Encoder -//=----------------------------------------------------------------------------- +// =----------------------------------------------------------------------------- pub struct Encoder<'a> { pub cursor: &'a mut io::Cursor>, @@ -25,9 +24,7 @@ pub struct Encoder<'a> { impl<'a> Encoder<'a> { pub fn new(cursor: &'a mut io::Cursor>) -> 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(&mut self, _name: &str, f: F) -> EncodeResult - where F: FnOnce(&mut Self) -> EncodeResult { + where F: FnOnce(&mut Self) -> EncodeResult + { f(self) } @@ -133,76 +135,75 @@ 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(&mut self, _: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_enum_variant_arg(&mut self, _: usize, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { f(self) } fn emit_enum_struct_variant(&mut self, - v_name: &str, - v_id: usize, - cnt: usize, - f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + v_name: &str, + v_id: usize, + cnt: usize, + 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(&mut self, - _: &str, - idx: usize, - f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_enum_struct_variant_field(&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(&mut self, _: &str, _len: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_struct(&mut self, _: &str, _len: usize, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { f(self) } - fn emit_struct_field(&mut self, _name: &str, _: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_struct_field(&mut self, _name: &str, _: usize, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { f(self) } - fn emit_tuple(&mut self, len: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_tuple(&mut self, len: usize, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { self.emit_seq(len, f) } - fn emit_tuple_arg(&mut self, idx: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_tuple_arg(&mut self, idx: usize, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { self.emit_seq_elt(idx, f) } - fn emit_tuple_struct(&mut self, _: &str, len: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_tuple_struct(&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(&mut self, idx: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_tuple_struct_arg(&mut self, idx: usize, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { self.emit_seq_elt(idx, f) } - fn emit_option(&mut self, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_option(&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(&mut self, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_option_some(&mut self, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { self.emit_enum_variant("Some", 1, 1, f) } - fn emit_seq(&mut self, len: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_seq(&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(&mut self, _idx: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_seq_elt(&mut self, _idx: usize, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { f(self) } - fn emit_map(&mut self, len: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_map(&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(&mut self, _idx: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_map_elt_key(&mut self, _idx: usize, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { f(self) } - fn emit_map_elt_val(&mut self, _idx: usize, f: F) -> EncodeResult where - F: FnOnce(&mut Encoder<'a>) -> EncodeResult, + fn emit_map_elt_val(&mut self, _idx: usize, f: F) -> EncodeResult + where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { f(self) } @@ -256,15 +257,13 @@ 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 } } } -//=----------------------------------------------------------------------------- +// =----------------------------------------------------------------------------- // Decoder -//=----------------------------------------------------------------------------- +// =----------------------------------------------------------------------------- pub struct Decoder<'a> { pub data: &'a [u8], @@ -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 { 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 { @@ -380,37 +377,32 @@ impl<'a> serialize::Decoder for Decoder<'a> { fn read_str(&mut self) -> Result { let len = try!(self.read_uint()); - let s = ::std::str::from_utf8(&self.data[self.position .. self.position + len]).unwrap(); + let s = ::std::str::from_utf8(&self.data[self.position..self.position + len]).unwrap(); self.position += len; Ok(s.to_string()) } - fn read_enum(&mut self, _name: &str, f: F) -> Result where - F: FnOnce(&mut Decoder<'a>) -> Result, + fn read_enum(&mut self, _name: &str, f: F) -> Result + where F: FnOnce(&mut Decoder<'a>) -> Result { f(self) } - fn read_enum_variant(&mut self, - _: &[&str], - mut f: F) - -> Result - where F: FnMut(&mut Decoder<'a>, usize) -> Result, + fn read_enum_variant(&mut self, _: &[&str], mut f: F) -> Result + where F: FnMut(&mut Decoder<'a>, usize) -> Result { let disr = try!(self.read_uint()); f(self, disr) } - fn read_enum_variant_arg(&mut self, _idx: usize, f: F) -> Result where - F: FnOnce(&mut Decoder<'a>) -> Result, + fn read_enum_variant_arg(&mut self, _idx: usize, f: F) -> Result + where F: FnOnce(&mut Decoder<'a>) -> Result { f(self) } - fn read_enum_struct_variant(&mut self, - _: &[&str], - mut f: F) -> Result - where F: FnMut(&mut Decoder<'a>, usize) -> Result, + fn read_enum_struct_variant(&mut self, _: &[&str], mut f: F) -> Result + where F: FnMut(&mut Decoder<'a>, usize) -> Result { let disr = try!(self.read_uint()); f(self, disr) @@ -420,68 +412,59 @@ impl<'a> serialize::Decoder for Decoder<'a> { _name: &str, _idx: usize, f: F) - -> Result where - F: FnOnce(&mut Decoder<'a>) -> Result, + -> Result + where F: FnOnce(&mut Decoder<'a>) -> Result { f(self) } - fn read_struct(&mut self, _name: &str, _: usize, f: F) -> Result where - F: FnOnce(&mut Decoder<'a>) -> Result, + fn read_struct(&mut self, _name: &str, _: usize, f: F) -> Result + where F: FnOnce(&mut Decoder<'a>) -> Result { f(self) } - fn read_struct_field(&mut self, - _name: &str, - _idx: usize, f: F) - -> Result where - F: FnOnce(&mut Decoder<'a>) -> Result, + fn read_struct_field(&mut self, _name: &str, _idx: usize, f: F) -> Result + where F: FnOnce(&mut Decoder<'a>) -> Result { f(self) } - fn read_tuple(&mut self, tuple_len: usize, f: F) -> Result where - F: FnOnce(&mut Decoder<'a>) -> Result, + fn read_tuple(&mut self, tuple_len: usize, f: F) -> Result + where F: FnOnce(&mut Decoder<'a>) -> Result { self.read_seq(move |d, len| { if len == tuple_len { f(d) } else { let err = format!("Invalid tuple length. Expected {}, found {}", - tuple_len, - len); + tuple_len, + len); Err(DecodeError::Expected(err)) } }) } - fn read_tuple_arg(&mut self, idx: usize, f: F) -> Result where - F: FnOnce(&mut Decoder<'a>) -> Result, + fn read_tuple_arg(&mut self, idx: usize, f: F) -> Result + where F: FnOnce(&mut Decoder<'a>) -> Result { self.read_seq_elt(idx, f) } - fn read_tuple_struct(&mut self, - _name: &str, - len: usize, f: F) - -> Result where - F: FnOnce(&mut Decoder<'a>) -> Result, + fn read_tuple_struct(&mut self, _name: &str, len: usize, f: F) -> Result + where F: FnOnce(&mut Decoder<'a>) -> Result { self.read_tuple(len, f) } - fn read_tuple_struct_arg(&mut self, - idx: usize, - f: F) - -> Result where - F: FnOnce(&mut Decoder<'a>) -> Result, + fn read_tuple_struct_arg(&mut self, idx: usize, f: F) -> Result + where F: FnOnce(&mut Decoder<'a>) -> Result { self.read_tuple_arg(idx, f) } - fn read_option(&mut self, mut f: F) -> Result where - F: FnMut(&mut Decoder<'a>, bool) -> Result, + fn read_option(&mut self, mut f: F) -> Result + where F: FnMut(&mut Decoder<'a>, bool) -> Result { 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(&mut self, f: F) -> Result where - F: FnOnce(&mut Decoder<'a>, usize) -> Result, + fn read_seq(&mut self, f: F) -> Result + where F: FnOnce(&mut Decoder<'a>, usize) -> Result { let len = try!(self.read_uint()); f(self, len) } - fn read_seq_elt(&mut self, _idx: usize, f: F) -> Result where - F: FnOnce(&mut Decoder<'a>) -> Result, + fn read_seq_elt(&mut self, _idx: usize, f: F) -> Result + where F: FnOnce(&mut Decoder<'a>) -> Result { f(self) } - fn read_map(&mut self, f: F) -> Result where - F: FnOnce(&mut Decoder<'a>, usize) -> Result, + fn read_map(&mut self, f: F) -> Result + where F: FnOnce(&mut Decoder<'a>, usize) -> Result { let len = try!(self.read_uint()); f(self, len) } - fn read_map_elt_key(&mut self, _idx: usize, f: F) -> Result where - F: FnOnce(&mut Decoder<'a>) -> Result, + fn read_map_elt_key(&mut self, _idx: usize, f: F) -> Result + where F: FnOnce(&mut Decoder<'a>) -> Result { f(self) } - fn read_map_elt_val(&mut self, _idx: usize, f: F) -> Result where - F: FnOnce(&mut Decoder<'a>) -> Result, + fn read_map_elt_val(&mut self, _idx: usize, f: F) -> Result + where F: FnOnce(&mut Decoder<'a>) -> Result { 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}; @@ -566,7 +549,7 @@ mod tests { } - fn check_round_trip(values: Vec) { + fn check_round_trip(values: Vec) { let mut cursor = Cursor::new(Vec::new()); for value in &values { @@ -591,7 +574,7 @@ mod tests { #[test] fn test_u8() { let mut vec = vec![]; - for i in ::std::u8::MIN .. ::std::u8::MAX { + for i in ::std::u8::MIN..::std::u8::MAX { vec.push(i); } check_round_trip(vec); @@ -599,7 +582,7 @@ mod tests { #[test] fn test_u16() { - for i in ::std::u16::MIN .. ::std::u16::MAX { + for i in ::std::u16::MIN..::std::u16::MAX { check_round_trip(vec![1, 2, 3, i, i, i]); } } @@ -622,7 +605,7 @@ mod tests { #[test] fn test_i8() { let mut vec = vec![]; - for i in ::std::i8::MIN .. ::std::i8::MAX { + for i in ::std::i8::MIN..::std::i8::MAX { vec.push(i); } check_round_trip(vec); @@ -630,7 +613,7 @@ mod tests { #[test] fn test_i16() { - for i in ::std::i16::MIN .. ::std::i16::MAX { + for i in ::std::i16::MIN..::std::i16::MAX { check_round_trip(vec![-1, 2, -3, i, i, i, 2]); } } @@ -658,8 +641,8 @@ mod tests { #[test] fn test_f32() { let mut vec = vec![]; - for i in -100 .. 100 { - vec.push( (i as f32) / 3.0 ); + for i in -100..100 { + vec.push((i as f32) / 3.0); } check_round_trip(vec); } @@ -667,8 +650,8 @@ mod tests { #[test] fn test_f64() { let mut vec = vec![]; - for i in -100 .. 100 { - vec.push( (i as f64) / 3.0 ); + for i in -100..100 { + vec.push((i as f64) / 3.0); } check_round_trip(vec); } @@ -681,14 +664,13 @@ mod tests { #[test] fn test_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(), - "abcbuÖganeiÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(), - "abcbuÖganeiovÄnameÜavmpßvmea€µsbpmaebn".to_string(), - "abcbuÖganeiovÄnameÜavmpßvmea€µnvapeapmaebn".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(), + "abcbuÖganeiÄnameÜavmpßvmea€µsbpnvapeapmaebn".to_string(), + "abcbuÖganeiovÄnameÜavmpßvmea€µsbpmaebn".to_string(), + "abcbuÖganeiovÄnameÜavmpßvmea€µnvapeapmaebn".to_string()]; check_round_trip(vec); } @@ -720,70 +702,82 @@ mod tests { #[test] fn test_struct() { check_round_trip(vec![Struct { - a: (), - b: 10, - c: 11, - d: 12, - e: 13, - f: 14, + a: (), + b: 10, + c: 11, + d: 12, + e: 13, + f: 14, - g: 15, - h: 16, - i: 17, - j: 18, - k: 19, + g: 15, + h: 16, + i: 17, + j: 18, + k: 19, - l: 'x', - m: "abc".to_string(), - n: 20.5, - o: 21.5, - p: false, - q: None, - }]); + l: 'x', + m: "abc".to_string(), + n: 20.5, + o: 21.5, + p: false, + q: None, + }]); check_round_trip(vec![Struct { - a: (), - b: 101, - c: 111, - d: 121, - e: 131, - f: 141, + a: (), + b: 101, + c: 111, + d: 121, + e: 131, + f: 141, - g: -15, - h: -16, - i: -17, - j: -18, - k: -19, + g: -15, + h: -16, + i: -17, + j: -18, + k: -19, - l: 'y', - m: "def".to_string(), - n: -20.5, - o: -21.5, - p: true, - q: Some(1234567), - }]); + l: 'y', + m: "def".to_string(), + n: -20.5, + o: -21.5, + p: true, + q: Some(1234567), + }]); } #[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)] 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] fn test_sequence() { let mut vec = vec![]; - for i in -100i64 .. 100i64 { - vec.push(i*100000); + for i in -100i64..100i64 { + vec.push(i * 100000); } check_round_trip(vec![vec]); @@ -793,8 +787,8 @@ mod tests { fn test_hash_map() { use std::collections::HashMap; let mut map = HashMap::new(); - for i in -100i64 .. 100i64 { - map.insert(i*100000, i*10000); + for i in -100i64..100i64 { + map.insert(i * 100000, i * 10000); } check_round_trip(vec![map]);