Migrate enum bench to lookahead

This commit is contained in:
Erick Tryzelaar 2014-05-24 14:08:35 -07:00
parent 7180d94196
commit cb1b006b0f
3 changed files with 28 additions and 10 deletions

View File

@ -2,7 +2,7 @@ use test::Bencher;
use serialize::{Decoder, Decodable}; use serialize::{Decoder, Decodable};
use de::{Deserializer, Deserializable}; use de::{Deserializer, Deserializable, Token, End};
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
@ -14,8 +14,8 @@ enum Animal {
impl<E, D: Deserializer<E>> Deserializable<E, D> for Animal { impl<E, D: Deserializer<E>> Deserializable<E, D> for Animal {
#[inline] #[inline]
fn deserialize(d: &mut D) -> Result<Animal, E> { fn deserialize_token(d: &mut D, token: Token) -> Result<Animal, E> {
match try!(d.expect_enum_start("Animal", ["Dog", "Frog"])) { match try!(d.expect_enum_start(token, "Animal", ["Dog", "Frog"])) {
0 => { 0 => {
try!(d.expect_end()); try!(d.expect_end());
Ok(Dog) Ok(Dog)
@ -23,7 +23,9 @@ impl<E, D: Deserializer<E>> Deserializable<E, D> for Animal {
1 => { 1 => {
let x0 = try!(Deserializable::deserialize(d)); let x0 = try!(Deserializable::deserialize(d));
let x1 = try!(Deserializable::deserialize(d)); let x1 = try!(Deserializable::deserialize(d));
try!(d.expect_end()); try!(d.expect_end());
Ok(Frog(x0, x1)) Ok(Frog(x0, x1))
} }
_ => unreachable!(), _ => unreachable!(),

28
de.rs
View File

@ -132,15 +132,18 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
Str(value) => Ok(value) Str(value) => Ok(value)
} }
} }
*/
#[inline] #[inline]
fn expect_strbuf(&mut self) -> Result<StrBuf, E> { fn expect_strbuf(&mut self, token: Token) -> Result<StrBuf, E> {
match_token! { match token {
Str(value) => Ok(value.to_strbuf()), Str(value) => Ok(value.to_strbuf()),
StrBuf(value) => Ok(value) StrBuf(value) => Ok(value),
_ => Err(self.syntax_error()),
} }
} }
/*
#[inline] #[inline]
fn expect_option< fn expect_option<
T: Deserializable<E, Self> T: Deserializable<E, Self>
@ -204,10 +207,11 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
} }
} }
} }
*/
#[inline] #[inline]
fn expect_enum_start<'a>(&mut self, name: &str, variants: &[&str]) -> Result<uint, E> { fn expect_enum_start<'a>(&mut self, token: Token, name: &str, variants: &[&str]) -> Result<uint, E> {
match_token! { match token {
EnumStart(n) => { EnumStart(n) => {
if name == n { if name == n {
match_token! { match_token! {
@ -222,9 +226,9 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
Err(self.syntax_error()) Err(self.syntax_error())
} }
} }
_ => Err(self.syntax_error()),
} }
} }
*/
#[inline] #[inline]
fn expect_collection< fn expect_collection<
@ -270,6 +274,16 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
expect_rest_of_collection(self, len) 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!(char, expect_char)
impl_deserializable!(&'static str, expect_str) impl_deserializable!(&'static str, expect_str)
*/
impl_deserializable!(StrBuf, expect_strbuf) impl_deserializable!(StrBuf, expect_strbuf)
/*
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////

View File

@ -15,7 +15,7 @@ pub mod de;
//pub mod json; //pub mod json;
#[cfg(test)] #[cfg(test)]
//pub mod bench_enum; pub mod bench_enum;
#[cfg(test)] #[cfg(test)]
//pub mod bench_struct; //pub mod bench_struct;