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 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 {
#[inline]
fn deserialize(d: &mut D) -> Result<Animal, E> {
match try!(d.expect_enum_start("Animal", ["Dog", "Frog"])) {
fn deserialize_token(d: &mut D, token: Token) -> Result<Animal, E> {
match try!(d.expect_enum_start(token, "Animal", ["Dog", "Frog"])) {
0 => {
try!(d.expect_end());
Ok(Dog)
@ -23,7 +23,9 @@ impl<E, D: Deserializer<E>> Deserializable<E, D> for Animal {
1 => {
let x0 = try!(Deserializable::deserialize(d));
let x1 = try!(Deserializable::deserialize(d));
try!(d.expect_end());
Ok(Frog(x0, x1))
}
_ => unreachable!(),

28
de.rs
View File

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

View File

@ -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;