Merge pull request #8 from crhino/rustc-62fb41c32

Update to newer rustc version
This commit is contained in:
Ty 2014-12-24 19:34:52 -06:00
commit ac6930c37d
6 changed files with 188 additions and 172 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "bincode" name = "bincode"
version = "0.0.2" version = "0.0.3"
authors = ["Ty Overby <ty@pre-alpha.com>"] authors = ["Ty Overby <ty@pre-alpha.com>"]
repository = "https://github.com/TyOverby/bincode" repository = "https://github.com/TyOverby/bincode"
@ -9,3 +9,6 @@ keywords = ["binary", "encode", "decode", "serialize", "deserialize"]
license = "MIT" license = "MIT"
description = "A binary serialization / deserialization strategy and implementation." description = "A binary serialization / deserialization strategy and implementation."
[dependencies]
rustc-serialize = "0.1.4"

View File

@ -1,13 +1,13 @@
extern crate bincode; extern crate bincode;
extern crate serialize; extern crate "rustc-serialize" as rustc_serialize;
#[deriving(Encodable, Decodable, PartialEq)] #[deriving(RustcEncodable, RustcDecodable, PartialEq)]
struct Entity { struct Entity {
x: f32, x: f32,
y: f32, y: f32,
} }
#[deriving(Encodable, Decodable, PartialEq)] #[deriving(RustcEncodable, RustcDecodable, PartialEq)]
struct World { struct World {
entities: Vec<Entity> entities: Vec<Entity>
} }

View File

@ -2,15 +2,15 @@
#![crate_type = "rlib"] #![crate_type = "rlib"]
#![crate_type = "dylib"] #![crate_type = "dylib"]
extern crate serialize; extern crate "rustc-serialize" as rustc_serialize;
use std::io::Buffer; use std::io::Buffer;
use std::io::MemWriter; use std::io::MemWriter;
use std::io::MemReader; use std::io::MemReader;
use std::io::IoError; use std::io::IoError;
use std::io::IoResult; use std::io::IoResult;
use serialize::Encodable; use rustc_serialize::Encodable;
use serialize::Decodable; use rustc_serialize::Decodable;
pub use writer::EncoderWriter; pub use writer::EncoderWriter;
pub use reader::DecoderReader; pub use reader::DecoderReader;
@ -21,7 +21,7 @@ mod reader;
pub fn encode<'a, T: Encodable<EncoderWriter<'a, MemWriter>, IoError>>(t: &T) ->IoResult<Vec<u8>> { pub fn encode<'a, T: Encodable<EncoderWriter<'a, MemWriter>, IoError>>(t: &T) ->IoResult<Vec<u8>> {
let mut w = MemWriter::new(); let mut w = MemWriter::new();
match encode_into(t, &mut w) { match encode_into(t, &mut w) {
Ok(()) => Ok(w.unwrap()), Ok(()) => Ok(w.into_inner()),
Err(e) => Err(e) Err(e) => Err(e)
} }
} }

View File

