diff --git a/Cargo.toml b/Cargo.toml index 9fbbf5f..a132f4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,4 @@ description = "A binary serialization / deserialization strategy and implementat [dependencies] rustc-serialize = "*" -byteorder = "*" +byteorder = "0.2.9" diff --git a/examples/basic.rs b/examples/basic.rs index 8198b62..103c6f2 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -24,7 +24,7 @@ fn main() { // 8 bytes for the length of the vector, 4 bytes per float. assert_eq!(encoded.len(), 8 + 4 * 4); - let decoded: World = bincode::decode(&encoded[]).unwrap(); + let decoded: World = bincode::decode(&encoded[..]).unwrap(); assert!(world == decoded); } diff --git a/src/lib.rs b/src/lib.rs index 14ebffe..b3ef9ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ #![crate_type = "dylib"] #![doc(html_logo_url = "./icon.png")] -#![feature(hash, core, io, unicode, collections)] +#![feature(core, io, unicode, collections)] extern crate "rustc-serialize" as rustc_serialize; extern crate byteorder; diff --git a/src/reader.rs b/src/reader.rs index f5b9709..79140dd 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -8,6 +8,8 @@ use std::fmt; use rustc_serialize::Decoder; use byteorder::{BigEndian, ReadBytesExt}; +use byteorder::Error as ByteOrderError; + use unicode; use super::SizeLimit; @@ -63,8 +65,15 @@ impl fmt::Display for DecodingError { pub type DecodingResult = Result; -fn wrap_io(err: IoError) -> DecodingError { - DecodingError::IoError(err) +fn wrap_io(err: ByteOrderError) -> DecodingError { + match err { + ByteOrderError::Io(ioe) => DecodingError::IoError(ioe), + ByteOrderError::UnexpectedEOF => + DecodingError::InvalidEncoding(InvalidEncoding { + desc: "Unexpected EOF while reading a multi-byte number", + detail: None + }) + } } impl Error for DecodingError { @@ -201,7 +210,7 @@ impl<'a, R: BufRead> Decoder for DecoderReader<'a, R> { let mut buf = [0]; - let _ = try!(self.reader.read(&mut buf[])); + let _ = try!(self.reader.read(&mut buf[..])); let first_byte = buf[0]; let width = unicode::str::utf8_char_width(first_byte); if width == 1 { return Ok(first_byte as char) } diff --git a/src/test.rs b/src/test.rs index 9acd1a8..4ae7cce 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,5 +1,3 @@ -extern crate "rustc-serialize" as serialize; - use std::fmt::Debug; use std::collections::HashMap; use std::ops::Deref; @@ -26,20 +24,20 @@ use super::SizeLimit::{Infinite, Bounded}; fn the_same(element: V) where V: Encodable + Decodable + PartialEq + Debug + 'static { - // Make sure that the bahavior is correct when wrapping with a RefBox. + // Make sure that the bahavior isize correct when wrapping with a RefBox. fn ref_box_correct(v: &V) -> bool where V: Encodable + Decodable + PartialEq + Debug + 'static { let rf = RefBox::new(v); let encoded = encode(&rf, Infinite).unwrap(); - let decoded: RefBox<'static, V> = decode(&encoded[]).unwrap(); + let decoded: RefBox<'static, V> = decode(&encoded[..]).unwrap(); decoded.take().deref() == v } let size = encoded_size(&element); let encoded = encode(&element, Infinite).unwrap(); - let decoded = decode(&encoded[]).unwrap(); + let decoded = decode(&encoded[..]).unwrap(); assert!(element == decoded); assert!(size == encoded.len() as u64); assert!(ref_box_correct(&element)) @@ -78,9 +76,9 @@ fn test_string() { #[test] fn test_tuple() { - the_same((1is,)); - the_same((1is,2is,3is)); - the_same((1is,"foo".to_string(),())); + the_same((1isize,)); + the_same((1isize,2isize,3isize)); + the_same((1isize,"foo".to_string(),())); } #[test] @@ -126,7 +124,7 @@ fn test_struct_tuple() { #[test] fn option() { - the_same(Some(5us)); + the_same(Some(5usize)); the_same(Some("foo bar".to_string())); the_same(None::); } @@ -193,7 +191,7 @@ fn unicode() { #[test] fn decoding_errors() { - fn is_invalid_encoding(res: DecodingResult) { + fn isize_invalid_encoding(res: DecodingResult) { match res { Ok(_) => panic!("Expecting error"), Err(DecodingError::IoError(_)) => panic!("Expecting InvalidEncoding"), @@ -202,16 +200,16 @@ fn decoding_errors() { } } - is_invalid_encoding(decode::(vec![0xA].as_slice())); - is_invalid_encoding(decode::(vec![0, 0, 0, 0, 0, 0, 0, 1, 0xFF].as_slice())); + isize_invalid_encoding(decode::(vec![0xA].as_slice())); + isize_invalid_encoding(decode::(vec![0, 0, 0, 0, 0, 0, 0, 1, 0xFF].as_slice())); // Out-of-bounds variant #[derive(RustcEncodable, RustcDecodable)] enum Test { One, Two, }; - is_invalid_encoding(decode::(vec![0, 0, 0, 5].as_slice())); - is_invalid_encoding(decode::>(vec![5, 0].as_slice())); + isize_invalid_encoding(decode::(vec![0, 0, 0, 5].as_slice())); + isize_invalid_encoding(decode::>(vec![5, 0].as_slice())); } #[test] @@ -251,7 +249,7 @@ fn test_encoded_size() { assert!(encoded_size(&0u32) == 4); assert!(encoded_size(&0u64) == 8); - // length is stored as u64 + // length isize stored as u64 assert!(encoded_size(&"") == 8); assert!(encoded_size(&"a") == 8 + 1); @@ -279,7 +277,7 @@ fn test_refbox() { // Test 1 { let encoded = encode(&Message::M1(RefBox::new(&large_object)), Infinite).unwrap(); - let decoded: Message<'static> = decode(&encoded[]).unwrap(); + let decoded: Message<'static> = decode(&encoded[..]).unwrap(); match decoded { Message::M1(b) => assert!(b.take().deref() == &large_object), @@ -290,7 +288,7 @@ fn test_refbox() { // Test 2 { let encoded = encode(&Message::M2(RefBox::new(&large_map)), Infinite).unwrap(); - let decoded: Message<'static> = decode(&encoded[]).unwrap(); + let decoded: Message<'static> = decode(&encoded[..]).unwrap(); match decoded { Message::M2(b) => assert!(b.take().deref() == &large_map), diff --git a/src/writer.rs b/src/writer.rs index fe0c3ac..de08ad7 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -1,11 +1,14 @@ use std::io::Write; use std::io::Error as IoError; +use std::io::ErrorKind as IoErrorKind; use std::error::Error; use std::num::Int; use std::fmt; use rustc_serialize::Encoder; + use byteorder::{BigEndian, WriteBytesExt}; +use byteorder::Error as ByteOrderError; pub type EncodingResult = Result; @@ -35,8 +38,14 @@ pub struct SizeChecker { pub written: u64 } -fn wrap_io(err: IoError) -> EncodingError { - EncodingError::IoError(err) +fn wrap_io(err: ByteOrderError) -> EncodingError { + match err { + ByteOrderError::Io(ioe) => EncodingError::IoError(ioe), + ByteOrderError::UnexpectedEOF => EncodingError::IoError( + IoError::new(IoErrorKind::Other, + "ByteOrder could not write to the buffer", + None)) + } } impl fmt::Display for EncodingError { @@ -140,13 +149,13 @@ impl<'a, W: Write> Encoder for EncoderWriter<'a, W> { } fn emit_char(&mut self, v: char) -> EncodingResult<()> { let mut cbuf = [0; 4]; - let sz = v.encode_utf8(&mut cbuf[]).unwrap_or(0); + let sz = v.encode_utf8(&mut cbuf[..]).unwrap_or(0); let ptr = &cbuf[..sz]; - self.writer.write_all(ptr).map_err(wrap_io) + self.writer.write_all(ptr).map_err(EncodingError::IoError) } fn emit_str(&mut self, v: &str) -> EncodingResult<()> { try!(self.emit_usize(v.len())); - self.writer.write_all(v.as_bytes()).map_err(wrap_io) + self.writer.write_all(v.as_bytes()).map_err(EncodingError::IoError) } fn emit_enum(&mut self, __: &str, f: F) -> EncodingResult<()> where F: FnOnce(&mut EncoderWriter<'a, W>) -> EncodingResult<()> {