update for 1.0.0-alpha
This commit is contained in:
parent
d8b5c35a2e
commit
6fe6e8d251
|
|
@ -11,4 +11,4 @@ license = "MIT"
|
|||
description = "A binary serialization / deserialization strategy and implementation."
|
||||
|
||||
[dependencies]
|
||||
rustc-serialize = "0.2.6"
|
||||
rustc-serialize = "0.2.7"
|
||||
|
|
|
|||
|
|
@ -1,23 +0,0 @@
|
|||
extern crate bincode;
|
||||
extern crate serialize;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io::{Truncate, ReadWrite, File, BufferedReader};
|
||||
|
||||
use bincode::SizeLimit;
|
||||
|
||||
fn main() {
|
||||
let mut word_counts = HashMap::new();
|
||||
word_counts.insert("foo".to_string(), 3u);
|
||||
word_counts.insert("fizzbuzz".to_string(), 8u);
|
||||
|
||||
let file = File::open_mode(&Path::new("store.bin"), Truncate, ReadWrite);
|
||||
let mut file = file.unwrap();
|
||||
bincode::encode_into(&word_counts, &mut file, SizeLimit::Infinite).unwrap();
|
||||
file.fsync().unwrap();
|
||||
|
||||
let out: HashMap<String, uint> =
|
||||
bincode::decode_from(&mut BufferedReader::new(file), SizeLimit::Infinite).unwrap();
|
||||
|
||||
assert!(out == word_counts);
|
||||
}
|
||||
|
|
@ -2,6 +2,8 @@
|
|||
#![crate_type = "rlib"]
|
||||
#![crate_type = "dylib"]
|
||||
|
||||
#![allow(unstable)]
|
||||
|
||||
extern crate "rustc-serialize" as rustc_serialize;
|
||||
|
||||
use std::io::Buffer;
|
||||
|
|
|
|||
|
|
@ -88,8 +88,8 @@ impl<'a, R: Reader+Buffer> Decoder for DecoderReader<'a, R> {
|
|||
fn read_nil(&mut self) -> DecodingResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
fn read_uint(&mut self) -> DecodingResult<uint> {
|
||||
Ok(try!(self.read_u64().map(|x| x as uint)))
|
||||
fn read_usize(&mut self) -> DecodingResult<usize> {
|
||||
Ok(try!(self.read_u64().map(|x| x as usize)))
|
||||
}
|
||||
fn read_u64(&mut self) -> DecodingResult<u64> {
|
||||
try!(self.read_type::<u64>());
|
||||
|
|
@ -107,8 +107,8 @@ impl<'a, R: Reader+Buffer> Decoder for DecoderReader<'a, R> {
|
|||
try!(self.read_type::<u8>());
|
||||
self.reader.read_u8().map_err(wrap_io)
|
||||
}
|
||||
fn read_int(&mut self) -> DecodingResult<int> {
|
||||
self.read_i64().map(|x| x as int)
|
||||
fn read_isize(&mut self) -> DecodingResult<isize> {
|
||||
self.read_i64().map(|x| x as isize)
|
||||
}
|
||||
fn read_i64(&mut self) -> DecodingResult<i64> {
|
||||
try!(self.read_type::<i64>());
|
||||
|
|
@ -152,7 +152,7 @@ impl<'a, R: Reader+Buffer> Decoder for DecoderReader<'a, R> {
|
|||
|
||||
}
|
||||
fn read_str(&mut self) -> DecodingResult<String> {
|
||||
let len = try!(self.read_uint());
|
||||
let len = try!(self.read_usize());
|
||||
|
||||
try!(self.read_bytes(len));
|
||||
let vector = try!(self.reader.read_exact(len));
|
||||
|
|
@ -169,9 +169,9 @@ impl<'a, R: Reader+Buffer> Decoder for DecoderReader<'a, R> {
|
|||
f(self)
|
||||
}
|
||||
fn read_enum_variant<T, F>(&mut self, names: &[&str], mut f: F) -> DecodingResult<T> where
|
||||
F: FnMut(&mut DecoderReader<'a, R>, uint) -> DecodingResult<T> {
|
||||
F: FnMut(&mut DecoderReader<'a, R>, usize) -> DecodingResult<T> {
|
||||
let id = try!(self.read_u32());
|
||||
let id = id as uint;
|
||||
let id = id as usize;
|
||||
if id >= names.len() {
|
||||
Err(DecodingError::InvalidBytes(InvalidBytes {
|
||||
desc: "out of bounds tag when reading enum variant",
|
||||
|
|
@ -181,47 +181,47 @@ impl<'a, R: Reader+Buffer> Decoder for DecoderReader<'a, R> {
|
|||
f(self, id)
|
||||
}
|
||||
}
|
||||
fn read_enum_variant_arg<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
|
||||
fn read_enum_variant_arg<T, F>(&mut self, _: usize, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> DecodingResult<T> where
|
||||
F: FnMut(&mut DecoderReader<'a, R>, uint) -> DecodingResult<T> {
|
||||
F: FnMut(&mut DecoderReader<'a, R>, usize) -> DecodingResult<T> {
|
||||
self.read_enum_variant(names, f)
|
||||
}
|
||||
fn read_enum_struct_variant_field<T, F>(&mut self,
|
||||
_: &str,
|
||||
f_idx: uint,
|
||||
f_idx: usize,
|
||||
f: F)
|
||||
-> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
self.read_enum_variant_arg(f_idx, f)
|
||||
}
|
||||
fn read_struct<T, F>(&mut self, _: &str, _: uint, f: F) -> DecodingResult<T> where
|
||||
fn read_struct<T, F>(&mut self, _: &str, _: usize, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn read_struct_field<T, F>(&mut self,
|
||||
_: &str,
|
||||
_: uint,
|
||||
_: usize,
|
||||
f: F)
|
||||
-> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn read_tuple<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
|
||||
fn read_tuple<T, F>(&mut self, _: usize, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn read_tuple_arg<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
|
||||
fn read_tuple_arg<T, F>(&mut self, _: usize, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn read_tuple_struct<T, F>(&mut self, _: &str, len: uint, f: F) -> DecodingResult<T> where
|
||||
fn read_tuple_struct<T, F>(&mut self, _: &str, len: usize, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
self.read_tuple(len, f)
|
||||
}
|
||||
fn read_tuple_struct_arg<T, F>(&mut self, a_idx: uint, f: F) -> DecodingResult<T> where
|
||||
fn read_tuple_struct_arg<T, F>(&mut self, a_idx: usize, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
self.read_tuple_arg(a_idx, f)
|
||||
}
|
||||
|
|
@ -238,24 +238,24 @@ impl<'a, R: Reader+Buffer> Decoder for DecoderReader<'a, R> {
|
|||
}
|
||||
}
|
||||
fn read_seq<T, F>(&mut self, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>, uint) -> DecodingResult<T> {
|
||||
let len = try!(self.read_uint());
|
||||
F: FnOnce(&mut DecoderReader<'a, R>, usize) -> DecodingResult<T> {
|
||||
let len = try!(self.read_usize());
|
||||
f(self, len)
|
||||
}
|
||||
fn read_seq_elt<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
|
||||
fn read_seq_elt<T, F>(&mut self, _: usize, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn read_map<T, F>(&mut self, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>, uint) -> DecodingResult<T> {
|
||||
let len = try!(self.read_uint());
|
||||
F: FnOnce(&mut DecoderReader<'a, R>, usize) -> DecodingResult<T> {
|
||||
let len = try!(self.read_usize());
|
||||
f(self, len)
|
||||
}
|
||||
fn read_map_elt_key<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
|
||||
fn read_map_elt_key<T, F>(&mut self, _: usize, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
fn read_map_elt_val<T, F>(&mut self, _: uint, f: F) -> DecodingResult<T> where
|
||||
fn read_map_elt_val<T, F>(&mut self, _: usize, f: F) -> DecodingResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> DecodingResult<T> {
|
||||
f(self)
|
||||
}
|
||||
|
|
|
|||
36
src/test.rs
36
src/test.rs
|
|
@ -55,18 +55,18 @@ fn test_string() {
|
|||
|
||||
#[test]
|
||||
fn test_tuple() {
|
||||
the_same((1i,));
|
||||
the_same((1i,2i,3i));
|
||||
the_same((1i,"foo".to_string(),()));
|
||||
the_same((1is,));
|
||||
the_same((1is,2is,3is));
|
||||
the_same((1is,"foo".to_string(),()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_basic_struct() {
|
||||
#[derive(RustcEncodable, RustcDecodable, PartialEq, Show)]
|
||||
struct Easy {
|
||||
x: int,
|
||||
x: isize,
|
||||
s: String,
|
||||
y: uint
|
||||
y: usize
|
||||
}
|
||||
the_same(Easy{x: -4, s: "foo".to_string(), y: 10});
|
||||
}
|
||||
|
|
@ -75,14 +75,14 @@ fn test_basic_struct() {
|
|||
fn test_nested_struct() {
|
||||
#[derive(RustcEncodable, RustcDecodable, PartialEq, Show)]
|
||||
struct Easy {
|
||||
x: int,
|
||||
x: isize,
|
||||
s: String,
|
||||
y: uint
|
||||
y: usize
|
||||
}
|
||||
#[derive(RustcEncodable, RustcDecodable, PartialEq, Show)]
|
||||
struct Nest {
|
||||
f: Easy,
|
||||
b: uint,
|
||||
b: usize,
|
||||
s: Easy
|
||||
}
|
||||
|
||||
|
|
@ -96,16 +96,16 @@ fn test_nested_struct() {
|
|||
#[test]
|
||||
fn test_struct_tuple() {
|
||||
#[derive(RustcEncodable, RustcDecodable, PartialEq, Show)]
|
||||
struct TubStr(uint, String, f32);
|
||||
struct TubStr(usize, String, f32);
|
||||
|
||||
the_same(TubStr(5, "hello".to_string(), 3.2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn option() {
|
||||
the_same(Some(5u));
|
||||
the_same(Some(5us));
|
||||
the_same(Some("foo bar".to_string()));
|
||||
the_same(None::<uint>);
|
||||
the_same(None::<usize>);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -113,7 +113,7 @@ fn enm() {
|
|||
#[derive(RustcEncodable, RustcDecodable, PartialEq, Show)]
|
||||
enum TestEnum {
|
||||
NoArg,
|
||||
OneArg(uint),
|
||||
OneArg(usize),
|
||||
AnotherNoArg
|
||||
}
|
||||
the_same(TestEnum::NoArg);
|
||||
|
|
@ -127,9 +127,9 @@ fn struct_enum() {
|
|||
#[derive(RustcEncodable, RustcDecodable, PartialEq, Show)]
|
||||
enum TestEnum {
|
||||
NoArg,
|
||||
OneArg(uint),
|
||||
OneArg(usize),
|
||||
AnotherNoArg,
|
||||
StructLike{x: uint, y: f32}
|
||||
StructLike{x: usize, y: f32}
|
||||
}
|
||||
the_same(TestEnum::NoArg);
|
||||
the_same(TestEnum::OneArg(4));
|
||||
|
|
@ -143,15 +143,15 @@ fn struct_enum() {
|
|||
fn many() {
|
||||
let v: Vec<u8> = vec![];
|
||||
the_same(v);
|
||||
the_same(vec![1u]);
|
||||
the_same(vec![1u,2,3,4,5,6]);
|
||||
the_same(vec![1u64]);
|
||||
the_same(vec![1u64,2,3,4,5,6]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn map(){
|
||||
let mut m = HashMap::new();
|
||||
m.insert(4u, "foo".to_string());
|
||||
m.insert(0u, "bar".to_string());
|
||||
m.insert(4u64, "foo".to_string());
|
||||
m.insert(0u64, "bar".to_string());
|
||||
the_same(m);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ impl<'a, W: Writer> Encoder for EncoderWriter<'a, W> {
|
|||
type Error = IoError;
|
||||
|
||||
fn emit_nil(&mut self) -> EwResult { Ok(()) }
|
||||
fn emit_uint(&mut self, v: uint) -> EwResult {
|
||||
fn emit_usize(&mut self, v: usize) -> EwResult {
|
||||
self.emit_u64(v as u64)
|
||||
}
|
||||
fn emit_u64(&mut self, v: u64) -> EwResult {
|
||||
|
|
@ -40,7 +40,7 @@ impl<'a, W: Writer> Encoder for EncoderWriter<'a, W> {
|
|||
fn emit_u8(&mut self, v: u8) -> EwResult {
|
||||
self.writer.write_u8(v)
|
||||
}
|
||||
fn emit_int(&mut self, v: int) -> EwResult {
|
||||
fn emit_isize(&mut self, v: isize) -> EwResult {
|
||||
self.emit_i64(v as i64)
|
||||
}
|
||||
fn emit_i64(&mut self, v: i64) -> EwResult {
|
||||
|
|
@ -68,7 +68,7 @@ impl<'a, W: Writer> Encoder for EncoderWriter<'a, W> {
|
|||
self.writer.write_char(v)
|
||||
}
|
||||
fn emit_str(&mut self, v: &str) -> EwResult {
|
||||
try!(self.emit_uint(v.len()));
|
||||
try!(self.emit_usize(v.len()));
|
||||
self.writer.write_str(v)
|
||||
}
|
||||
fn emit_enum<F>(&mut self, __: &str, f: F) -> EwResult where
|
||||
|
|
@ -76,56 +76,56 @@ impl<'a, W: Writer> Encoder for EncoderWriter<'a, W> {
|
|||
f(self)
|
||||
}
|
||||
fn emit_enum_variant<F>(&mut self, _: &str,
|
||||
v_id: uint,
|
||||
_: uint,
|
||||
v_id: usize,
|
||||
_: usize,
|
||||
f: F) -> EwResult where
|
||||
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
let max_u32: u32 = Int::max_value();
|
||||
if v_id > (max_u32 as uint) {
|
||||
if v_id > (max_u32 as usize) {
|
||||
panic!("Variant tag doesn't fit in a u32")
|
||||
}
|
||||
try!(self.emit_u32(v_id as u32));
|
||||
f(self)
|
||||
}
|
||||
fn emit_enum_variant_arg<F>(&mut self, _: uint, f: F) -> EwResult where
|
||||
fn emit_enum_variant_arg<F>(&mut self, _: usize, f: F) -> EwResult where
|
||||
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
f(self)
|
||||
}
|
||||
fn emit_enum_struct_variant<F>(&mut self, _: &str,
|
||||
_: uint,
|
||||
_: uint,
|
||||
_: usize,
|
||||
_: usize,
|
||||
f: F) -> EwResult where
|
||||
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
f(self)
|
||||
}
|
||||
fn emit_enum_struct_variant_field<F>(&mut self,
|
||||
_: &str,
|
||||
_: uint,
|
||||
_: usize,
|
||||
f: F) -> EwResult where
|
||||
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
f(self)
|
||||
}
|
||||
fn emit_struct<F>(&mut self, _: &str, _: uint, f: F) -> EwResult where
|
||||
fn emit_struct<F>(&mut self, _: &str, _: usize, f: F) -> EwResult where
|
||||
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
f(self)
|
||||
}
|
||||
fn emit_struct_field<F>(&mut self, _: &str, _: uint, f: F) -> EwResult where
|
||||
fn emit_struct_field<F>(&mut self, _: &str, _: usize, f: F) -> EwResult where
|
||||
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
f(self)
|
||||
}
|
||||
fn emit_tuple<F>(&mut self, _: uint, f: F) -> EwResult where
|
||||
fn emit_tuple<F>(&mut self, _: usize, f: F) -> EwResult where
|
||||
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
f(self)
|
||||
}
|
||||
fn emit_tuple_arg<F>(&mut self, _: uint, f: F) -> EwResult where
|
||||
fn emit_tuple_arg<F>(&mut self, _: usize, f: F) -> EwResult where
|
||||
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
f(self)
|
||||
}
|
||||
fn emit_tuple_struct<F>(&mut self, _: &str, len: uint, f: F) -> EwResult where
|
||||
fn emit_tuple_struct<F>(&mut self, _: &str, len: usize, f: F) -> EwResult where
|
||||
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
self.emit_tuple(len, f)
|
||||
}
|
||||
fn emit_tuple_struct_arg<F>(&mut self, f_idx: uint, f: F) -> EwResult where
|
||||
fn emit_tuple_struct_arg<F>(&mut self, f_idx: usize, f: F) -> EwResult where
|
||||
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
self.emit_tuple_arg(f_idx, f)
|
||||
}
|
||||
|
|
@ -141,25 +141,25 @@ impl<'a, W: Writer> Encoder for EncoderWriter<'a, W> {
|
|||
try!(self.writer.write_u8(1));
|
||||
f(self)
|
||||
}
|
||||
fn emit_seq<F>(&mut self, len: uint, f: F) -> EwResult where
|
||||
fn emit_seq<F>(&mut self, len: usize, f: F) -> EwResult where
|
||||
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
try!(self.emit_uint(len));
|
||||
try!(self.emit_usize(len));
|
||||
f(self)
|
||||
}
|
||||
fn emit_seq_elt<F>(&mut self, _: uint, f: F) -> EwResult where
|
||||
fn emit_seq_elt<F>(&mut self, _: usize, f: F) -> EwResult where
|
||||
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
f(self)
|
||||
}
|
||||
fn emit_map<F>(&mut self, len: uint, f: F) -> EwResult where
|
||||
fn emit_map<F>(&mut self, len: usize, f: F) -> EwResult where
|
||||
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
try!(self.emit_uint(len));
|
||||
try!(self.emit_usize(len));
|
||||
f(self)
|
||||
}
|
||||
fn emit_map_elt_key<F>(&mut self, _: uint, mut f: F) -> EwResult where
|
||||
fn emit_map_elt_key<F>(&mut self, _: usize, mut f: F) -> EwResult where
|
||||
F: FnMut(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
f(self)
|
||||
}
|
||||
fn emit_map_elt_val<F>(&mut self, _: uint, f: F) -> EwResult where
|
||||
fn emit_map_elt_val<F>(&mut self, _: usize, f: F) -> EwResult where
|
||||
F: FnOnce(&mut EncoderWriter<'a, W>) -> EwResult {
|
||||
f(self)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue