mirror of
https://github.com/serde-rs/serde.git
synced 2025-09-30 06:21:26 +00:00
explicit expect_{seq,map}
should error on another collection type
150ns vs 195ns
This commit is contained in:
parent
23630be5dd
commit
c7b6f45d7a
100
de.rs
100
de.rs
@ -161,7 +161,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn expect_seq<
|
||||
fn expect_collection<
|
||||
T: Deserializable<E, Self>,
|
||||
C: FromIterator<T>
|
||||
>(&mut self) -> Result<C, E> {
|
||||
@ -174,7 +174,51 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
MapStart(len) => len
|
||||
};
|
||||
|
||||
let iter = self.by_ref().batch(|d| {
|
||||
expect_rest_of_collection(self, len)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn expect_seq<
|
||||
T: Deserializable<E, Self>,
|
||||
C: FromIterator<T>
|
||||
>(&mut self) -> Result<C, E> {
|
||||
let len = match_token! {
|
||||
SeqStart(len) => len
|
||||
};
|
||||
|
||||
expect_rest_of_collection(self, len)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn expect_map<
|
||||
K: Deserializable<E, Self>,
|
||||
V: Deserializable<E, Self>,
|
||||
C: FromIterator<(K, V)>
|
||||
>(&mut self) -> Result<C, E> {
|
||||
let len = match_token! {
|
||||
MapStart(len) => len
|
||||
};
|
||||
|
||||
expect_rest_of_collection(self, len)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn expect_end(&mut self) -> Result<(), E> {
|
||||
match_token! {
|
||||
End => Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
fn expect_rest_of_collection<
|
||||
E,
|
||||
D: Deserializer<E>,
|
||||
T: Deserializable<E, D>,
|
||||
C: FromIterator<T>
|
||||
>(d: &mut D, len: uint) -> Result<C, E> {
|
||||
let iter = d.by_ref().batch(|d| {
|
||||
let d = d.iter();
|
||||
|
||||
let token = match d.next() {
|
||||
@ -194,52 +238,6 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
|
||||
});
|
||||
|
||||
result::collect_with_capacity(iter, len)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn expect_map<
|
||||
K: Deserializable<E, Self>,
|
||||
V: Deserializable<E, Self>,
|
||||
C: FromIterator<(K, V)>
|
||||
>(&mut self) -> Result<C, E> {
|
||||
// By default we don't care what our source input was. We can take
|
||||
// anything that's a Collection<(K, V)>.We'll error out later if the types
|
||||
// are wrong.
|
||||
|
||||
let len = match_token! {
|
||||
TupleStart(len) => len,
|
||||
VecStart(len) => len,
|
||||
MapStart(len) => len
|
||||
};
|
||||
|
||||
let iter = self.by_ref().batch(|d| {
|
||||
let d = d.iter();
|
||||
|
||||
let token = match d.next() {
|
||||
Some(token) => token,
|
||||
None => { return None; }
|
||||
};
|
||||
|
||||
match token {
|
||||
Ok(Sep) => {
|
||||
let kv: Result<(K, V), E> = Deserializable::deserialize(d);
|
||||
Some(kv)
|
||||
}
|
||||
Ok(End) => None,
|
||||
Ok(_) => Some(Err(d.syntax_error())),
|
||||
Err(e) => Some(Err(e)),
|
||||
}
|
||||
});
|
||||
|
||||
result::collect_with_capacity(iter, len)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn expect_end(&mut self) -> Result<(), E> {
|
||||
match_token! {
|
||||
End => Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
@ -352,12 +350,10 @@ macro_rules! deserialize_tuple (
|
||||
|
||||
try!(d.expect_tuple_start(len));
|
||||
|
||||
let result = ($(
|
||||
{
|
||||
let result = ($({
|
||||
let $name = try!(d.expect_tuple_elt());
|
||||
$name
|
||||
}
|
||||
,)*);
|
||||
},)*);
|
||||
|
||||
try!(d.expect_end());
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user