From cd3cd7b01c15476903c9cdc70e543264ded009e8 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 25 May 2014 22:08:37 -0700 Subject: [PATCH] Update deserializing hashmaps --- de.rs | 80 ++++++++++++++++++++++++++++++++++++++------------------ serde.rs | 2 +- 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/de.rs b/de.rs index 0c4ae17d..f9cb6151 100644 --- a/de.rs +++ b/de.rs @@ -2,7 +2,6 @@ extern crate collections; use std::hash::Hash; use std::num; -use std::result; use collections::HashMap; #[deriving(Clone, Eq)] @@ -221,6 +220,7 @@ pub trait Deserializer: Iterator> { } } + /* #[inline] fn expect_collection< T: Deserializable, @@ -238,6 +238,7 @@ pub trait Deserializer: Iterator> { expect_rest_of_collection(self, len) } + */ #[inline] fn expect_seq_start(&mut self, token: Token) -> Result { @@ -248,17 +249,11 @@ pub trait Deserializer: Iterator> { } #[inline] - fn expect_map< - K: Deserializable, - V: Deserializable, - C: FromIterator<(K, V)> - >(&mut self, token: Token) -> Result { - let len = match token { - MapStart(len) => len, - _ => { return Err(self.syntax_error()); } - }; - - expect_rest_of_collection(self, len) + fn expect_map_start(&mut self, token: Token) -> Result { + match token { + MapStart(len) => Ok(len), + _ => Err(self.syntax_error()), + } } #[inline] @@ -274,6 +269,7 @@ pub trait Deserializer: Iterator> { ////////////////////////////////////////////////////////////////////////////// +/* // FIXME: https://github.com/mozilla/rust/issues/11751 #[inline] fn expect_rest_of_collection< @@ -298,6 +294,7 @@ fn expect_rest_of_collection< result::collect_with_capacity(iter, len) } +*/ ////////////////////////////////////////////////////////////////////////////// @@ -362,6 +359,26 @@ impl< ////////////////////////////////////////////////////////////////////////////// +macro_rules! deserialize_seq { + ($seq:expr) => { + { + loop { + match d.next() { + Some(Ok(End)) => { break; } + Some(Ok(token)) => { + let v = try!(Deserializable::deserialize_token(d, token)); + $seq.push(v) + } + Some(Err(err)) => { return Err(err); } + None => { return Err(d.end_of_stream_error()); } + } + } + + Ok($seq) + } + } +} + impl< E, D: Deserializer, @@ -372,19 +389,29 @@ impl< 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()); } - } - } + deserialize_seq!(value) + } +} - Ok(value) +////////////////////////////////////////////////////////////////////////////// + +macro_rules! deserialize_map { + ($seq:expr) => { + { + loop { + match d.next() { + Some(Ok(End)) => { break; } + Some(Ok(token)) => { + let (k, v): (K, V) = try!(Deserializable::deserialize_token(d, token)); + $seq.insert(k, v); + } + Some(Err(err)) => { return Err(err); } + None => { return Err(d.end_of_stream_error()); } + } + } + + Ok($seq) + } } } @@ -396,7 +423,10 @@ impl< > Deserializable for HashMap { #[inline] fn deserialize_token(d: &mut D, token: Token) -> Result, E> { - d.expect_map(token) + let len = try!(d.expect_map_start(token)); + let mut value = HashMap::with_capacity(len); + + deserialize_map!(value) } } diff --git a/serde.rs b/serde.rs index 09864cf7..3763b5a5 100644 --- a/serde.rs +++ b/serde.rs @@ -18,7 +18,7 @@ pub mod de; //pub mod bench_bytes; #[cfg(test)] -//pub mod bench_enum; +pub mod bench_enum; #[cfg(test)] //pub mod bench_struct;