diff --git a/bench_enum.rs b/bench_enum.rs index ac98f5cd..80089115 100644 --- a/bench_enum.rs +++ b/bench_enum.rs @@ -2,7 +2,7 @@ use test::Bencher; use serialize::{Decoder, Decodable}; -use de::{Deserializer, Deserializable}; +use de::{Deserializer, Deserializable, Token, End}; ////////////////////////////////////////////////////////////////////////////// @@ -14,8 +14,8 @@ enum Animal { impl> Deserializable for Animal { #[inline] - fn deserialize(d: &mut D) -> Result { - match try!(d.expect_enum_start("Animal", ["Dog", "Frog"])) { + fn deserialize_token(d: &mut D, token: Token) -> Result { + match try!(d.expect_enum_start(token, "Animal", ["Dog", "Frog"])) { 0 => { try!(d.expect_end()); Ok(Dog) @@ -23,7 +23,9 @@ impl> Deserializable for Animal { 1 => { let x0 = try!(Deserializable::deserialize(d)); let x1 = try!(Deserializable::deserialize(d)); + try!(d.expect_end()); + Ok(Frog(x0, x1)) } _ => unreachable!(), diff --git a/de.rs b/de.rs index 776c22d9..54783230 100644 --- a/de.rs +++ b/de.rs @@ -132,15 +132,18 @@ pub trait Deserializer: Iterator> { Str(value) => Ok(value) } } + */ #[inline] - fn expect_strbuf(&mut self) -> Result { - match_token! { + fn expect_strbuf(&mut self, token: Token) -> Result { + match token { Str(value) => Ok(value.to_strbuf()), - StrBuf(value) => Ok(value) + StrBuf(value) => Ok(value), + _ => Err(self.syntax_error()), } } + /* #[inline] fn expect_option< T: Deserializable @@ -204,10 +207,11 @@ pub trait Deserializer: Iterator> { } } } + */ #[inline] - fn expect_enum_start<'a>(&mut self, name: &str, variants: &[&str]) -> Result { - match_token! { + fn expect_enum_start<'a>(&mut self, token: Token, name: &str, variants: &[&str]) -> Result { + match token { EnumStart(n) => { if name == n { match_token! { @@ -222,9 +226,9 @@ pub trait Deserializer: Iterator> { Err(self.syntax_error()) } } + _ => Err(self.syntax_error()), } } - */ #[inline] fn expect_collection< @@ -270,6 +274,16 @@ pub trait Deserializer: Iterator> { expect_rest_of_collection(self, len) } + + #[inline] + fn expect_end(&mut self) -> Result<(), E> { + match self.next() { + Some(Ok(End)) => Ok(()), + Some(Ok(_)) => Err(self.syntax_error()), + Some(Err(err)) => Err(err), + None => Err(self.end_of_stream_error()), + } + } } ////////////////////////////////////////////////////////////////////////////// @@ -343,8 +357,10 @@ impl_deserializable!(f64, expect_num) /* impl_deserializable!(char, expect_char) impl_deserializable!(&'static str, expect_str) +*/ impl_deserializable!(StrBuf, expect_strbuf) +/* ////////////////////////////////////////////////////////////////////////////// diff --git a/serde.rs b/serde.rs index 538144d5..d3e2ae4e 100644 --- a/serde.rs +++ b/serde.rs @@ -15,7 +15,7 @@ pub mod de; //pub mod json; #[cfg(test)] -//pub mod bench_enum; +pub mod bench_enum; #[cfg(test)] //pub mod bench_struct;