From 499638eccdac849e82763b9dcee8351df9810f9b Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 3 Aug 2014 12:34:57 -0700 Subject: [PATCH] simplify deserializing a value from a primitive --- src/bench_log.rs | 36 ++++++------------------------------ src/de.rs | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/bench_log.rs b/src/bench_log.rs index 58b18241..0b7ba6ae 100644 --- a/src/bench_log.rs +++ b/src/bench_log.rs @@ -51,11 +51,7 @@ impl de::Deserializable for HttpProtocol { D: de::Deserializer, E >(d: &mut D, token: de::Token) -> Result { - let x: uint = try!(de::Deserializable::deserialize_token(d, token)); - match FromPrimitive::from_uint(x) { - Some(x) => Ok(x), - None => d.syntax_error(), - } + d.expect_from_primitive(token) } } @@ -90,11 +86,7 @@ impl de::Deserializable for HttpMethod { D: de::Deserializer, E >(d: &mut D, token: de::Token) -> Result { - let x: uint = try!(de::Deserializable::deserialize_token(d, token)); - match FromPrimitive::from_uint(x) { - Some(x) => Ok(x), - None => d.syntax_error(), - } + d.expect_from_primitive(token) } } @@ -122,11 +114,7 @@ impl de::Deserializable for CacheStatus { D: de::Deserializer, E >(d: &mut D, token: de::Token) -> Result { - let x: uint = try!(de::Deserializable::deserialize_token(d, token)); - match FromPrimitive::from_uint(x) { - Some(x) => Ok(x), - None => d.syntax_error(), - } + d.expect_from_primitive(token) } } @@ -163,11 +151,7 @@ impl de::Deserializable for OriginProtocol { D: de::Deserializer, E >(d: &mut D, token: de::Token) -> Result { - let x: uint = try!(de::Deserializable::deserialize_token(d, token)); - match FromPrimitive::from_uint(x) { - Some(x) => Ok(x), - None => d.syntax_error(), - } + d.expect_from_primitive(token) } } @@ -196,11 +180,7 @@ impl de::Deserializable for ZonePlan { D: de::Deserializer, E >(d: &mut D, token: de::Token) -> Result { - let x: uint = try!(de::Deserializable::deserialize_token(d, token)); - match FromPrimitive::from_uint(x) { - Some(x) => Ok(x), - None => d.syntax_error(), - } + d.expect_from_primitive(token) } } @@ -480,11 +460,7 @@ impl de::Deserializable for Country { D: de::Deserializer, E >(d: &mut D, token: de::Token) -> Result { - let x: uint = try!(de::Deserializable::deserialize_token(d, token)); - match FromPrimitive::from_uint(x) { - Some(x) => Ok(x), - None => d.syntax_error(), - } + d.expect_from_primitive(token) } } diff --git a/src/de.rs b/src/de.rs index 7ca6cbc3..e7ce7538 100644 --- a/src/de.rs +++ b/src/de.rs @@ -102,6 +102,22 @@ pub trait Deserializer: Iterator> { U64(x) => to_result!(num::cast(x), self.syntax_error()), F32(x) => to_result!(num::cast(x), self.syntax_error()), F64(x) => to_result!(num::cast(x), self.syntax_error()), + + #[inline] + fn expect_from_primitive(&mut self, token: Token) -> Result { + match token { + Int(x) => to_result!(num::from_int(x), self.syntax_error()), + I8(x) => to_result!(num::from_i8(x), self.syntax_error()), + I16(x) => to_result!(num::from_i16(x), self.syntax_error()), + I32(x) => to_result!(num::from_i32(x), self.syntax_error()), + I64(x) => to_result!(num::from_i64(x), self.syntax_error()), + Uint(x) => to_result!(num::from_uint(x), self.syntax_error()), + U8(x) => to_result!(num::from_u8(x), self.syntax_error()), + U16(x) => to_result!(num::from_u16(x), self.syntax_error()), + U32(x) => to_result!(num::from_u32(x), self.syntax_error()), + U64(x) => to_result!(num::from_u64(x), self.syntax_error()), + F32(x) => to_result!(num::from_f32(x), self.syntax_error()), + F64(x) => to_result!(num::from_f64(x), self.syntax_error()), _ => self.syntax_error(), } }