@ -1,5 +1,5 @@
use std::io::{Buffer, Reader, IoError, IoResult, OtherIoError}; use std::io::{Buffer, Reader, IoError, IoResult, OtherIoError};
use serialize::Decoder; use rustc_serialize::Decoder;
pub struct DecoderReader<'a, R: 'a> { pub struct DecoderReader<'a, R: 'a> {
reader: &'a mut R reader: &'a mut R
@ -70,83 +70,86 @@ impl<'a, R: Reader+Buffer> Decoder<IoError> for DecoderReader<'a, R> {
} }
Ok(String::from_utf8(vector).unwrap()) Ok(String::from_utf8(vector).unwrap())
} }
fn read_enum<T>(&mut self, _: &str, fn read_enum<T, F>(&mut self, _: &str, f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>| -> IoResult<T>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
f(self) f(self)
} }
fn read_enum_variant<T>(&mut self, _: &[&str], fn read_enum_variant<T, F>(&mut self, _: &[&str], mut f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>, uint| -> IoResult<T>) -> IoResult<T> { F: FnMut(&mut DecoderReader<'a, R>, uint) -> IoResult<T> {
let id = try!(self.read_uint()); let id = try!(self.read_uint());
f(self, id) f(self, id)
} }
fn read_enum_variant_arg<T>(&mut self, _: uint, fn read_enum_variant_arg<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>| -> IoResult<T>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
f(self) f(self)
} }
fn read_enum_struct_variant<T>(&mut self, names: &[&str], fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>, uint| -> IoResult<T>) -> IoResult<T> { F: FnMut(&mut DecoderReader<'a, R>, uint) -> IoResult<T> {
self.read_enum_variant(names, f) self.read_enum_variant(names, f)
} }
fn read_enum_struct_variant_field<T>(&mut self, _: &str, f_idx: uint, fn read_enum_struct_variant_field<T, F>(&mut self,
f: |&mut DecoderReader<'a, R>| -> IoResult<T>) -> IoResult<T> { _: &str,
f_idx: uint,
f: F)
-> IoResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
self.read_enum_variant_arg(f_idx, f) self.read_enum_variant_arg(f_idx, f)
} }
fn read_struct<T>(&mut self, _: &str, _: uint, fn read_struct<T, F>(&mut self, _: &str, _: uint, f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>| -> IoResult<T>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
f(self) f(self)
} }
fn read_struct_field<T>(&mut self, _: &str, _: uint, fn read_struct_field<T, F>(&mut self,
f: |&mut DecoderReader<'a, R>| -> IoResult<T>) -> IoResult<T> { _: &str,
_: uint,
f: F)
-> IoResult<T> where
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
f(self) f(self)
} }
fn read_tuple<T>(&mut self, _: uint, fn read_tuple<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>| -> IoResult<T>) -> F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
IoResult<T> {
f(self) f(self)
} }
fn read_tuple_arg<T>(&mut self, _: uint, fn read_tuple_arg<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>| -> IoResult<T>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
f(self) f(self)
} }
fn read_tuple_struct<T>(&mut self, _: &str, len: uint, fn read_tuple_struct<T, F>(&mut self, _: &str, len: uint, f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>| -> IoResult<T>) -> F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
IoResult<T> {
self.read_tuple(len, f) self.read_tuple(len, f)
} }
fn read_tuple_struct_arg<T>(&mut self, a_idx: uint, fn read_tuple_struct_arg<T, F>(&mut self, a_idx: uint, f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>| -> IoResult<T>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
self.read_tuple_arg(a_idx, f) self.read_tuple_arg(a_idx, f)
} }
fn read_option<T>(&mut self, fn read_option<T, F>(&mut self, mut f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>, bool| -> IoResult<T>) -> F: FnMut(&mut DecoderReader<'a, R>, bool) -> IoResult<T> {
IoResult<T> {
match try!(self.reader.read_u8()) { match try!(self.reader.read_u8()) {
1 => f(self, true), 1 => f(self, true),
_ => f(self, false) _ => f(self, false)
} }
} }
fn read_seq<T>(&mut self, fn read_seq<T, F>(&mut self, f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>, uint| -> IoResult<T>) -> F: FnOnce(&mut DecoderReader<'a, R>, uint) -> IoResult<T> {
IoResult<T> {
let len = try!(self.read_uint()); let len = try!(self.read_uint());
f(self, len) f(self, len)
} }
fn read_seq_elt<T>(&mut self, _: uint, fn read_seq_elt<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>| -> IoResult<T>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
f(self) f(self)
} }
fn read_map<T>(&mut self, fn read_map<T, F>(&mut self, f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>, uint| -> IoResult<T>) -> F: FnOnce(&mut DecoderReader<'a, R>, uint) -> IoResult<T> {
IoResult<T> {
let len = try!(self.read_uint()); let len = try!(self.read_uint());
f(self, len) f(self, len)
} }
fn read_map_elt_key<T>(&mut self, _: uint, fn read_map_elt_key<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>| -> IoResult<T>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
f(self) f(self)
} }
fn read_map_elt_val<T>(&mut self, _: uint, fn read_map_elt_val<T, F>(&mut self, _: uint, f: F) -> IoResult<T> where
f: |&mut DecoderReader<'a, R>| -> IoResult<T>) -> IoResult<T> { F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
f(self) f(self)
} }
fn error(&mut self, err: &str) -> IoError { fn error(&mut self, err: &str) -> IoError {

View File

@ -1,10 +1,12 @@
extern crate "rustc-serialize" as serialize;
use std::io::MemWriter; use std::io::MemWriter;
use std::fmt::Show; use std::fmt::Show;
use std::io::MemReader; use std::io::MemReader;
use std::io::IoError; use std::io::IoError;
use std::collections::HashMap; use std::collections::HashMap;
use serialize::{ use rustc_serialize::{
Encoder, Encoder,
Decoder, Decoder,
Encodable, Encodable,
@ -63,7 +65,7 @@ fn test_tuple() {
#[test] #[test]
fn test_basic_struct() { fn test_basic_struct() {
#[deriving(Encodable, Decodable, PartialEq, Show)] #[deriving(RustcEncodable, RustcDecodable, PartialEq, Show)]
struct Easy { struct Easy {
x: int, x: int,
s: String, s: String,
@ -74,13 +76,13 @@ fn test_basic_struct() {
#[test] #[test]
fn test_nested_struct() { fn test_nested_struct() {
#[deriving(Encodable, Decodable, PartialEq, Show)] #[deriving(RustcEncodable, RustcDecodable, PartialEq, Show)]
struct Easy { struct Easy {
x: int, x: int,
s: String, s: String,
y: uint y: uint
} }
#[deriving(Encodable, Decodable, PartialEq, Show)] #[deriving(RustcEncodable, RustcDecodable, PartialEq, Show)]
struct Nest { struct Nest {
f: Easy, f: Easy,
b: uint, b: uint,
@ -96,7 +98,7 @@ fn test_nested_struct() {
#[test] #[test]
fn test_struct_tuple() { fn test_struct_tuple() {
#[deriving(Encodable, Decodable, PartialEq, Show)] #[deriving(RustcEncodable, RustcDecodable, PartialEq, Show)]
struct TubStr(uint, String, f32); struct TubStr(uint, String, f32);
the_same(TubStr(5, "hello".to_string(), 3.2)); the_same(TubStr(5, "hello".to_string(), 3.2));
@ -111,7 +113,7 @@ fn option() {
#[test] #[test]
fn enm() { fn enm() {
#[deriving(Encodable, Decodable, PartialEq, Show)] #[deriving(RustcEncodable, RustcDecodable, PartialEq, Show)]
enum TestEnum { enum TestEnum {
NoArg, NoArg,
OneArg(uint), OneArg(uint),
@ -125,7 +127,7 @@ fn enm() {
#[test] #[test]
fn struct_enum() { fn struct_enum() {
#[deriving(Encodable, Decodable, PartialEq, Show)] #[deriving(RustcEncodable, RustcDecodable, PartialEq, Show)]
enum TestEnum { enum TestEnum {
NoArg, NoArg,
OneArg(uint), OneArg(uint),

View File

@ -1,5 +1,5 @@
use std::io::{Writer, IoError, IoResult}; use std::io::{Writer, IoError, IoResult};
use serialize::Encoder; use rustc_serialize::Encoder;
type EwResult = IoResult<()>; type EwResult = IoResult<()>;
@ -61,84 +61,92 @@ impl<'a, W: Writer> Encoder<IoError> for EncoderWriter<'a, W> {
try!(self.emit_uint(v.len())); try!(self.emit_uint(v.len()));
self.writer.write_str(v) self.writer.write_str(v)
} }
fn emit_enum(&mut self, _: &str, fn emit_enum<F>(&mut self, __: &str, f: F) -> EwResult where
f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
f(self) f(self)
} }
fn emit_enum_variant(&mut self, fn emit_enum_variant<F>(&mut self, _: &str,
_: &str, v_id: uint, _: uint, v_id: uint,
f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { _: uint,
f: F) -> EwResult where
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
try!(self.emit_uint(v_id)); try!(self.emit_uint(v_id));
f(self) f(self)
} }
fn emit_enum_variant_arg(&mut self, _: uint, fn emit_enum_variant_arg<F>(&mut self, _: uint, f: F) -> EwResult where
f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
f(self) f(self)
} }
fn emit_enum_struct_variant(&mut self, _: &str, _: uint, fn emit_enum_struct_variant<F>(&mut self, _: &str,
_: uint, f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { _: uint,
_: uint,
f: F) -> EwResult where
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
f(self) f(self)
} }
fn emit_enum_struct_variant_field(&mut self, _: &str, fn emit_enum_struct_variant_field<F>(&mut self,
_: uint, f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { _: &str,
_: uint,
f: F) -> EwResult where
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
f(self) f(self)
} }
fn emit_struct(&mut self, _: &str, _: uint, fn emit_struct<F>(&mut self, _: &str, _: uint, f: F) -> EwResult where
f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
f(self) f(self)
} }
fn emit_struct_field(&mut self, _: &str, _: uint, fn emit_struct_field<F>(&mut self, _: &str, _: uint, f: F) -> EwResult where
f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
f(self) f(self)
} }
fn emit_tuple(&mut self, _: uint, fn emit_tuple<F>(&mut self, _: uint, f: F) -> EwResult where
f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
f(self) f(self)
} }
fn emit_tuple_arg(&mut self, _: uint, fn emit_tuple_arg<F>(&mut self, _: uint, f: F) -> EwResult where
f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
f(self) f(self)
} }
fn emit_tuple_struct(&mut self, _: &str, len: uint, fn emit_tuple_struct<F>(&mut self, _: &str, len: uint, f: F) -> EwResult where
f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
self.emit_tuple(len, f) self.emit_tuple(len, f)
} }
fn emit_tuple_struct_arg(&mut self, f_idx: uint, fn emit_tuple_struct_arg<F>(&mut self, f_idx: uint, f: F) -> EwResult where
f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
self.emit_tuple_arg(f_idx, f) self.emit_tuple_arg(f_idx, f)
} }
fn emit_option(&mut self, fn emit_option<F>(&mut self, f: F) -> EwResult where
f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
f(self) f(self)
} }
fn emit_option_none(&mut self) -> EwResult { fn emit_option_none(&mut self) -> EwResult {
self.writer.write_u8(0) self.writer.write_u8(0)
} }
fn emit_option_some(&mut self, fn emit_option_some<F>(&mut self, f: F) -> EwResult where
f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
try!(self.writer.write_u8(1)); try!(self.writer.write_u8(1));
f(self) f(self)
} }
fn emit_seq(&mut self, len: uint, fn emit_seq<F>(&mut self, len: uint, f: F) -> EwResult where
f: |this: &mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
try!(self.emit_uint(len)); try!(self.emit_uint(len));
f(self) f(self)
} }
fn emit_seq_elt(&mut self, _: uint, fn emit_seq_elt<F>(&mut self, _: uint, f: F) -> EwResult where
f: |this: &mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
f(self) f(self)
} }
fn emit_map(&mut self, len: uint, fn emit_map<F>(&mut self, len: uint, f: F) -> EwResult where
f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
try!(self.emit_uint(len)); try!(self.emit_uint(len));
f(self) f(self)
} }
fn emit_map_elt_key(&mut self, _: uint, fn emit_map_elt_key<F>(&mut self, _: uint, mut f: F) -> EwResult where
f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnMut(&mut EncoderWriter<'a, W>) -> EwResult {
f(self) f(self)
} }
fn emit_map_elt_val(&mut self, _: uint, fn emit_map_elt_val<F>(&mut self, _: uint, f: F) -> EwResult where
f: |&mut EncoderWriter<'a, W>| -> EwResult) -> EwResult { F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
f(self) f(self)
} }
} }