Rewrite to work around rust/issues/11751. 162ns vs 156ns

This commit is contained in:
Erick Tryzelaar 2014-05-25 21:36:56 -07:00
parent 7030b5d790
commit f274dcbd44

41
de.rs
View File

@ -224,6 +224,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
} }
} }
/*
#[inline] #[inline]
fn expect_collection< fn expect_collection<
T: Deserializable<E, Self>, T: Deserializable<E, Self>,
@ -241,20 +242,17 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
expect_rest_of_collection(self, len) expect_rest_of_collection(self, len)
} }
*/
#[inline] #[inline]
fn expect_seq< fn expect_seq_start(&mut self, token: Token) -> Result<uint, E> {
T: Deserializable<E, Self>, match token {
C: FromIterator<T> SeqStart(len) => Ok(len),
>(&mut self, token: Token) -> Result<C, E> { _ => Err(self.syntax_error()),
let len = match token { }
SeqStart(len) => len,
_ => { return Err(self.syntax_error()); }
};
expect_rest_of_collection(self, len)
} }
/*
#[inline] #[inline]
fn expect_map< fn expect_map<
K: Deserializable<E, Self>, K: Deserializable<E, Self>,
@ -268,6 +266,7 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
expect_rest_of_collection(self, len) expect_rest_of_collection(self, len)
} }
*/
#[inline] #[inline]
fn expect_end(&mut self) -> Result<(), E> { fn expect_end(&mut self) -> Result<(), E> {
@ -282,6 +281,8 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
/*
// FIXME: https://github.com/mozilla/rust/issues/11751
#[inline] #[inline]
fn expect_rest_of_collection< fn expect_rest_of_collection<
E, E,
@ -305,6 +306,7 @@ fn expect_rest_of_collection<
result::collect_with_capacity(iter, len) result::collect_with_capacity(iter, len)
} }
*/
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
@ -381,7 +383,24 @@ impl<
> Deserializable<E, D> for Vec<T> { > Deserializable<E, D> for Vec<T> {
#[inline] #[inline]
fn deserialize_token(d: &mut D, token: Token) -> Result<Vec<T>, E> { fn deserialize_token(d: &mut D, token: Token) -> Result<Vec<T>, E> {
d.expect_collection(token) let len = try!(d.expect_seq_start(token));
let mut value = Vec::with_capacity(len);
loop {
match d.next() {
Some(Ok(End)) => { break; }
Some(Ok(token)) => {
let v = try!(Deserializable::deserialize_token(d, token));
value.push(v)
}
Some(Err(err)) => { return Err(err); }
None => { return Err(d.end_of_stream_error()); }
}
}
try!(d.expect_end());
Ok(value)
} }
} }