mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-25 06:57:56 +00:00
Migrate over to cargo
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
use test::Bencher;
|
||||
|
||||
use serialize::Decodable;
|
||||
|
||||
use de::{Deserializable};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Show)]
|
||||
enum Error {
|
||||
EndOfStream,
|
||||
SyntaxError,
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
mod decoder {
|
||||
use std::vec;
|
||||
use serialize::Decoder;
|
||||
|
||||
use super::{Error, EndOfStream, SyntaxError};
|
||||
|
||||
pub struct BytesDecoder {
|
||||
iter: vec::MoveItems<u8>,
|
||||
}
|
||||
|
||||
impl BytesDecoder {
|
||||
#[inline]
|
||||
pub fn new(values: Vec<u8>) -> BytesDecoder {
|
||||
BytesDecoder {
|
||||
iter: values.move_iter()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Decoder<Error> for BytesDecoder {
|
||||
// Primitive types:
|
||||
fn read_nil(&mut self) -> Result<(), Error> { Err(SyntaxError) }
|
||||
fn read_uint(&mut self) -> Result<uint, Error> { Err(SyntaxError) }
|
||||
fn read_u64(&mut self) -> Result<u64, Error> { Err(SyntaxError) }
|
||||
fn read_u32(&mut self) -> Result<u32, Error> { Err(SyntaxError) }
|
||||
fn read_u16(&mut self) -> Result<u16, Error> { Err(SyntaxError) }
|
||||
#[inline]
|
||||
fn read_u8(&mut self) -> Result<u8, Error> {
|
||||
match self.iter.next() {
|
||||
Some(value) => Ok(value),
|
||||
None => Err(EndOfStream),
|
||||
}
|
||||
}
|
||||
fn read_int(&mut self) -> Result<int, Error> { Err(SyntaxError) }
|
||||
fn read_i64(&mut self) -> Result<i64, Error> { Err(SyntaxError) }
|
||||
fn read_i32(&mut self) -> Result<i32, Error> { Err(SyntaxError) }
|
||||
fn read_i16(&mut self) -> Result<i16, Error> { Err(SyntaxError) }
|
||||
fn read_i8(&mut self) -> Result<i8, Error> { Err(SyntaxError) }
|
||||
fn read_bool(&mut self) -> Result<bool, Error> { Err(SyntaxError) }
|
||||
fn read_f64(&mut self) -> Result<f64, Error> { Err(SyntaxError) }
|
||||
fn read_f32(&mut self) -> Result<f32, Error> { Err(SyntaxError) }
|
||||
fn read_char(&mut self) -> Result<char, Error> { Err(SyntaxError) }
|
||||
fn read_str(&mut self) -> Result<String, Error> { Err(SyntaxError) }
|
||||
|
||||
// Compound types:
|
||||
fn read_enum<T>(&mut self, _name: &str, _f: |&mut BytesDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_enum_variant<T>(&mut self,
|
||||
_names: &[&str],
|
||||
_f: |&mut BytesDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_enum_variant_arg<T>(&mut self,
|
||||
_a_idx: uint,
|
||||
_f: |&mut BytesDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_enum_struct_variant<T>(&mut self,
|
||||
_names: &[&str],
|
||||
_f: |&mut BytesDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_enum_struct_variant_field<T>(&mut self,
|
||||
_f_name: &str,
|
||||
_f_idx: uint,
|
||||
_f: |&mut BytesDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_struct<T>(&mut self, _s_name: &str, _len: uint, _f: |&mut BytesDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_struct_field<T>(&mut self,
|
||||
_f_name: &str,
|
||||
_f_idx: uint,
|
||||
_f: |&mut BytesDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_tuple<T>(&mut self, _f: |&mut BytesDecoder, uint| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_tuple_arg<T>(&mut self, _a_idx: uint, _f: |&mut BytesDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_tuple_struct<T>(&mut self,
|
||||
_s_name: &str,
|
||||
_f: |&mut BytesDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_tuple_struct_arg<T>(&mut self,
|
||||
_a_idx: uint,
|
||||
_f: |&mut BytesDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
// Specialized types:
|
||||
fn read_option<T>(&mut self, _f: |&mut BytesDecoder, bool| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
#[inline]
|
||||
fn read_seq<T>(&mut self, f: |&mut BytesDecoder, uint| -> Result<T, Error>) -> Result<T, Error> {
|
||||
f(self, 3)
|
||||
}
|
||||
#[inline]
|
||||
fn read_seq_elt<T>(&mut self, _idx: uint, f: |&mut BytesDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||
f(self)
|
||||
}
|
||||
|
||||
fn read_map<T>(&mut self, _f: |&mut BytesDecoder, uint| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_map_elt_key<T>(&mut self, _idx: uint, _f: |&mut BytesDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_map_elt_val<T>(&mut self, _idx: uint, _f: |&mut BytesDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
mod deserializer {
|
||||
use std::num;
|
||||
use std::vec;
|
||||
|
||||
use super::{Error, EndOfStream, SyntaxError};
|
||||
|
||||
use de::Deserializer;
|
||||
use de::{Token, Int, SeqStart, Sep, End};
|
||||
|
||||
#[deriving(Eq, Show)]
|
||||
enum State {
|
||||
StartState,
|
||||
SepOrEndState,
|
||||
EndState,
|
||||
}
|
||||
|
||||
pub struct BytesDeserializer {
|
||||
state: State,
|
||||
len: uint,
|
||||
iter: vec::MoveItems<u8>,
|
||||
}
|
||||
|
||||
impl BytesDeserializer {
|
||||
#[inline]
|
||||
pub fn new(values: Vec<int>) -> BytesDeserializer {
|
||||
BytesDeserializer {
|
||||
state: StartState,
|
||||
len: values.len(),
|
||||
iter: values.move_iter(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator<Result<Token, Error>> for BytesDeserializer {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Result<Token, Error>> {
|
||||
match self.state {
|
||||
StartState => {
|
||||
self.state = SepOrEndState;
|
||||
Some(Ok(SeqStart(self.len)))
|
||||
}
|
||||
SepOrEndState => {
|
||||
match self.iter.next() {
|
||||
Some(value) => {
|
||||
Some(Ok(Int(value)))
|
||||
}
|
||||
None => {
|
||||
self.state = EndState;
|
||||
Some(Ok(End))
|
||||
}
|
||||
}
|
||||
}
|
||||
EndState => {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserializer<Error> for BytesDeserializer {
|
||||
#[inline]
|
||||
fn end_of_stream_error<T>(&self) -> Result<T, Error> {
|
||||
Err(EndOfStream)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn syntax_error<T>(&self) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
fn run_bench_decoder(bytes: Vec<u8>) {
|
||||
let mut d = decoder::BytesDecoder::new(bytes.clone());
|
||||
let value: Vec<u8> = Decodable::decode(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, bytes);
|
||||
}
|
||||
|
||||
fn run_bench_deserializer(bytes: Vec<u8>) {
|
||||
let mut d = deserializer::BytesDeserializer::new(bytes.clone());
|
||||
let value: Vec<u8> = Deserializable::deserialize(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, bytes);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_bytes_decoder_empty(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
run_bench_decoder(vec!())
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_bytes_deserializer_empty(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
run_bench_deserializer(vec!())
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,311 @@
|
||||
use test::Bencher;
|
||||
|
||||
use serialize::{Decoder, Decodable};
|
||||
|
||||
use de::{Deserializer, Deserializable, Token};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Clone, PartialEq, Show, Decodable)]
|
||||
enum Animal {
|
||||
Dog,
|
||||
Frog(String, int)
|
||||
}
|
||||
|
||||
impl Deserializable for Animal {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>, E
|
||||
>(d: &mut D, token: Token) -> Result<Animal, E> {
|
||||
match try!(d.expect_enum_start(token, "Animal", ["Dog", "Frog"])) {
|
||||
0 => {
|
||||
try!(d.expect_enum_end());
|
||||
Ok(Dog)
|
||||
}
|
||||
1 => {
|
||||
let x0 = try!(Deserializable::deserialize(d));
|
||||
let x1 = try!(Deserializable::deserialize(d));
|
||||
|
||||
try!(d.expect_enum_end());
|
||||
|
||||
Ok(Frog(x0, x1))
|
||||
}
|
||||
_ => d.syntax_error(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Show)]
|
||||
enum Error {
|
||||
EndOfStream,
|
||||
SyntaxError,
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
mod decoder {
|
||||
use serialize::Decoder;
|
||||
|
||||
use super::{Animal, Dog, Frog, Error, SyntaxError};
|
||||
|
||||
enum State {
|
||||
AnimalState(Animal),
|
||||
DogState,
|
||||
FrogState,
|
||||
IntState(int),
|
||||
StringState(String),
|
||||
}
|
||||
|
||||
pub struct AnimalDecoder {
|
||||
stack: Vec<State>,
|
||||
|
||||
}
|
||||
|
||||
impl AnimalDecoder {
|
||||
#[inline]
|
||||
pub fn new(animal: Animal) -> AnimalDecoder {
|
||||
AnimalDecoder {
|
||||
stack: vec!(AnimalState(animal)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Decoder<Error> for AnimalDecoder {
|
||||
// Primitive types:
|
||||
fn read_nil(&mut self) -> Result<(), Error> { Err(SyntaxError) }
|
||||
fn read_uint(&mut self) -> Result<uint, Error> { Err(SyntaxError) }
|
||||
fn read_u64(&mut self) -> Result<u64, Error> { Err(SyntaxError) }
|
||||
fn read_u32(&mut self) -> Result<u32, Error> { Err(SyntaxError) }
|
||||
fn read_u16(&mut self) -> Result<u16, Error> { Err(SyntaxError) }
|
||||
fn read_u8(&mut self) -> Result<u8, Error> { Err(SyntaxError) }
|
||||
#[inline]
|
||||
fn read_int(&mut self) -> Result<int, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(IntState(x)) => Ok(x),
|
||||
_ => Err(SyntaxError),
|
||||
}
|
||||
}
|
||||
fn read_i64(&mut self) -> Result<i64, Error> { Err(SyntaxError) }
|
||||
fn read_i32(&mut self) -> Result<i32, Error> { Err(SyntaxError) }
|
||||
fn read_i16(&mut self) -> Result<i16, Error> { Err(SyntaxError) }
|
||||
fn read_i8(&mut self) -> Result<i8, Error> { Err(SyntaxError) }
|
||||
fn read_bool(&mut self) -> Result<bool, Error> { Err(SyntaxError) }
|
||||
fn read_f64(&mut self) -> Result<f64, Error> { Err(SyntaxError) }
|
||||
fn read_f32(&mut self) -> Result<f32, Error> { Err(SyntaxError) }
|
||||
fn read_char(&mut self) -> Result<char, Error> { Err(SyntaxError) }
|
||||
#[inline]
|
||||
fn read_str(&mut self) -> Result<String, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(StringState(x)) => Ok(x),
|
||||
_ => Err(SyntaxError),
|
||||
}
|
||||
}
|
||||
|
||||
// Compound types:
|
||||
#[inline]
|
||||
fn read_enum<T>(&mut self, name: &str, f: |&mut AnimalDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(AnimalState(animal)) => {
|
||||
self.stack.push(AnimalState(animal));
|
||||
if name == "Animal" {
|
||||
f(self)
|
||||
} else {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
_ => Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_enum_variant<T>(&mut self, names: &[&str], f: |&mut AnimalDecoder, uint| -> Result<T, Error>) -> Result<T, Error> {
|
||||
let name = match self.stack.pop() {
|
||||
Some(AnimalState(Dog)) => "Dog",
|
||||
Some(AnimalState(Frog(x0, x1))) => {
|
||||
self.stack.push(IntState(x1));
|
||||
self.stack.push(StringState(x0));
|
||||
"Frog"
|
||||
}
|
||||
_ => { return Err(SyntaxError); }
|
||||
};
|
||||
|
||||
let idx = match names.iter().position(|n| *n == name) {
|
||||
Some(idx) => idx,
|
||||
None => { return Err(SyntaxError); }
|
||||
};
|
||||
|
||||
f(self, idx)
|
||||
}
|
||||
#[inline]
|
||||
fn read_enum_variant_arg<T>(&mut self, _a_idx: uint, f: |&mut AnimalDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||
f(self)
|
||||
}
|
||||
fn read_enum_struct_variant<T>(&mut self,
|
||||
_names: &[&str],
|
||||
_f: |&mut AnimalDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_enum_struct_variant_field<T>(&mut self,
|
||||
_f_name: &str,
|
||||
_f_idx: uint,
|
||||
_f: |&mut AnimalDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_struct<T>(&mut self, _s_name: &str, _len: uint, _f: |&mut AnimalDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_struct_field<T>(&mut self,
|
||||
_f_name: &str,
|
||||
_f_idx: uint,
|
||||
_f: |&mut AnimalDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_tuple<T>(&mut self, _f: |&mut AnimalDecoder, uint| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_tuple_arg<T>(&mut self, _a_idx: uint, _f: |&mut AnimalDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_tuple_struct<T>(&mut self,
|
||||
_s_name: &str,
|
||||
_f: |&mut AnimalDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_tuple_struct_arg<T>(&mut self,
|
||||
_a_idx: uint,
|
||||
_f: |&mut AnimalDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
// Specialized types:
|
||||
fn read_option<T>(&mut self, _f: |&mut AnimalDecoder, bool| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
#[inline]
|
||||
fn read_seq<T>(&mut self, f: |&mut AnimalDecoder, uint| -> Result<T, Error>) -> Result<T, Error> {
|
||||
f(self, 3)
|
||||
}
|
||||
#[inline]
|
||||
fn read_seq_elt<T>(&mut self, _idx: uint, f: |&mut AnimalDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||
f(self)
|
||||
}
|
||||
|
||||
fn read_map<T>(&mut self, _f: |&mut AnimalDecoder, uint| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_map_elt_key<T>(&mut self, _idx: uint, _f: |&mut AnimalDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_map_elt_val<T>(&mut self, _idx: uint, _f: |&mut AnimalDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
mod deserializer {
|
||||
use super::{Animal, Dog, Frog, Error, EndOfStream, SyntaxError};
|
||||
|
||||
use de::Deserializer;
|
||||
use de::{Token, Int, String, EnumStart, End};
|
||||
|
||||
enum State {
|
||||
AnimalState(Animal),
|
||||
IntState(int),
|
||||
StringState(String),
|
||||
EndState,
|
||||
|
||||
}
|
||||
|
||||
pub struct AnimalDeserializer {
|
||||
stack: Vec<State>,
|
||||
}
|
||||
|
||||
impl AnimalDeserializer {
|
||||
#[inline]
|
||||
pub fn new(animal: Animal) -> AnimalDeserializer {
|
||||
AnimalDeserializer {
|
||||
stack: vec!(AnimalState(animal)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator<Result<Token, Error>> for AnimalDeserializer {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Result<Token, Error>> {
|
||||
match self.stack.pop() {
|
||||
Some(AnimalState(Dog)) => {
|
||||
self.stack.push(EndState);
|
||||
Some(Ok(EnumStart("Animal", "Dog", 0)))
|
||||
}
|
||||
Some(AnimalState(Frog(x0, x1))) => {
|
||||
self.stack.push(EndState);
|
||||
self.stack.push(IntState(x1));
|
||||
self.stack.push(StringState(x0));
|
||||
Some(Ok(EnumStart("Animal", "Frog", 2)))
|
||||
}
|
||||
Some(IntState(x)) => {
|
||||
Some(Ok(Int(x)))
|
||||
}
|
||||
Some(StringState(x)) => {
|
||||
Some(Ok(String(x)))
|
||||
}
|
||||
Some(EndState) => {
|
||||
Some(Ok(End))
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserializer<Error> for AnimalDeserializer {
|
||||
#[inline]
|
||||
fn end_of_stream_error<T>(&self) -> Result<T, Error> {
|
||||
Err(EndOfStream)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn syntax_error<T>(&self) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[bench]
|
||||
fn bench_decoder_dog(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let animal = Dog;
|
||||
|
||||
let mut d = decoder::AnimalDecoder::new(animal.clone());
|
||||
let value: Animal = Decodable::decode(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, animal);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_decoder_frog(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let animal = Frog("Henry".to_string(), 349);
|
||||
|
||||
let mut d = decoder::AnimalDecoder::new(animal.clone());
|
||||
let value: Animal = Decodable::decode(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, animal);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deserializer_dog(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let animal = Dog;
|
||||
|
||||
let mut d = deserializer::AnimalDeserializer::new(animal.clone());
|
||||
let value: Animal = Deserializable::deserialize(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, animal);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deserializer_frog(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let animal = Frog("Henry".to_string(), 349);
|
||||
|
||||
let mut d = deserializer::AnimalDeserializer::new(animal.clone());
|
||||
let value: Animal = Deserializable::deserialize(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, animal);
|
||||
})
|
||||
}
|
||||
+1318
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,316 @@
|
||||
use std::fmt::Show;
|
||||
use std::collections::HashMap;
|
||||
use test::Bencher;
|
||||
|
||||
use serialize::{Decoder, Decodable};
|
||||
|
||||
use de::{Deserializer, Deserializable};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Show)]
|
||||
enum Error {
|
||||
EndOfStream,
|
||||
SyntaxError,
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
mod decoder {
|
||||
use std::collections::HashMap;
|
||||
use std::collections::hashmap::MoveEntries;
|
||||
use serialize;
|
||||
|
||||
use super::{Error, EndOfStream, SyntaxError};
|
||||
|
||||
enum Value {
|
||||
StringValue(String),
|
||||
IntValue(int),
|
||||
}
|
||||
|
||||
pub struct IntDecoder {
|
||||
len: uint,
|
||||
iter: MoveEntries<String, int>,
|
||||
stack: Vec<Value>,
|
||||
}
|
||||
|
||||
impl IntDecoder {
|
||||
#[inline]
|
||||
pub fn new(values: HashMap<String, int>) -> IntDecoder {
|
||||
IntDecoder {
|
||||
len: values.len(),
|
||||
iter: values.move_iter(),
|
||||
stack: vec!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl serialize::Decoder<Error> for IntDecoder {
|
||||
// Primitive types:
|
||||
fn read_nil(&mut self) -> Result<(), Error> { Err(SyntaxError) }
|
||||
fn read_uint(&mut self) -> Result<uint, Error> { Err(SyntaxError) }
|
||||
fn read_u64(&mut self) -> Result<u64, Error> { Err(SyntaxError) }
|
||||
fn read_u32(&mut self) -> Result<u32, Error> { Err(SyntaxError) }
|
||||
fn read_u16(&mut self) -> Result<u16, Error> { Err(SyntaxError) }
|
||||
fn read_u8(&mut self) -> Result<u8, Error> { Err(SyntaxError) }
|
||||
#[inline]
|
||||
fn read_int(&mut self) -> Result<int, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(IntValue(x)) => Ok(x),
|
||||
Some(_) => Err(SyntaxError),
|
||||
None => Err(EndOfStream),
|
||||
}
|
||||
}
|
||||
fn read_i64(&mut self) -> Result<i64, Error> { Err(SyntaxError) }
|
||||
fn read_i32(&mut self) -> Result<i32, Error> { Err(SyntaxError) }
|
||||
fn read_i16(&mut self) -> Result<i16, Error> { Err(SyntaxError) }
|
||||
fn read_i8(&mut self) -> Result<i8, Error> { Err(SyntaxError) }
|
||||
fn read_bool(&mut self) -> Result<bool, Error> { Err(SyntaxError) }
|
||||
fn read_f64(&mut self) -> Result<f64, Error> { Err(SyntaxError) }
|
||||
fn read_f32(&mut self) -> Result<f32, Error> { Err(SyntaxError) }
|
||||
fn read_char(&mut self) -> Result<char, Error> { Err(SyntaxError) }
|
||||
#[inline]
|
||||
fn read_str(&mut self) -> Result<String, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(StringValue(x)) => Ok(x),
|
||||
Some(_) => Err(SyntaxError),
|
||||
None => Err(EndOfStream),
|
||||
}
|
||||
}
|
||||
|
||||
// Compound types:
|
||||
fn read_enum<T>(&mut self, _name: &str, _f: |&mut IntDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_enum_variant<T>(&mut self,
|
||||
_names: &[&str],
|
||||
_f: |&mut IntDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_enum_variant_arg<T>(&mut self,
|
||||
_a_idx: uint,
|
||||
_f: |&mut IntDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_enum_struct_variant<T>(&mut self,
|
||||
_names: &[&str],
|
||||
_f: |&mut IntDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_enum_struct_variant_field<T>(&mut self,
|
||||
_f_name: &str,
|
||||
_f_idx: uint,
|
||||
_f: |&mut IntDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_struct<T>(&mut self, _s_name: &str, _len: uint, _f: |&mut IntDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_struct_field<T>(&mut self,
|
||||
_f_name: &str,
|
||||
_f_idx: uint,
|
||||
_f: |&mut IntDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_tuple<T>(&mut self, _f: |&mut IntDecoder, uint| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_tuple_arg<T>(&mut self, _a_idx: uint, _f: |&mut IntDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_tuple_struct<T>(&mut self,
|
||||
_s_name: &str,
|
||||
_f: |&mut IntDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_tuple_struct_arg<T>(&mut self,
|
||||
_a_idx: uint,
|
||||
_f: |&mut IntDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
// Specialized types:
|
||||
fn read_option<T>(&mut self, _f: |&mut IntDecoder, bool| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_seq<T>(&mut self, _f: |&mut IntDecoder, uint| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_seq_elt<T>(&mut self, _idx: uint, _f: |&mut IntDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
#[inline]
|
||||
fn read_map<T>(&mut self, f: |&mut IntDecoder, uint| -> Result<T, Error>) -> Result<T, Error> {
|
||||
let len = self.len;
|
||||
f(self, len)
|
||||
}
|
||||
#[inline]
|
||||
fn read_map_elt_key<T>(&mut self, _idx: uint, f: |&mut IntDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||
match self.iter.next() {
|
||||
Some((key, value)) => {
|
||||
self.stack.push(IntValue(value));
|
||||
self.stack.push(StringValue(key));
|
||||
f(self)
|
||||
}
|
||||
None => {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn read_map_elt_val<T>(&mut self, _idx: uint, f: |&mut IntDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||
f(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
mod deserializer {
|
||||
use std::collections::HashMap;
|
||||
use std::collections::hashmap::MoveEntries;
|
||||
|
||||
use super::{Error, EndOfStream, SyntaxError};
|
||||
|
||||
use de;
|
||||
|
||||
#[deriving(PartialEq, Show)]
|
||||
enum State {
|
||||
StartState,
|
||||
KeyOrEndState,
|
||||
ValueState(int),
|
||||
EndState,
|
||||
}
|
||||
|
||||
pub struct IntDeserializer {
|
||||
stack: Vec<State>,
|
||||
len: uint,
|
||||
iter: MoveEntries<String, int>,
|
||||
}
|
||||
|
||||
impl IntDeserializer {
|
||||
#[inline]
|
||||
pub fn new(values: HashMap<String, int>) -> IntDeserializer {
|
||||
IntDeserializer {
|
||||
stack: vec!(StartState),
|
||||
len: values.len(),
|
||||
iter: values.move_iter(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator<Result<de::Token, Error>> for IntDeserializer {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Result<de::Token, Error>> {
|
||||
match self.stack.pop() {
|
||||
Some(StartState) => {
|
||||
self.stack.push(KeyOrEndState);
|
||||
Some(Ok(de::MapStart(self.len)))
|
||||
}
|
||||
Some(KeyOrEndState) => {
|
||||
match self.iter.next() {
|
||||
Some((key, value)) => {
|
||||
self.stack.push(ValueState(value));
|
||||
Some(Ok(de::String(key)))
|
||||
}
|
||||
None => {
|
||||
self.stack.push(EndState);
|
||||
Some(Ok(de::End))
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(ValueState(x)) => {
|
||||
self.stack.push(KeyOrEndState);
|
||||
Some(Ok(de::Int(x)))
|
||||
}
|
||||
Some(EndState) => {
|
||||
None
|
||||
}
|
||||
None => {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Deserializer<Error> for IntDeserializer {
|
||||
#[inline]
|
||||
fn end_of_stream_error<T>(&self) -> Result<T, Error> {
|
||||
Err(EndOfStream)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn syntax_error<T>(&self) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
fn run_decoder<
|
||||
E: Show,
|
||||
D: Decoder<E>,
|
||||
T: Clone + PartialEq + Show + Decodable<D, E>
|
||||
>(mut d: D, value: T) {
|
||||
let v: T = Decodable::decode(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, v);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_decoder_000(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let m: HashMap<String, int> = HashMap::new();
|
||||
run_decoder(decoder::IntDecoder::new(m.clone()), m)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_decoder_003(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut m: HashMap<String, int> = HashMap::new();
|
||||
for i in range(0i, 3) {
|
||||
m.insert(i.to_string(), i);
|
||||
}
|
||||
run_decoder(decoder::IntDecoder::new(m.clone()), m)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_decoder_100(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut m: HashMap<String, int> = HashMap::new();
|
||||
for i in range(0i, 100) {
|
||||
m.insert(i.to_string(), i);
|
||||
}
|
||||
run_decoder(decoder::IntDecoder::new(m.clone()), m)
|
||||
})
|
||||
}
|
||||
|
||||
fn run_deserializer<
|
||||
E: Show,
|
||||
D: Deserializer<E>,
|
||||
T: Clone + PartialEq + Show + Deserializable
|
||||
>(mut d: D, value: T) {
|
||||
let v: T = Deserializable::deserialize(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, v);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deserializer_000(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let m: HashMap<String, int> = HashMap::new();
|
||||
run_deserializer(deserializer::IntDeserializer::new(m.clone()), m)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deserializer_003(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut m: HashMap<String, int> = HashMap::new();
|
||||
for i in range(0i, 3) {
|
||||
m.insert(i.to_string(), i);
|
||||
}
|
||||
run_deserializer(deserializer::IntDeserializer::new(m.clone()), m)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deserializer_100(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut m: HashMap<String, int> = HashMap::new();
|
||||
for i in range(0i, 100) {
|
||||
m.insert(i.to_string(), i);
|
||||
}
|
||||
run_deserializer(deserializer::IntDeserializer::new(m.clone()), m)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,582 @@
|
||||
use std::collections::HashMap;
|
||||
use test::Bencher;
|
||||
|
||||
use serialize::{Decoder, Decodable};
|
||||
|
||||
use de;
|
||||
use de::{Token, Deserializer, Deserializable};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Clone, PartialEq, Show, Decodable)]
|
||||
struct Inner {
|
||||
a: (),
|
||||
b: uint,
|
||||
c: HashMap<String, Option<char>>,
|
||||
}
|
||||
|
||||
impl Deserializable for Inner {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<Inner, E> {
|
||||
match token {
|
||||
de::StructStart("Inner", _) | de::MapStart(_) => { }
|
||||
_ => { return d.syntax_error(); }
|
||||
}
|
||||
|
||||
let mut a = None;
|
||||
let mut b = None;
|
||||
let mut c = None;
|
||||
|
||||
loop {
|
||||
let token = match try!(d.expect_token()) {
|
||||
de::End => { break; }
|
||||
token => token,
|
||||
};
|
||||
|
||||
let name = match token {
|
||||
de::Str(name) => name,
|
||||
de::String(ref name) => name.as_slice(),
|
||||
_ => { return d.syntax_error(); }
|
||||
};
|
||||
|
||||
match name {
|
||||
"a" => {
|
||||
a = Some(try!(de::Deserializable::deserialize(d)));
|
||||
}
|
||||
"b" => {
|
||||
b = Some(try!(de::Deserializable::deserialize(d)));
|
||||
}
|
||||
"c" => {
|
||||
c = Some(try!(de::Deserializable::deserialize(d)));
|
||||
}
|
||||
_ => { }
|
||||
}
|
||||
}
|
||||
|
||||
match (a, b, c) {
|
||||
(Some(a), Some(b), Some(c)) => {
|
||||
Ok(Inner { a: a, b: b, c: c })
|
||||
}
|
||||
_ => d.syntax_error(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Clone, PartialEq, Show, Decodable)]
|
||||
struct Outer {
|
||||
inner: Vec<Inner>,
|
||||
}
|
||||
|
||||
impl Deserializable for Outer {
|
||||
#[inline]
|
||||
fn deserialize_token<
|
||||
D: Deserializer<E>,
|
||||
E
|
||||
>(d: &mut D, token: Token) -> Result<Outer, E> {
|
||||
match token {
|
||||
de::StructStart("Outer", _) | de::MapStart(_) => { }
|
||||
_ => { return d.syntax_error(); }
|
||||
}
|
||||
|
||||
let mut inner = None;
|
||||
|
||||
loop {
|
||||
let token = match try!(d.expect_token()) {
|
||||
de::End => { break; }
|
||||
token => token,
|
||||
};
|
||||
|
||||
let name = match token {
|
||||
de::Str(name) => name,
|
||||
de::String(ref name) => name.as_slice(),
|
||||
_ => { return d.syntax_error(); }
|
||||
};
|
||||
|
||||
match name {
|
||||
"inner" => {
|
||||
inner = Some(try!(de::Deserializable::deserialize(d)));
|
||||
}
|
||||
_ => { }
|
||||
}
|
||||
}
|
||||
|
||||
match inner {
|
||||
Some(inner) => {
|
||||
Ok(Outer { inner: inner })
|
||||
}
|
||||
_ => d.syntax_error(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Show)]
|
||||
enum Error {
|
||||
EndOfStream,
|
||||
SyntaxError,
|
||||
}
|
||||
|
||||
mod decoder {
|
||||
use std::collections::HashMap;
|
||||
use serialize::Decoder;
|
||||
|
||||
use super::{Outer, Inner, Error, SyntaxError};
|
||||
|
||||
#[deriving(Show)]
|
||||
enum State {
|
||||
OuterState(Outer),
|
||||
InnerState(Inner),
|
||||
NullState,
|
||||
UintState(uint),
|
||||
CharState(char),
|
||||
StringState(String),
|
||||
FieldState(&'static str),
|
||||
VecState(Vec<Inner>),
|
||||
MapState(HashMap<String, Option<char>>),
|
||||
OptionState(bool),
|
||||
}
|
||||
|
||||
pub struct OuterDecoder {
|
||||
stack: Vec<State>,
|
||||
|
||||
}
|
||||
|
||||
impl OuterDecoder {
|
||||
#[inline]
|
||||
pub fn new(animal: Outer) -> OuterDecoder {
|
||||
OuterDecoder {
|
||||
stack: vec!(OuterState(animal)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Decoder<Error> for OuterDecoder {
|
||||
// Primitive types:
|
||||
#[inline]
|
||||
fn read_nil(&mut self) -> Result<(), Error> {
|
||||
match self.stack.pop() {
|
||||
Some(NullState) => Ok(()),
|
||||
_ => Err(SyntaxError),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn read_uint(&mut self) -> Result<uint, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(UintState(value)) => Ok(value),
|
||||
_ => Err(SyntaxError),
|
||||
}
|
||||
}
|
||||
fn read_u64(&mut self) -> Result<u64, Error> { Err(SyntaxError) }
|
||||
fn read_u32(&mut self) -> Result<u32, Error> { Err(SyntaxError) }
|
||||
fn read_u16(&mut self) -> Result<u16, Error> { Err(SyntaxError) }
|
||||
fn read_u8(&mut self) -> Result<u8, Error> { Err(SyntaxError) }
|
||||
fn read_int(&mut self) -> Result<int, Error> { Err(SyntaxError) }
|
||||
fn read_i64(&mut self) -> Result<i64, Error> { Err(SyntaxError) }
|
||||
fn read_i32(&mut self) -> Result<i32, Error> { Err(SyntaxError) }
|
||||
fn read_i16(&mut self) -> Result<i16, Error> { Err(SyntaxError) }
|
||||
fn read_i8(&mut self) -> Result<i8, Error> { Err(SyntaxError) }
|
||||
fn read_bool(&mut self) -> Result<bool, Error> { Err(SyntaxError) }
|
||||
fn read_f64(&mut self) -> Result<f64, Error> { Err(SyntaxError) }
|
||||
fn read_f32(&mut self) -> Result<f32, Error> { Err(SyntaxError) }
|
||||
#[inline]
|
||||
fn read_char(&mut self) -> Result<char, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(CharState(c)) => Ok(c),
|
||||
_ => Err(SyntaxError),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn read_str(&mut self) -> Result<String, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(StringState(value)) => Ok(value),
|
||||
_ => Err(SyntaxError),
|
||||
}
|
||||
}
|
||||
|
||||
// Compound types:
|
||||
fn read_enum<T>(&mut self, _name: &str, _f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_enum_variant<T>(&mut self,
|
||||
_names: &[&str],
|
||||
_f: |&mut OuterDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_enum_variant_arg<T>(&mut self,
|
||||
_a_idx: uint,
|
||||
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_enum_struct_variant<T>(&mut self,
|
||||
_names: &[&str],
|
||||
_f: |&mut OuterDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_enum_struct_variant_field<T>(&mut self,
|
||||
_f_name: &str,
|
||||
_f_idx: uint,
|
||||
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
#[inline]
|
||||
fn read_struct<T>(&mut self, s_name: &str, _len: uint, f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(OuterState(Outer { inner: inner })) => {
|
||||
if s_name == "Outer" {
|
||||
self.stack.push(VecState(inner));
|
||||
self.stack.push(FieldState("inner"));
|
||||
f(self)
|
||||
} else {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
Some(InnerState(Inner { a: (), b: b, c: c })) => {
|
||||
if s_name == "Inner" {
|
||||
self.stack.push(MapState(c));
|
||||
self.stack.push(FieldState("c"));
|
||||
|
||||
self.stack.push(UintState(b));
|
||||
self.stack.push(FieldState("b"));
|
||||
|
||||
self.stack.push(NullState);
|
||||
self.stack.push(FieldState("a"));
|
||||
f(self)
|
||||
} else {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
_ => Err(SyntaxError),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn read_struct_field<T>(&mut self, f_name: &str, _f_idx: uint, f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(FieldState(name)) => {
|
||||
if f_name == name {
|
||||
f(self)
|
||||
} else {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
_ => Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
|
||||
fn read_tuple<T>(&mut self, _f: |&mut OuterDecoder, uint| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_tuple_arg<T>(&mut self, _a_idx: uint, _f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_tuple_struct<T>(&mut self,
|
||||
_s_name: &str,
|
||||
_f: |&mut OuterDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_tuple_struct_arg<T>(&mut self,
|
||||
_a_idx: uint,
|
||||
_f: |&mut OuterDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
// Specialized types:
|
||||
#[inline]
|
||||
fn read_option<T>(&mut self, f: |&mut OuterDecoder, bool| -> Result<T, Error>) -> Result<T, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(OptionState(b)) => f(self, b),
|
||||
_ => Err(SyntaxError),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_seq<T>(&mut self, f: |&mut OuterDecoder, uint| -> Result<T, Error>) -> Result<T, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(VecState(value)) => {
|
||||
let len = value.len();
|
||||
for inner in value.move_iter().rev() {
|
||||
self.stack.push(InnerState(inner));
|
||||
}
|
||||
f(self, len)
|
||||
}
|
||||
_ => Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn read_seq_elt<T>(&mut self, _idx: uint, f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||
f(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn read_map<T>(&mut self, f: |&mut OuterDecoder, uint| -> Result<T, Error>) -> Result<T, Error> {
|
||||
match self.stack.pop() {
|
||||
Some(MapState(map)) => {
|
||||
let len = map.len();
|
||||
for (key, value) in map.move_iter() {
|
||||
match value {
|
||||
Some(c) => {
|
||||
self.stack.push(CharState(c));
|
||||
self.stack.push(OptionState(true));
|
||||
}
|
||||
None => {
|
||||
self.stack.push(OptionState(false));
|
||||
}
|
||||
}
|
||||
self.stack.push(StringState(key));
|
||||
}
|
||||
f(self, len)
|
||||
}
|
||||
_ => Err(SyntaxError),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn read_map_elt_key<T>(&mut self, _idx: uint, f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||
f(self)
|
||||
}
|
||||
#[inline]
|
||||
fn read_map_elt_val<T>(&mut self, _idx: uint, f: |&mut OuterDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||
f(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
mod deserializer {
|
||||
use std::collections::HashMap;
|
||||
use super::{Outer, Inner, Error, EndOfStream, SyntaxError};
|
||||
use de::Deserializer;
|
||||
use de::{Token, Uint, Char, String, Null, TupleStart, StructStart, Str, SeqStart, MapStart, End, Option};
|
||||
|
||||
enum State {
|
||||
OuterState(Outer),
|
||||
InnerState(Inner),
|
||||
FieldState(&'static str),
|
||||
NullState,
|
||||
UintState(uint),
|
||||
CharState(char),
|
||||
StringState(String),
|
||||
OptionState(bool),
|
||||
TupleState(uint),
|
||||
VecState(Vec<Inner>),
|
||||
MapState(HashMap<String, Option<char>>),
|
||||
EndState,
|
||||
|
||||
}
|
||||
|
||||
pub struct OuterDeserializer {
|
||||
stack: Vec<State>,
|
||||
}
|
||||
|
||||
impl OuterDeserializer {
|
||||
#[inline]
|
||||
pub fn new(outer: Outer) -> OuterDeserializer {
|
||||
OuterDeserializer {
|
||||
stack: vec!(OuterState(outer)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator<Result<Token, Error>> for OuterDeserializer {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Result<Token, Error>> {
|
||||
match self.stack.pop() {
|
||||
Some(OuterState(Outer { inner })) => {
|
||||
self.stack.push(EndState);
|
||||
self.stack.push(VecState(inner));
|
||||
self.stack.push(FieldState("inner"));
|
||||
Some(Ok(StructStart("Outer", 1)))
|
||||
}
|
||||
Some(InnerState(Inner { a: (), b, c })) => {
|
||||
self.stack.push(EndState);
|
||||
self.stack.push(MapState(c));
|
||||
self.stack.push(FieldState("c"));
|
||||
|
||||
self.stack.push(UintState(b));
|
||||
self.stack.push(FieldState("b"));
|
||||
|
||||
self.stack.push(NullState);
|
||||
self.stack.push(FieldState("a"));
|
||||
Some(Ok(StructStart("Inner", 3)))
|
||||
}
|
||||
Some(FieldState(name)) => Some(Ok(Str(name))),
|
||||
Some(VecState(value)) => {
|
||||
self.stack.push(EndState);
|
||||
let len = value.len();
|
||||
for inner in value.move_iter().rev() {
|
||||
self.stack.push(InnerState(inner));
|
||||
}
|
||||
Some(Ok(SeqStart(len)))
|
||||
}
|
||||
Some(MapState(value)) => {
|
||||
self.stack.push(EndState);
|
||||
let len = value.len();
|
||||
for (key, value) in value.move_iter() {
|
||||
match value {
|
||||
Some(c) => {
|
||||
self.stack.push(CharState(c));
|
||||
self.stack.push(OptionState(true));
|
||||
}
|
||||
None => {
|
||||
self.stack.push(OptionState(false));
|
||||
}
|
||||
}
|
||||
self.stack.push(StringState(key));
|
||||
}
|
||||
Some(Ok(MapStart(len)))
|
||||
}
|
||||
Some(TupleState(len)) => Some(Ok(TupleStart(len))),
|
||||
Some(NullState) => Some(Ok(Null)),
|
||||
Some(UintState(x)) => Some(Ok(Uint(x))),
|
||||
Some(CharState(x)) => Some(Ok(Char(x))),
|
||||
Some(StringState(x)) => Some(Ok(String(x))),
|
||||
Some(OptionState(x)) => Some(Ok(Option(x))),
|
||||
Some(EndState) => {
|
||||
Some(Ok(End))
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserializer<Error> for OuterDeserializer {
|
||||
#[inline]
|
||||
fn end_of_stream_error<T>(&self) -> Result<T, Error> {
|
||||
Err(EndOfStream)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn syntax_error<T>(&self) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_decoder_0_0(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("abc".to_string(), Some('c'));
|
||||
|
||||
let outer = Outer {
|
||||
inner: vec!(),
|
||||
};
|
||||
|
||||
let mut d = decoder::OuterDecoder::new(outer.clone());
|
||||
let value: Outer = Decodable::decode(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, outer);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_decoder_1_0(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let map = HashMap::new();
|
||||
|
||||
let outer = Outer {
|
||||
inner: vec!(
|
||||
Inner {
|
||||
a: (),
|
||||
b: 5,
|
||||
c: map,
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
let mut d = decoder::OuterDecoder::new(outer.clone());
|
||||
let value: Outer = Decodable::decode(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, outer);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_decoder_1_5(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("1".to_string(), Some('a'));
|
||||
map.insert("2".to_string(), None);
|
||||
map.insert("3".to_string(), Some('b'));
|
||||
map.insert("4".to_string(), None);
|
||||
map.insert("5".to_string(), Some('c'));
|
||||
|
||||
let outer = Outer {
|
||||
inner: vec!(
|
||||
Inner {
|
||||
a: (),
|
||||
b: 5,
|
||||
c: map,
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
let mut d = decoder::OuterDecoder::new(outer.clone());
|
||||
let value: Outer = Decodable::decode(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, outer);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deserializer_0_0(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("abc".to_string(), Some('c'));
|
||||
|
||||
let outer = Outer {
|
||||
inner: vec!(),
|
||||
};
|
||||
|
||||
let mut d = deserializer::OuterDeserializer::new(outer.clone());
|
||||
let value: Outer = Deserializable::deserialize(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, outer);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deserializer_1_0(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let map = HashMap::new();
|
||||
|
||||
let outer = Outer {
|
||||
inner: vec!(
|
||||
Inner {
|
||||
a: (),
|
||||
b: 5,
|
||||
c: map,
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
let mut d = deserializer::OuterDeserializer::new(outer.clone());
|
||||
let value: Outer = Deserializable::deserialize(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, outer);
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deserializer_1_5(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("1".to_string(), Some('a'));
|
||||
map.insert("2".to_string(), None);
|
||||
map.insert("3".to_string(), Some('b'));
|
||||
map.insert("4".to_string(), None);
|
||||
map.insert("5".to_string(), Some('c'));
|
||||
|
||||
let outer = Outer {
|
||||
inner: vec!(
|
||||
Inner {
|
||||
a: (),
|
||||
b: 5,
|
||||
c: map,
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
let mut d = deserializer::OuterDeserializer::new(outer.clone());
|
||||
let value: Outer = Deserializable::deserialize(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, outer);
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,471 @@
|
||||
use std::fmt::Show;
|
||||
use test::Bencher;
|
||||
|
||||
use serialize::{Decoder, Decodable};
|
||||
|
||||
use de::{Deserializer, Deserializable};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Show)]
|
||||
enum Error {
|
||||
EndOfStream,
|
||||
SyntaxError,
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
mod decoder {
|
||||
use std::vec;
|
||||
use serialize;
|
||||
|
||||
use super::{Error, EndOfStream, SyntaxError};
|
||||
|
||||
pub struct IntDecoder {
|
||||
len: uint,
|
||||
iter: vec::MoveItems<int>,
|
||||
}
|
||||
|
||||
impl IntDecoder {
|
||||
#[inline]
|
||||
pub fn new(values: Vec<int>) -> IntDecoder {
|
||||
IntDecoder {
|
||||
len: values.len(),
|
||||
iter: values.move_iter(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl serialize::Decoder<Error> for IntDecoder {
|
||||
// Primitive types:
|
||||
fn read_nil(&mut self) -> Result<(), Error> { Err(SyntaxError) }
|
||||
fn read_uint(&mut self) -> Result<uint, Error> { Err(SyntaxError) }
|
||||
fn read_u64(&mut self) -> Result<u64, Error> { Err(SyntaxError) }
|
||||
fn read_u32(&mut self) -> Result<u32, Error> { Err(SyntaxError) }
|
||||
fn read_u16(&mut self) -> Result<u16, Error> { Err(SyntaxError) }
|
||||
fn read_u8(&mut self) -> Result<u8, Error> { Err(SyntaxError) }
|
||||
#[inline]
|
||||
fn read_int(&mut self) -> Result<int, Error> {
|
||||
match self.iter.next() {
|
||||
Some(value) => Ok(value),
|
||||
None => Err(EndOfStream),
|
||||
}
|
||||
}
|
||||
fn read_i64(&mut self) -> Result<i64, Error> { Err(SyntaxError) }
|
||||
fn read_i32(&mut self) -> Result<i32, Error> { Err(SyntaxError) }
|
||||
fn read_i16(&mut self) -> Result<i16, Error> { Err(SyntaxError) }
|
||||
fn read_i8(&mut self) -> Result<i8, Error> { Err(SyntaxError) }
|
||||
fn read_bool(&mut self) -> Result<bool, Error> { Err(SyntaxError) }
|
||||
fn read_f64(&mut self) -> Result<f64, Error> { Err(SyntaxError) }
|
||||
fn read_f32(&mut self) -> Result<f32, Error> { Err(SyntaxError) }
|
||||
fn read_char(&mut self) -> Result<char, Error> { Err(SyntaxError) }
|
||||
fn read_str(&mut self) -> Result<String, Error> { Err(SyntaxError) }
|
||||
|
||||
// Compound types:
|
||||
fn read_enum<T>(&mut self, _name: &str, _f: |&mut IntDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_enum_variant<T>(&mut self,
|
||||
_names: &[&str],
|
||||
_f: |&mut IntDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_enum_variant_arg<T>(&mut self,
|
||||
_a_idx: uint,
|
||||
_f: |&mut IntDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_enum_struct_variant<T>(&mut self,
|
||||
_names: &[&str],
|
||||
_f: |&mut IntDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_enum_struct_variant_field<T>(&mut self,
|
||||
_f_name: &str,
|
||||
_f_idx: uint,
|
||||
_f: |&mut IntDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_struct<T>(&mut self, _s_name: &str, _len: uint, _f: |&mut IntDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_struct_field<T>(&mut self,
|
||||
_f_name: &str,
|
||||
_f_idx: uint,
|
||||
_f: |&mut IntDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_tuple<T>(&mut self, _f: |&mut IntDecoder, uint| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_tuple_arg<T>(&mut self, _a_idx: uint, _f: |&mut IntDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_tuple_struct<T>(&mut self,
|
||||
_s_name: &str,
|
||||
_f: |&mut IntDecoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_tuple_struct_arg<T>(&mut self,
|
||||
_a_idx: uint,
|
||||
_f: |&mut IntDecoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
// Specialized types:
|
||||
fn read_option<T>(&mut self, _f: |&mut IntDecoder, bool| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
#[inline]
|
||||
fn read_seq<T>(&mut self, f: |&mut IntDecoder, uint| -> Result<T, Error>) -> Result<T, Error> {
|
||||
let len = self.len;
|
||||
f(self, len)
|
||||
}
|
||||
#[inline]
|
||||
fn read_seq_elt<T>(&mut self, _idx: uint, f: |&mut IntDecoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||
f(self)
|
||||
}
|
||||
|
||||
fn read_map<T>(&mut self, _f: |&mut IntDecoder, uint| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_map_elt_key<T>(&mut self, _idx: uint, _f: |&mut IntDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_map_elt_val<T>(&mut self, _idx: uint, _f: |&mut IntDecoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
}
|
||||
|
||||
|
||||
pub struct U8Decoder {
|
||||
len: uint,
|
||||
iter: vec::MoveItems<u8>,
|
||||
}
|
||||
|
||||
impl U8Decoder {
|
||||
#[inline]
|
||||
pub fn new(values: Vec<u8>) -> U8Decoder {
|
||||
U8Decoder {
|
||||
len: values.len(),
|
||||
iter: values.move_iter(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl serialize::Decoder<Error> for U8Decoder {
|
||||
// Primitive types:
|
||||
fn read_nil(&mut self) -> Result<(), Error> { Err(SyntaxError) }
|
||||
fn read_uint(&mut self) -> Result<uint, Error> { Err(SyntaxError) }
|
||||
fn read_u64(&mut self) -> Result<u64, Error> { Err(SyntaxError) }
|
||||
fn read_u32(&mut self) -> Result<u32, Error> { Err(SyntaxError) }
|
||||
fn read_u16(&mut self) -> Result<u16, Error> { Err(SyntaxError) }
|
||||
#[inline]
|
||||
fn read_u8(&mut self) -> Result<u8, Error> {
|
||||
match self.iter.next() {
|
||||
Some(value) => Ok(value),
|
||||
None => Err(EndOfStream),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn read_int(&mut self) -> Result<int, Error> { Err(SyntaxError) }
|
||||
fn read_i64(&mut self) -> Result<i64, Error> { Err(SyntaxError) }
|
||||
fn read_i32(&mut self) -> Result<i32, Error> { Err(SyntaxError) }
|
||||
fn read_i16(&mut self) -> Result<i16, Error> { Err(SyntaxError) }
|
||||
fn read_i8(&mut self) -> Result<i8, Error> { Err(SyntaxError) }
|
||||
fn read_bool(&mut self) -> Result<bool, Error> { Err(SyntaxError) }
|
||||
fn read_f64(&mut self) -> Result<f64, Error> { Err(SyntaxError) }
|
||||
fn read_f32(&mut self) -> Result<f32, Error> { Err(SyntaxError) }
|
||||
fn read_char(&mut self) -> Result<char, Error> { Err(SyntaxError) }
|
||||
fn read_str(&mut self) -> Result<String, Error> { Err(SyntaxError) }
|
||||
|
||||
// Compound types:
|
||||
fn read_enum<T>(&mut self, _name: &str, _f: |&mut U8Decoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_enum_variant<T>(&mut self,
|
||||
_names: &[&str],
|
||||
_f: |&mut U8Decoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_enum_variant_arg<T>(&mut self,
|
||||
_a_idx: uint,
|
||||
_f: |&mut U8Decoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_enum_struct_variant<T>(&mut self,
|
||||
_names: &[&str],
|
||||
_f: |&mut U8Decoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_enum_struct_variant_field<T>(&mut self,
|
||||
_f_name: &str,
|
||||
_f_idx: uint,
|
||||
_f: |&mut U8Decoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_struct<T>(&mut self, _s_name: &str, _len: uint, _f: |&mut U8Decoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_struct_field<T>(&mut self,
|
||||
_f_name: &str,
|
||||
_f_idx: uint,
|
||||
_f: |&mut U8Decoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_tuple<T>(&mut self, _f: |&mut U8Decoder, uint| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_tuple_arg<T>(&mut self, _a_idx: uint, _f: |&mut U8Decoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
fn read_tuple_struct<T>(&mut self,
|
||||
_s_name: &str,
|
||||
_f: |&mut U8Decoder, uint| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_tuple_struct_arg<T>(&mut self,
|
||||
_a_idx: uint,
|
||||
_f: |&mut U8Decoder| -> Result<T, Error>)
|
||||
-> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
// Specialized types:
|
||||
fn read_option<T>(&mut self, _f: |&mut U8Decoder, bool| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
|
||||
#[inline]
|
||||
fn read_seq<T>(&mut self, f: |&mut U8Decoder, uint| -> Result<T, Error>) -> Result<T, Error> {
|
||||
let len = self.len;
|
||||
f(self, len)
|
||||
}
|
||||
#[inline]
|
||||
fn read_seq_elt<T>(&mut self, _idx: uint, f: |&mut U8Decoder| -> Result<T, Error>) -> Result<T, Error> {
|
||||
f(self)
|
||||
}
|
||||
|
||||
fn read_map<T>(&mut self, _f: |&mut U8Decoder, uint| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_map_elt_key<T>(&mut self, _idx: uint, _f: |&mut U8Decoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
fn read_map_elt_val<T>(&mut self, _idx: uint, _f: |&mut U8Decoder| -> Result<T, Error>) -> Result<T, Error> { Err(SyntaxError) }
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
mod deserializer {
|
||||
//use std::num;
|
||||
use std::vec;
|
||||
|
||||
use super::{Error, EndOfStream, SyntaxError};
|
||||
|
||||
use de;
|
||||
|
||||
#[deriving(PartialEq, Show)]
|
||||
enum State {
|
||||
StartState,
|
||||
SepOrEndState,
|
||||
EndState,
|
||||
}
|
||||
|
||||
pub struct IntDeserializer {
|
||||
state: State,
|
||||
len: uint,
|
||||
iter: vec::MoveItems<int>,
|
||||
}
|
||||
|
||||
impl IntDeserializer {
|
||||
#[inline]
|
||||
pub fn new(values: Vec<int>) -> IntDeserializer {
|
||||
IntDeserializer {
|
||||
state: StartState,
|
||||
len: values.len(),
|
||||
iter: values.move_iter(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator<Result<de::Token, Error>> for IntDeserializer {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Result<de::Token, Error>> {
|
||||
match self.state {
|
||||
StartState => {
|
||||
self.state = SepOrEndState;
|
||||
Some(Ok(de::SeqStart(self.len)))
|
||||
}
|
||||
SepOrEndState => {
|
||||
match self.iter.next() {
|
||||
Some(value) => {
|
||||
Some(Ok(de::Int(value)))
|
||||
}
|
||||
None => {
|
||||
self.state = EndState;
|
||||
Some(Ok(de::End))
|
||||
}
|
||||
}
|
||||
}
|
||||
EndState => {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Deserializer<Error> for IntDeserializer {
|
||||
#[inline]
|
||||
fn end_of_stream_error<T>(&self) -> Result<T, Error> {
|
||||
Err(EndOfStream)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn syntax_error<T>(&self) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct U8Deserializer {
|
||||
state: State,
|
||||
len: uint,
|
||||
iter: vec::MoveItems<u8>,
|
||||
}
|
||||
|
||||
impl U8Deserializer {
|
||||
#[inline]
|
||||
pub fn new(values: Vec<u8>) -> U8Deserializer {
|
||||
U8Deserializer {
|
||||
state: StartState,
|
||||
len: values.len(),
|
||||
iter: values.move_iter(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator<Result<de::Token, Error>> for U8Deserializer {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Result<de::Token, Error>> {
|
||||
match self.state {
|
||||
StartState => {
|
||||
self.state = SepOrEndState;
|
||||
Some(Ok(de::SeqStart(self.len)))
|
||||
}
|
||||
SepOrEndState => {
|
||||
match self.iter.next() {
|
||||
Some(value) => {
|
||||
Some(Ok(de::U8(value)))
|
||||
}
|
||||
None => {
|
||||
self.state = EndState;
|
||||
Some(Ok(de::End))
|
||||
}
|
||||
}
|
||||
}
|
||||
EndState => {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Deserializer<Error> for U8Deserializer {
|
||||
#[inline]
|
||||
fn end_of_stream_error<T>(&self) -> Result<T, Error> {
|
||||
Err(EndOfStream)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn syntax_error<T>(&self) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
fn run_decoder<
|
||||
E: Show,
|
||||
D: Decoder<E>,
|
||||
T: Clone + PartialEq + Show + Decodable<D, E>
|
||||
>(mut d: D, value: T) {
|
||||
let v: T = Decodable::decode(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, v);
|
||||
}
|
||||
|
||||
fn run_deserializer<
|
||||
E: Show,
|
||||
D: Deserializer<E>,
|
||||
T: Clone + PartialEq + Show + Deserializable
|
||||
>(mut d: D, value: T) {
|
||||
let v: T = Deserializable::deserialize(&mut d).unwrap();
|
||||
|
||||
assert_eq!(value, v);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_decoder_int_000(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let v: Vec<int> = vec!();
|
||||
run_decoder(decoder::IntDecoder::new(v.clone()), v)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_decoder_int_003(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let v: Vec<int> = vec!(1, 2, 3);
|
||||
run_decoder(decoder::IntDecoder::new(v.clone()), v)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_decoder_int_100(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let v: Vec<int> = range(0i, 100).collect();
|
||||
run_decoder(decoder::IntDecoder::new(v.clone()), v)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_decoder_u8_000(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let v: Vec<u8> = vec!();
|
||||
run_decoder(decoder::U8Decoder::new(v.clone()), v)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_decoder_u8_003(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let v: Vec<u8> = vec!(1, 2, 3);
|
||||
run_decoder(decoder::U8Decoder::new(v.clone()), v)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_decoder_u8_100(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let v: Vec<u8> = range(0u8, 100).collect();
|
||||
run_decoder(decoder::U8Decoder::new(v.clone()), v)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deserializer_int_000(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let v: Vec<int> = vec!();
|
||||
run_deserializer(deserializer::IntDeserializer::new(v.clone()), v)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deserializer_int_003(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let v: Vec<int> = vec!(1, 2, 3);
|
||||
run_deserializer(deserializer::IntDeserializer::new(v.clone()), v)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deserializer_int_100(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let v: Vec<int> = range(0i, 100).collect();
|
||||
run_deserializer(deserializer::IntDeserializer::new(v.clone()), v)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deserializer_u8_000(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let v: Vec<u8> = vec!();
|
||||
run_deserializer(deserializer::U8Deserializer::new(v.clone()), v)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deserializer_u8_003(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let v: Vec<u8> = vec!(1, 2, 3);
|
||||
run_deserializer(deserializer::U8Deserializer::new(v.clone()), v)
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_deserializer_u8_100(b: &mut Bencher) {
|
||||
b.iter(|| {
|
||||
let v: Vec<u8> = range(0u8, 100).collect();
|
||||
run_deserializer(deserializer::U8Deserializer::new(v.clone()), v)
|
||||
})
|
||||
}
|
||||
+3987
File diff suppressed because it is too large
Load Diff
+47
@@ -0,0 +1,47 @@
|
||||
#![feature(macro_rules, phase)]
|
||||
#![crate_type = "dylib"]
|
||||
#![crate_type = "rlib"]
|
||||
|
||||
// test harness access
|
||||
#[cfg(test)]
|
||||
extern crate test;
|
||||
|
||||
#[phase(plugin, link)]
|
||||
extern crate log;
|
||||
|
||||
#[phase(plugin)]
|
||||
extern crate serde_macros;
|
||||
|
||||
#[cfg(test)]
|
||||
extern crate debug;
|
||||
|
||||
#[cfg(test)]
|
||||
extern crate serialize;
|
||||
|
||||
pub mod de;
|
||||
pub mod ser;
|
||||
pub mod json;
|
||||
|
||||
//#[cfg(test)]
|
||||
//pub mod bench_bytes;
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod bench_enum;
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod bench_struct;
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod bench_vec;
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod bench_map;
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod bench_log;
|
||||
|
||||
// an inner module so we can use serde_macros.
|
||||
mod serde {
|
||||
pub use de;
|
||||
pub use ser;
|
||||
}
|
||||
+797
@@ -0,0 +1,797 @@
|
||||
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use std::collections::{HashMap, TreeMap};
|
||||
use std::hash::Hash;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Serializer<E> {
|
||||
fn serialize_null(&mut self) -> Result<(), E>;
|
||||
|
||||
fn serialize_bool(&mut self, v: bool) -> Result<(), E>;
|
||||
|
||||
#[inline]
|
||||
fn serialize_int(&mut self, v: int) -> Result<(), E> {
|
||||
self.serialize_i64(v as i64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i8(&mut self, v: i8) -> Result<(), E> {
|
||||
self.serialize_i64(v as i64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i16(&mut self, v: i16) -> Result<(), E> {
|
||||
self.serialize_i64(v as i64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i32(&mut self, v: i32) -> Result<(), E> {
|
||||
self.serialize_i64(v as i64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_i64(&mut self, v: i64) -> Result<(), E>;
|
||||
|
||||
#[inline]
|
||||
fn serialize_uint(&mut self, v: uint) -> Result<(), E> {
|
||||
self.serialize_u64(v as u64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u8(&mut self, v: u8) -> Result<(), E> {
|
||||
self.serialize_u64(v as u64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u16(&mut self, v: u16) -> Result<(), E> {
|
||||
self.serialize_u64(v as u64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u32(&mut self, v: u32) -> Result<(), E> {
|
||||
self.serialize_u64(v as u64)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_u64(&mut self, v: u64) -> Result<(), E>;
|
||||
|
||||
#[inline]
|
||||
fn serialize_f32(&mut self, v: f32) -> Result<(), E> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
fn serialize_f64(&mut self, v: f64) -> Result<(), E>;
|
||||
|
||||
fn serialize_char(&mut self, v: char) -> Result<(), E>;
|
||||
|
||||
fn serialize_str(&mut self, v: &str) -> Result<(), E>;
|
||||
|
||||
fn serialize_tuple_start(&mut self, len: uint) -> Result<(), E>;
|
||||
fn serialize_tuple_sep<T: Serializable>(&mut self, v: &T) -> Result<(), E>;
|
||||
fn serialize_tuple_end(&mut self) -> Result<(), E>;
|
||||
|
||||
fn serialize_struct_start(&mut self, name: &str, len: uint) -> Result<(), E>;
|
||||
fn serialize_struct_sep<T: Serializable>(&mut self, name: &str, v: &T) -> Result<(), E>;
|
||||
fn serialize_struct_end(&mut self) -> Result<(), E>;
|
||||
|
||||
fn serialize_enum_start(&mut self, name: &str, variant: &str, len: uint) -> Result<(), E>;
|
||||
fn serialize_enum_sep<T: Serializable>(&mut self, v: &T) -> Result<(), E>;
|
||||
fn serialize_enum_end(&mut self) -> Result<(), E>;
|
||||
|
||||
fn serialize_option<T: Serializable>(&mut self, v: &Option<T>) -> Result<(), E>;
|
||||
|
||||
fn serialize_seq<
|
||||
T: Serializable,
|
||||
Iter: Iterator<T>
|
||||
>(&mut self, iter: Iter) -> Result<(), E>;
|
||||
|
||||
fn serialize_map<
|
||||
K: Serializable,
|
||||
V: Serializable,
|
||||
Iter: Iterator<(K, V)>
|
||||
>(&mut self, iter: Iter) -> Result<(), E>;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
pub trait Serializable {
|
||||
fn serialize<
|
||||
S: Serializer<E>,
|
||||
E
|
||||
>(&self, s: &mut S) -> Result<(), E>;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! impl_serializable {
|
||||
($ty:ty, $method:ident) => {
|
||||
impl Serializable for $ty {
|
||||
#[inline]
|
||||
fn serialize<S: Serializer<E>, E>(&self, s: &mut S) -> Result<(), E> {
|
||||
s.$method(*self)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_serializable!(bool, serialize_bool)
|
||||
impl_serializable!(int, serialize_int)
|
||||
impl_serializable!(i8, serialize_i8)
|
||||
impl_serializable!(i16, serialize_i16)
|
||||
impl_serializable!(i32, serialize_i32)
|
||||
impl_serializable!(i64, serialize_i64)
|
||||
impl_serializable!(uint, serialize_uint)
|
||||
impl_serializable!(u8, serialize_u8)
|
||||
impl_serializable!(u16, serialize_u16)
|
||||
impl_serializable!(u32, serialize_u32)
|
||||
impl_serializable!(u64, serialize_u64)
|
||||
impl_serializable!(f32, serialize_f32)
|
||||
impl_serializable!(f64, serialize_f64)
|
||||
impl_serializable!(char, serialize_char)
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<'a> Serializable for &'a str {
|
||||
#[inline]
|
||||
fn serialize<
|
||||
S: Serializer<E>,
|
||||
E
|
||||
>(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize_str(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Serializable for String {
|
||||
#[inline]
|
||||
fn serialize<
|
||||
S: Serializer<E>,
|
||||
E
|
||||
>(&self, s: &mut S) -> Result<(), E> {
|
||||
self.as_slice().serialize(s)
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<
|
||||
'a,
|
||||
T: Serializable
|
||||
> Serializable for &'a T {
|
||||
#[inline]
|
||||
fn serialize<
|
||||
S: Serializer<E>,
|
||||
E
|
||||
>(&self, s: &mut S) -> Result<(), E> {
|
||||
(*self).serialize(s)
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<
|
||||
T: Serializable
|
||||
> Serializable for Option<T> {
|
||||
#[inline]
|
||||
fn serialize<
|
||||
S: Serializer<E>,
|
||||
E
|
||||
>(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize_option(self)
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<
|
||||
T: Serializable
|
||||
> Serializable for Vec<T> {
|
||||
#[inline]
|
||||
fn serialize<
|
||||
S: Serializer<E>,
|
||||
E
|
||||
>(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize_seq(self.iter())
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
impl<
|
||||
K: Serializable + Eq + Hash,
|
||||
V: Serializable
|
||||
> Serializable for HashMap<K, V> {
|
||||
#[inline]
|
||||
fn serialize<
|
||||
S: Serializer<E>,
|
||||
E
|
||||
>(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize_map(self.iter())
|
||||
}
|
||||
}
|
||||
|
||||
impl<
|
||||
K: Serializable + Ord,
|
||||
V: Serializable
|
||||
> Serializable for TreeMap<K, V> {
|
||||
#[inline]
|
||||
fn serialize<
|
||||
S: Serializer<E>,
|
||||
E
|
||||
>(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize_map(self.iter())
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
macro_rules! peel {
|
||||
($name:ident, $($other:ident,)*) => (impl_serialize_tuple!($($other,)*))
|
||||
}
|
||||
|
||||
macro_rules! impl_serialize_tuple {
|
||||
() => {
|
||||
impl Serializable for () {
|
||||
#[inline]
|
||||
fn serialize<
|
||||
S: Serializer<E>,
|
||||
E
|
||||
>(&self, s: &mut S) -> Result<(), E> {
|
||||
s.serialize_null()
|
||||
}
|
||||
}
|
||||
};
|
||||
( $($name:ident,)+ ) => {
|
||||
impl<
|
||||
$($name:Serializable),*
|
||||
> Serializable for ($($name,)*) {
|
||||
#[inline]
|
||||
#[allow(uppercase_variables)]
|
||||
fn serialize<
|
||||
S: Serializer<E>,
|
||||
E
|
||||
>(&self, s: &mut S) -> Result<(), E> {
|
||||
// FIXME: how can we count macro args?
|
||||
let mut len = 0;
|
||||
$({ let $name = 1; len += $name; })*;
|
||||
|
||||
let ($(ref $name,)*) = *self;
|
||||
|
||||
try!(s.serialize_tuple_start(len));
|
||||
$(
|
||||
try!(s.serialize_tuple_sep($name));
|
||||
)*
|
||||
s.serialize_tuple_end()
|
||||
}
|
||||
}
|
||||
peel!($($name,)*)
|
||||
}
|
||||
}
|
||||
|
||||
impl_serialize_tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::{HashMap, TreeMap};
|
||||
|
||||
use serialize::Decoder;
|
||||
|
||||
use super::{Serializer, Serializable};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Clone, PartialEq, Show, Decodable)]
|
||||
#[deriving_serializable]
|
||||
struct Inner {
|
||||
a: (),
|
||||
b: uint,
|
||||
c: HashMap<String, Option<char>>,
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Clone, PartialEq, Show, Decodable)]
|
||||
#[deriving_serializable]
|
||||
struct Outer {
|
||||
inner: Vec<Inner>,
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Clone, PartialEq, Show, Decodable)]
|
||||
#[deriving_serializable]
|
||||
enum Animal {
|
||||
Dog,
|
||||
Frog(String, int)
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[deriving(Clone, PartialEq, Show)]
|
||||
pub enum Token<'a> {
|
||||
Null,
|
||||
Bool(bool),
|
||||
Int(int),
|
||||
I8(i8),
|
||||
I16(i16),
|
||||
I32(i32),
|
||||
I64(i64),
|
||||
Uint(uint),
|
||||
U8(u8),
|
||||
U16(u16),
|
||||
U32(u32),
|
||||
U64(u64),
|
||||
F32(f32),
|
||||
F64(f64),
|
||||
Char(char),
|
||||
Str(&'a str),
|
||||
|
||||
TupleStart(uint),
|
||||
TupleSep,
|
||||
TupleEnd,
|
||||
|
||||
StructStart(&'a str, uint),
|
||||
StructSep(&'a str),
|
||||
StructEnd,
|
||||
|
||||
EnumStart(&'a str, &'a str, uint),
|
||||
EnumSep,
|
||||
EnumEnd,
|
||||
|
||||
Option(bool),
|
||||
|
||||
SeqStart(uint),
|
||||
SeqEnd,
|
||||
|
||||
MapStart(uint),
|
||||
MapEnd,
|
||||
}
|
||||
|
||||
#[deriving(Show)]
|
||||
enum Error {
|
||||
EndOfStream,
|
||||
SyntaxError,
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct AssertSerializer<Iter> {
|
||||
iter: Iter,
|
||||
}
|
||||
|
||||
impl<'a, Iter: Iterator<Token<'a>>> AssertSerializer<Iter> {
|
||||
fn new(iter: Iter) -> AssertSerializer<Iter> {
|
||||
AssertSerializer {
|
||||
iter: iter,
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize<'b>(&mut self, token: Token<'b>) -> Result<(), Error> {
|
||||
let t = match self.iter.next() {
|
||||
Some(t) => t,
|
||||
None => { fail!(); }
|
||||
};
|
||||
|
||||
assert_eq!(t, token);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Iter: Iterator<Token<'a>>> Serializer<Error> for AssertSerializer<Iter> {
|
||||
fn serialize_null(&mut self) -> Result<(), Error> {
|
||||
self.serialize(Null)
|
||||
}
|
||||
fn serialize_bool(&mut self, v: bool) -> Result<(), Error> {
|
||||
self.serialize(Bool(v))
|
||||
}
|
||||
fn serialize_int(&mut self, v: int) -> Result<(), Error> {
|
||||
self.serialize(Int(v))
|
||||
}
|
||||
|
||||
fn serialize_i8(&mut self, v: i8) -> Result<(), Error> {
|
||||
self.serialize(I8(v))
|
||||
}
|
||||
|
||||
fn serialize_i16(&mut self, v: i16) -> Result<(), Error> {
|
||||
self.serialize(I16(v))
|
||||
}
|
||||
|
||||
fn serialize_i32(&mut self, v: i32) -> Result<(), Error> {
|
||||
self.serialize(I32(v))
|
||||
}
|
||||
|
||||
fn serialize_i64(&mut self, v: i64) -> Result<(), Error> {
|
||||
self.serialize(I64(v))
|
||||
}
|
||||
|
||||
fn serialize_uint(&mut self, v: uint) -> Result<(), Error> {
|
||||
self.serialize(Uint(v))
|
||||
}
|
||||
|
||||
fn serialize_u8(&mut self, v: u8) -> Result<(), Error> {
|
||||
self.serialize(U8(v))
|
||||
}
|
||||
|
||||
fn serialize_u16(&mut self, v: u16) -> Result<(), Error> {
|
||||
self.serialize(U16(v))
|
||||
}
|
||||
|
||||
fn serialize_u32(&mut self, v: u32) -> Result<(), Error> {
|
||||
self.serialize(U32(v))
|
||||
}
|
||||
|
||||
fn serialize_u64(&mut self, v: u64) -> Result<(), Error> {
|
||||
self.serialize(U64(v))
|
||||
}
|
||||
|
||||
fn serialize_f32(&mut self, v: f32) -> Result<(), Error> {
|
||||
self.serialize(F32(v))
|
||||
}
|
||||
|
||||
fn serialize_f64(&mut self, v: f64) -> Result<(), Error> {
|
||||
self.serialize(F64(v))
|
||||
}
|
||||
|
||||
fn serialize_char(&mut self, v: char) -> Result<(), Error> {
|
||||
self.serialize(Char(v))
|
||||
}
|
||||
|
||||
fn serialize_str(&mut self, v: &str) -> Result<(), Error> {
|
||||
self.serialize(Str(v))
|
||||
}
|
||||
|
||||
fn serialize_tuple_start(&mut self, len: uint) -> Result<(), Error> {
|
||||
self.serialize(TupleStart(len))
|
||||
}
|
||||
|
||||
fn serialize_tuple_sep<
|
||||
T: Serializable
|
||||
>(&mut self, value: &T) -> Result<(), Error> {
|
||||
try!(self.serialize(TupleSep));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple_end(&mut self) -> Result<(), Error> {
|
||||
self.serialize(TupleEnd)
|
||||
}
|
||||
|
||||
fn serialize_struct_start(&mut self, name: &str, len: uint) -> Result<(), Error> {
|
||||
self.serialize(StructStart(name, len))
|
||||
}
|
||||
|
||||
fn serialize_struct_sep<
|
||||
T: Serializable
|
||||
>(&mut self, name: &str, value: &T) -> Result<(), Error> {
|
||||
try!(self.serialize(StructSep(name)));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_struct_end(&mut self) -> Result<(), Error> {
|
||||
self.serialize(StructEnd)
|
||||
}
|
||||
|
||||
fn serialize_enum_start(&mut self, name: &str, variant: &str, len: uint) -> Result<(), Error> {
|
||||
self.serialize(EnumStart(name, variant, len))
|
||||
}
|
||||
|
||||
fn serialize_enum_sep<
|
||||
T: Serializable
|
||||
>(&mut self, value: &T) -> Result<(), Error> {
|
||||
try!(self.serialize(EnumSep));
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_enum_end(&mut self) -> Result<(), Error> {
|
||||
self.serialize(EnumEnd)
|
||||
}
|
||||
|
||||
fn serialize_option<
|
||||
T: Serializable
|
||||
>(&mut self, v: &Option<T>) -> Result<(), Error> {
|
||||
match *v {
|
||||
Some(ref v) => {
|
||||
try!(self.serialize(Option(true)));
|
||||
v.serialize(self)
|
||||
}
|
||||
None => {
|
||||
self.serialize(Option(false))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_seq<
|
||||
T: Serializable,
|
||||
SeqIter: Iterator<T>
|
||||
>(&mut self, mut iter: SeqIter) -> Result<(), Error> {
|
||||
let (len, _) = iter.size_hint();
|
||||
try!(self.serialize(SeqStart(len)));
|
||||
for elt in iter {
|
||||
elt.serialize(self).unwrap();
|
||||
}
|
||||
self.serialize(SeqEnd)
|
||||
}
|
||||
|
||||
fn serialize_map<
|
||||
K: Serializable,
|
||||
V: Serializable,
|
||||
MapIter: Iterator<(K, V)>
|
||||
>(&mut self, mut iter: MapIter) -> Result<(), Error> {
|
||||
let (len, _) = iter.size_hint();
|
||||
try!(self.serialize(MapStart(len)));
|
||||
for (key, value) in iter {
|
||||
try!(key.serialize(self));
|
||||
try!(value.serialize(self));
|
||||
}
|
||||
self.serialize(MapEnd)
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#[test]
|
||||
fn test_tokens_int() {
|
||||
let tokens = vec!(
|
||||
Int(5)
|
||||
);
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
5i.serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tokens_str() {
|
||||
let tokens = vec!(
|
||||
Str("a"),
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
"a".serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tokens_null() {
|
||||
let tokens = vec!(
|
||||
Null,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
().serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tokens_option_none() {
|
||||
let tokens = vec!(
|
||||
Option(false),
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
None::<int>.serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tokens_option_some() {
|
||||
let tokens = vec!(
|
||||
Option(true),
|
||||
Int(5),
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
Some(5i).serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tokens_tuple() {
|
||||
let tokens = vec!(
|
||||
TupleStart(2),
|
||||
TupleSep,
|
||||
Int(5),
|
||||
|
||||
TupleSep,
|
||||
Str("a"),
|
||||
TupleEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
(5i, "a").serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tokens_tuple_compound() {
|
||||
let tokens = vec!(
|
||||
TupleStart(3),
|
||||
TupleSep,
|
||||
Null,
|
||||
|
||||
TupleSep,
|
||||
Null,
|
||||
|
||||
TupleSep,
|
||||
TupleStart(2),
|
||||
TupleSep,
|
||||
Int(5),
|
||||
|
||||
TupleSep,
|
||||
Str("a"),
|
||||
TupleEnd,
|
||||
TupleEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
((), (), (5i, "a")).serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tokens_struct_empty() {
|
||||
let tokens = vec!(
|
||||
StructStart("Outer", 1),
|
||||
StructSep("inner"),
|
||||
SeqStart(0),
|
||||
SeqEnd,
|
||||
StructEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
Outer { inner: vec!() }.serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tokens_struct() {
|
||||
let tokens = vec!(
|
||||
StructStart("Outer", 1),
|
||||
StructSep("inner"),
|
||||
SeqStart(1),
|
||||
StructStart("Inner", 3),
|
||||
StructSep("a"),
|
||||
Null,
|
||||
|
||||
StructSep("b"),
|
||||
Uint(5),
|
||||
|
||||
StructSep("c"),
|
||||
MapStart(1),
|
||||
Str("abc"),
|
||||
Option(true),
|
||||
Char('c'),
|
||||
MapEnd,
|
||||
StructEnd,
|
||||
SeqEnd,
|
||||
StructEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
|
||||
let mut map = HashMap::new();
|
||||
map.insert("abc".to_string(), Some('c'));
|
||||
|
||||
Outer {
|
||||
inner: vec!(
|
||||
Inner {
|
||||
a: (),
|
||||
b: 5,
|
||||
c: map,
|
||||
},
|
||||
)
|
||||
}.serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tokens_enum() {
|
||||
let tokens = vec!(
|
||||
EnumStart("Animal", "Dog", 0),
|
||||
EnumEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
Dog.serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
|
||||
let tokens = vec!(
|
||||
EnumStart("Animal", "Frog", 2),
|
||||
EnumSep,
|
||||
Str("Henry"),
|
||||
|
||||
EnumSep,
|
||||
Int(349),
|
||||
EnumEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
Frog("Henry".to_string(), 349).serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tokens_vec_empty() {
|
||||
let tokens = vec!(
|
||||
SeqStart(0),
|
||||
SeqEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
let v: Vec<int> = vec!();
|
||||
v.serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tokens_vec() {
|
||||
let tokens = vec!(
|
||||
SeqStart(3),
|
||||
Int(5),
|
||||
Int(6),
|
||||
Int(7),
|
||||
SeqEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
(vec!(5i, 6, 7)).serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tokens_vec_compound() {
|
||||
let tokens = vec!(
|
||||
SeqStart(3),
|
||||
SeqStart(1),
|
||||
Int(1),
|
||||
SeqEnd,
|
||||
|
||||
SeqStart(2),
|
||||
Int(2),
|
||||
Int(3),
|
||||
SeqEnd,
|
||||
|
||||
SeqStart(3),
|
||||
Int(4),
|
||||
Int(5),
|
||||
Int(6),
|
||||
SeqEnd,
|
||||
SeqEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
(vec!(vec!(1i), vec!(2, 3), vec!(4, 5, 6))).serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tokens_treemap() {
|
||||
let tokens = vec!(
|
||||
MapStart(2),
|
||||
Int(5),
|
||||
Str("a"),
|
||||
|
||||
Int(6),
|
||||
Str("b"),
|
||||
MapEnd,
|
||||
);
|
||||
|
||||
let mut serializer = AssertSerializer::new(tokens.move_iter());
|
||||
|
||||
let mut map = TreeMap::new();
|
||||
map.insert(5i, "a".to_string());
|
||||
map.insert(6i, "b".to_string());
|
||||
|
||||
map.serialize(&mut serializer).unwrap();
|
||||
assert_eq!(serializer.iter.next(), None);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user