mirror of
https://github.com/serde-rs/serde.git
synced 2025-09-30 14:31:53 +00:00
Experiment with lookahead, it's a tad slower than before
This commit is contained in:
parent
e34dcdcd18
commit
db242fed53
168
de.rs
168
de.rs
@ -31,11 +31,9 @@ pub enum Token {
|
|||||||
|
|
||||||
macro_rules! decode_primitive {
|
macro_rules! decode_primitive {
|
||||||
($( $Variant:pat => $E:expr ),+) => {
|
($( $Variant:pat => $E:expr ),+) => {
|
||||||
match self.next() {
|
match token {
|
||||||
$( Some(Ok($Variant)) => $E ),+,
|
$( $Variant => $E ),+,
|
||||||
Some(Ok(_)) => Err(self.syntax_error()),
|
_ => Err(self.syntax_error()),
|
||||||
Some(Err(err)) => Err(err),
|
|
||||||
None => Err(self.end_of_stream_error()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -74,103 +72,124 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
fn syntax_error(&self) -> E;
|
fn syntax_error(&self) -> E;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_null(&mut self) -> Result<(), E> {
|
fn expect_token(&mut self) -> Result<Token, E> {
|
||||||
|
match self.next() {
|
||||||
|
Some(Ok(token)) => Ok(token),
|
||||||
|
Some(Err(err)) => Err(err),
|
||||||
|
None => Err(self.end_of_stream_error()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn expect_null(&mut self, token: Token) -> Result<(), E> {
|
||||||
decode_primitive!(
|
decode_primitive!(
|
||||||
Null => Ok(()),
|
Null => Ok(()),
|
||||||
CollectionStart => self.expect_collection_end()
|
CollectionStart => {
|
||||||
|
let token = try!(self.expect_token());
|
||||||
|
self.expect_collection_end(token)
|
||||||
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_bool(&mut self) -> Result<bool, E> {
|
fn expect_bool(&mut self, token: Token) -> Result<bool, E> {
|
||||||
decode_primitive!(Bool(value) => Ok(value))
|
decode_primitive!(Bool(value) => Ok(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_int(&mut self) -> Result<int, E> {
|
fn expect_int(&mut self, token: Token) -> Result<int, E> {
|
||||||
decode_primitive_num!(to_int)
|
decode_primitive_num!(to_int)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_i8(&mut self) -> Result<i8, E> {
|
fn expect_i8(&mut self, token: Token) -> Result<i8, E> {
|
||||||
decode_primitive_num!(to_i8)
|
decode_primitive_num!(to_i8)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_i16(&mut self) -> Result<i16, E> {
|
fn expect_i16(&mut self, token: Token) -> Result<i16, E> {
|
||||||
decode_primitive_num!(to_i16)
|
decode_primitive_num!(to_i16)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_i32(&mut self) -> Result<i32, E> {
|
fn expect_i32(&mut self, token: Token) -> Result<i32, E> {
|
||||||
decode_primitive_num!(to_i32)
|
decode_primitive_num!(to_i32)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_i64(&mut self) -> Result<i64, E> {
|
fn expect_i64(&mut self, token: Token) -> Result<i64, E> {
|
||||||
decode_primitive_num!(to_i64)
|
decode_primitive_num!(to_i64)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_uint(&mut self) -> Result<uint, E> {
|
fn expect_uint(&mut self, token: Token) -> Result<uint, E> {
|
||||||
decode_primitive_num!(to_uint)
|
decode_primitive_num!(to_uint)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_u8(&mut self) -> Result<u8, E> {
|
fn expect_u8(&mut self, token: Token) -> Result<u8, E> {
|
||||||
decode_primitive_num!(to_u8)
|
decode_primitive_num!(to_u8)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_u16(&mut self) -> Result<u16, E> {
|
fn expect_u16(&mut self, token: Token) -> Result<u16, E> {
|
||||||
decode_primitive_num!(to_u16)
|
decode_primitive_num!(to_u16)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_u32(&mut self) -> Result<u32, E> {
|
fn expect_u32(&mut self, token: Token) -> Result<u32, E> {
|
||||||
decode_primitive_num!(to_u32)
|
decode_primitive_num!(to_u32)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_u64(&mut self) -> Result<u64, E> {
|
fn expect_u64(&mut self, token: Token) -> Result<u64, E> {
|
||||||
decode_primitive_num!(to_u64)
|
decode_primitive_num!(to_u64)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_f32(&mut self) -> Result<f32, E> {
|
fn expect_f32(&mut self, token: Token) -> Result<f32, E> {
|
||||||
decode_primitive_num!(to_f32)
|
decode_primitive_num!(to_f32)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_f64(&mut self) -> Result<f64, E> {
|
fn expect_f64(&mut self, token: Token) -> Result<f64, E> {
|
||||||
decode_primitive_num!(to_f64)
|
decode_primitive_num!(to_f64)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_char(&mut self) -> Result<char, E> {
|
fn expect_char(&mut self, token: Token) -> Result<char, E> {
|
||||||
decode_primitive!(Char(value) => Ok(value))
|
decode_primitive!(Char(value) => Ok(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_str(&mut self) -> Result<&'static str, E> {
|
fn expect_str(&mut self, token: Token) -> Result<&'static str, E> {
|
||||||
decode_primitive!(Str(value) => Ok(value))
|
decode_primitive!(Str(value) => Ok(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_strbuf(&mut self) -> Result<StrBuf, E> {
|
fn expect_strbuf(&mut self, token: Token) -> Result<StrBuf, E> {
|
||||||
decode_primitive!(
|
decode_primitive!(
|
||||||
Str(value) => Ok(value.to_strbuf()),
|
Str(value) => Ok(value.to_strbuf()),
|
||||||
StrBuf(value) => Ok(value)
|
StrBuf(value) => Ok(value)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn expect_option<
|
||||||
|
T: Deserializable<E, Self>
|
||||||
|
>(&mut self, token: Token) -> Result<Option<T>, E> {
|
||||||
|
match token {
|
||||||
|
Null => Ok(None),
|
||||||
|
_ => fail!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_collection<
|
fn expect_collection<
|
||||||
T: Deserializable<E, Self>,
|
T: Deserializable<E, Self>,
|
||||||
C: FromIterator<T>
|
C: FromIterator<T>
|
||||||
>(&mut self) -> Result<C, E> {
|
>(&mut self, token: Token) -> Result<C, E> {
|
||||||
try!(self.expect_collection_start());
|
try!(self.expect_collection_start(token));
|
||||||
|
|
||||||
let iter = self.by_ref().batch(|d| {
|
let iter = self.by_ref().batch(|d| {
|
||||||
let d = d.iter();
|
let d = d.iter();
|
||||||
@ -201,43 +220,35 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_collection_start(&mut self) -> Result<(), E> {
|
fn expect_collection_start(&mut self, token: Token) -> Result<(), E> {
|
||||||
match self.next() {
|
match token {
|
||||||
Some(Ok(CollectionStart)) => Ok(()),
|
CollectionStart => Ok(()),
|
||||||
Some(Ok(_)) => Err(self.syntax_error()),
|
_ => Err(self.syntax_error()),
|
||||||
Some(Err(err)) => Err(err),
|
|
||||||
None => Err(self.end_of_stream_error()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_collection_sep(&mut self) -> Result<(), E> {
|
fn expect_collection_sep(&mut self, token: Token) -> Result<(), E> {
|
||||||
match self.next() {
|
match token {
|
||||||
Some(Ok(CollectionSep)) => Ok(()),
|
CollectionSep => Ok(()),
|
||||||
Some(Ok(_)) => Err(self.syntax_error()),
|
_ => Err(self.syntax_error()),
|
||||||
Some(Err(err)) => Err(err),
|
|
||||||
None => Err(self.end_of_stream_error()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_collection_end(&mut self) -> Result<(), E> {
|
fn expect_collection_end(&mut self, token: Token) -> Result<(), E> {
|
||||||
match self.next() {
|
match token {
|
||||||
Some(Ok(CollectionEnd)) => Ok(()),
|
CollectionEnd => Ok(()),
|
||||||
Some(Ok(_)) => Err(self.syntax_error()),
|
_ => Err(self.syntax_error()),
|
||||||
Some(Err(err)) => Err(err),
|
|
||||||
None => Err(self.end_of_stream_error()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_collection_sep_or_end(&mut self) -> Result<bool, E> {
|
fn expect_collection_sep_or_end(&mut self, token: Token) -> Result<bool, E> {
|
||||||
match self.next() {
|
match token {
|
||||||
Some(Ok(CollectionSep)) => Ok(false),
|
CollectionSep => Ok(false),
|
||||||
Some(Ok(CollectionEnd)) => Ok(true),
|
CollectionEnd => Ok(true),
|
||||||
Some(Ok(_)) => Err(self.syntax_error()),
|
_ => Err(self.syntax_error()),
|
||||||
Some(Err(err)) => Err(err),
|
|
||||||
None => Err(self.end_of_stream_error()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -245,7 +256,12 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
|||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
pub trait Deserializable<E, D: Deserializer<E>> {
|
pub trait Deserializable<E, D: Deserializer<E>> {
|
||||||
fn deserialize(d: &mut D) -> Result<Self, E>;
|
fn deserialize(d: &mut D) -> Result<Self, E> {
|
||||||
|
let token = try!(d.expect_token());
|
||||||
|
Deserializable::deserialize_token(d, token)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_token(d: &mut D, token: Token) -> Result<Self, E>;
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
@ -257,8 +273,8 @@ macro_rules! impl_deserializable {
|
|||||||
D: Deserializer<E>
|
D: Deserializer<E>
|
||||||
> Deserializable<E, D> for $ty {
|
> Deserializable<E, D> for $ty {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize(d: &mut D) -> Result<$ty, E> {
|
fn deserialize_token(d: &mut D, token: Token) -> Result<$ty, E> {
|
||||||
d.$method()
|
d.$method(token)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -289,9 +305,8 @@ impl<
|
|||||||
T: Deserializable<E, D>
|
T: Deserializable<E, D>
|
||||||
> Deserializable<E, D> for Option<T> {
|
> Deserializable<E, D> for Option<T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize(_d: &mut D) -> Result<Option<T>, E> {
|
fn deserialize_token(d: &mut D, token: Token) -> Result<Option<T>, E> {
|
||||||
fail!()
|
d.expect_option(token)
|
||||||
//d.expect_collection()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -303,8 +318,8 @@ impl<
|
|||||||
T: Deserializable<E, D>
|
T: Deserializable<E, D>
|
||||||
> Deserializable<E, D> for Vec<T> {
|
> Deserializable<E, D> for Vec<T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize(d: &mut D) -> Result<Vec<T>, E> {
|
fn deserialize_token(d: &mut D, token: Token) -> Result<Vec<T>, E> {
|
||||||
d.expect_collection()
|
d.expect_collection(token)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -315,8 +330,8 @@ impl<
|
|||||||
V: Deserializable<E, D>
|
V: Deserializable<E, D>
|
||||||
> Deserializable<E, D> for HashMap<K, V> {
|
> Deserializable<E, D> for HashMap<K, V> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize(d: &mut D) -> Result<HashMap<K, V>, E> {
|
fn deserialize_token(d: &mut D, token: Token) -> Result<HashMap<K, V>, E> {
|
||||||
d.expect_collection()
|
d.expect_collection(token)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -327,8 +342,8 @@ impl<
|
|||||||
D: Deserializer<E>
|
D: Deserializer<E>
|
||||||
> Deserializable<E, D> for () {
|
> Deserializable<E, D> for () {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize(d: &mut D) -> Result<(), E> {
|
fn deserialize_token(d: &mut D, token: Token) -> Result<(), E> {
|
||||||
d.expect_null()
|
d.expect_null(token)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -340,13 +355,15 @@ impl<
|
|||||||
T0: Deserializable<E, D>
|
T0: Deserializable<E, D>
|
||||||
> Deserializable<E, D> for (T0,) {
|
> Deserializable<E, D> for (T0,) {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize(d: &mut D) -> Result<(T0,), E> {
|
fn deserialize_token(d: &mut D, token: Token) -> Result<(T0,), E> {
|
||||||
try!(d.expect_collection_start());
|
try!(d.expect_collection_start(token));
|
||||||
|
|
||||||
try!(d.expect_collection_sep());
|
let token = try!(d.expect_token());
|
||||||
|
try!(d.expect_collection_sep(token));
|
||||||
let x0 = try!(Deserializable::deserialize(d));
|
let x0 = try!(Deserializable::deserialize(d));
|
||||||
|
|
||||||
try!(d.expect_collection_end());
|
let token = try!(d.expect_token());
|
||||||
|
try!(d.expect_collection_end(token));
|
||||||
|
|
||||||
Ok((x0,))
|
Ok((x0,))
|
||||||
}
|
}
|
||||||
@ -361,16 +378,19 @@ impl<
|
|||||||
T1: Deserializable<E, D>
|
T1: Deserializable<E, D>
|
||||||
> Deserializable<E, D> for (T0, T1) {
|
> Deserializable<E, D> for (T0, T1) {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deserialize(d: &mut D) -> Result<(T0, T1), E> {
|
fn deserialize_token(d: &mut D, token: Token) -> Result<(T0, T1), E> {
|
||||||
try!(d.expect_collection_start());
|
try!(d.expect_collection_start(token));
|
||||||
|
|
||||||
try!(d.expect_collection_sep());
|
let token = try!(d.expect_token());
|
||||||
|
try!(d.expect_collection_sep(token));
|
||||||
let x0 = try!(Deserializable::deserialize(d));
|
let x0 = try!(Deserializable::deserialize(d));
|
||||||
|
|
||||||
try!(d.expect_collection_sep());
|
let token = try!(d.expect_token());
|
||||||
|
try!(d.expect_collection_sep(token));
|
||||||
let x1 = try!(Deserializable::deserialize(d));
|
let x1 = try!(Deserializable::deserialize(d));
|
||||||
|
|
||||||
try!(d.expect_collection_end());
|
let token = try!(d.expect_token());
|
||||||
|
try!(d.expect_collection_end(token));
|
||||||
|
|
||||||
Ok((x0, x1))
|
Ok((x0, x1))
|
||||||
}
|
}
|
||||||
@ -509,8 +529,9 @@ mod tests {
|
|||||||
SyntaxError
|
SyntaxError
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
#[inline]
|
#[inline]
|
||||||
fn expect_int(&mut self) -> Result<int, Error> {
|
fn expect_int(&mut self, token: Token) -> Result<int, Error> {
|
||||||
assert_eq!(self.state, Value);
|
assert_eq!(self.state, Value);
|
||||||
|
|
||||||
self.state = Sep;
|
self.state = Sep;
|
||||||
@ -520,6 +541,7 @@ mod tests {
|
|||||||
None => Err(self.end_of_stream_error()),
|
None => Err(self.end_of_stream_error()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
struct IntsDecoder {
|
struct IntsDecoder {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user