Use new io, update for rustc changes

This commit is contained in:
Ty Overby 2015-02-21 21:42:59 -08:00
parent a38f8bc2c7
commit 5f85e9b19a
6 changed files with 44 additions and 28 deletions

View File

@ -12,4 +12,4 @@ description = "A binary serialization / deserialization strategy and implementat
[dependencies]
rustc-serialize = "*"
byteorder = "*"
byteorder = "0.2.9"

View File

@ -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);
}

View File

@ -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;

View File

@ -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<T> = Result<T, DecodingError>;
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) }

View File

@ -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<V>(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: &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::<usize>);
}
@ -193,7 +191,7 @@ fn unicode() {
#[test]
fn decoding_errors() {
fn is_invalid_encoding<T>(res: DecodingResult<T>) {
fn isize_invalid_encoding<T>(res: DecodingResult<T>) {
match res {
Ok(_) => panic!("Expecting error"),
Err(DecodingError::IoError(_)) => panic!("Expecting InvalidEncoding"),
@ -202,16 +200,16 @@ fn decoding_errors() {
}
}
is_invalid_encoding(decode::<bool>(vec![0xA].as_slice()));
is_invalid_encoding(decode::<String>(vec![0, 0, 0, 0, 0, 0, 0, 1, 0xFF].as_slice()));
isize_invalid_encoding(decode::<bool>(vec![0xA].as_slice()));
isize_invalid_encoding(decode::<String>(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::<Test>(vec![0, 0, 0, 5].as_slice()));
is_invalid_encoding(decode::<Option<u8>>(vec![5, 0].as_slice()));
isize_invalid_encoding(decode::<Test>(vec![0, 0, 0, 5].as_slice()));
isize_invalid_encoding(decode::<Option<u8>>(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),

View File

@ -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<T> = Result<T, EncodingError>;
@ -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<F>(&mut self, __: &str, f: F) -> EncodingResult<()> where
F: FnOnce(&mut EncoderWriter<'a, W>) -> EncodingResult<()> {