From 07ac9eb53808898d6b44f0e8ac4ab8ad54b0bd95 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 5 Apr 2017 12:11:47 -0700 Subject: [PATCH] Remove unused number conversion logic --- serde/src/de/from_primitive.rs | 168 ++++----------------------------- serde/src/de/impls.rs | 39 +++++--- 2 files changed, 45 insertions(+), 162 deletions(-) diff --git a/serde/src/de/from_primitive.rs b/serde/src/de/from_primitive.rs index 14e51860..75f56b47 100644 --- a/serde/src/de/from_primitive.rs +++ b/serde/src/de/from_primitive.rs @@ -57,71 +57,40 @@ bounded_impl!(f64, f64::MIN, f64::MAX); /// A generic trait for converting a value to a number. pub trait ToPrimitive { /// Converts the value of `self` to an `isize`. - #[inline] - fn to_isize(&self) -> Option { - self.to_i64().and_then(|x| x.to_isize()) - } + fn to_isize(&self) -> Option; /// Converts the value of `self` to an `i8`. - #[inline] - fn to_i8(&self) -> Option { - self.to_i64().and_then(|x| x.to_i8()) - } + fn to_i8(&self) -> Option; /// Converts the value of `self` to an `i16`. - #[inline] - fn to_i16(&self) -> Option { - self.to_i64().and_then(|x| x.to_i16()) - } + fn to_i16(&self) -> Option; /// Converts the value of `self` to an `i32`. - #[inline] - fn to_i32(&self) -> Option { - self.to_i64().and_then(|x| x.to_i32()) - } + fn to_i32(&self) -> Option; /// Converts the value of `self` to an `i64`. fn to_i64(&self) -> Option; /// Converts the value of `self` to a `usize`. - #[inline] - fn to_usize(&self) -> Option { - self.to_u64().and_then(|x| x.to_usize()) - } + fn to_usize(&self) -> Option; /// Converts the value of `self` to an `u8`. - #[inline] - fn to_u8(&self) -> Option { - self.to_u64().and_then(|x| x.to_u8()) - } + fn to_u8(&self) -> Option; /// Converts the value of `self` to an `u16`. - #[inline] - fn to_u16(&self) -> Option { - self.to_u64().and_then(|x| x.to_u16()) - } + fn to_u16(&self) -> Option; /// Converts the value of `self` to an `u32`. - #[inline] - fn to_u32(&self) -> Option { - self.to_u64().and_then(|x| x.to_u32()) - } + fn to_u32(&self) -> Option; /// Converts the value of `self` to an `u64`. - #[inline] fn to_u64(&self) -> Option; /// Converts the value of `self` to an `f32`. - #[inline] - fn to_f32(&self) -> Option { - self.to_f64().and_then(|x| x.to_f32()) - } + fn to_f32(&self) -> Option; /// Converts the value of `self` to an `f64`. - #[inline] - fn to_f64(&self) -> Option { - self.to_i64().and_then(|x| x.to_f64()) - } + fn to_f64(&self) -> Option; } macro_rules! impl_to_primitive_int_to_int { @@ -268,129 +237,32 @@ impl_to_primitive_uint! { u16 } impl_to_primitive_uint! { u32 } impl_to_primitive_uint! { u64 } -macro_rules! impl_to_primitive_float_to_float { - ($SrcT:ident, $DstT:ident, $slf:expr) => ( - if size_of::<$SrcT>() <= size_of::<$DstT>() { - Some($slf as $DstT) - } else { - let n = $slf as f64; - let max_value: $SrcT = ::core::$SrcT::MAX; - if -max_value as f64 <= n && n <= max_value as f64 { - Some($slf as $DstT) - } else { - None - } - } - ) -} - -macro_rules! impl_to_primitive_float { - ($T:ident) => ( - impl ToPrimitive for $T { - #[inline] - fn to_isize(&self) -> Option { Some(*self as isize) } - #[inline] - fn to_i8(&self) -> Option { Some(*self as i8) } - #[inline] - fn to_i16(&self) -> Option { Some(*self as i16) } - #[inline] - fn to_i32(&self) -> Option { Some(*self as i32) } - #[inline] - fn to_i64(&self) -> Option { Some(*self as i64) } - - #[inline] - fn to_usize(&self) -> Option { Some(*self as usize) } - #[inline] - fn to_u8(&self) -> Option { Some(*self as u8) } - #[inline] - fn to_u16(&self) -> Option { Some(*self as u16) } - #[inline] - fn to_u32(&self) -> Option { Some(*self as u32) } - #[inline] - fn to_u64(&self) -> Option { Some(*self as u64) } - - #[inline] - fn to_f32(&self) -> Option { impl_to_primitive_float_to_float!($T, f32, *self) } - #[inline] - fn to_f64(&self) -> Option { impl_to_primitive_float_to_float!($T, f64, *self) } - } - ) -} - -impl_to_primitive_float! { f32 } -impl_to_primitive_float! { f64 } - pub trait FromPrimitive: Sized { - #[inline] - fn from_isize(n: isize) -> Option { - FromPrimitive::from_i64(n as i64) - } - - #[inline] - fn from_i8(n: i8) -> Option { - FromPrimitive::from_i64(n as i64) - } - - #[inline] - fn from_i16(n: i16) -> Option { - FromPrimitive::from_i64(n as i64) - } - - #[inline] - fn from_i32(n: i32) -> Option { - FromPrimitive::from_i64(n as i64) - } - + fn from_isize(n: isize) -> Option; + fn from_i8(n: i8) -> Option; + fn from_i16(n: i16) -> Option; + fn from_i32(n: i32) -> Option; fn from_i64(n: i64) -> Option; - - #[inline] - fn from_usize(n: usize) -> Option { - FromPrimitive::from_u64(n as u64) - } - - #[inline] - fn from_u8(n: u8) -> Option { - FromPrimitive::from_u64(n as u64) - } - - #[inline] - fn from_u16(n: u16) -> Option { - FromPrimitive::from_u64(n as u64) - } - - #[inline] - fn from_u32(n: u32) -> Option { - FromPrimitive::from_u64(n as u64) - } - + fn from_usize(n: usize) -> Option; + fn from_u8(n: u8) -> Option; + fn from_u16(n: u16) -> Option; + fn from_u32(n: u32) -> Option; fn from_u64(n: u64) -> Option; - - #[inline] - fn from_f32(n: f32) -> Option { - FromPrimitive::from_f64(n as f64) - } - - #[inline] - fn from_f64(n: f64) -> Option { - FromPrimitive::from_i64(n as i64) - } } macro_rules! impl_from_primitive { ($T:ty, $to_ty:ident) => ( impl FromPrimitive for $T { + #[inline] fn from_isize(n: isize) -> Option<$T> { n.$to_ty() } #[inline] fn from_i8(n: i8) -> Option<$T> { n.$to_ty() } #[inline] fn from_i16(n: i16) -> Option<$T> { n.$to_ty() } #[inline] fn from_i32(n: i32) -> Option<$T> { n.$to_ty() } #[inline] fn from_i64(n: i64) -> Option<$T> { n.$to_ty() } - + #[inline] fn from_usize(n: usize) -> Option<$T> { n.$to_ty() } #[inline] fn from_u8(n: u8) -> Option<$T> { n.$to_ty() } #[inline] fn from_u16(n: u16) -> Option<$T> { n.$to_ty() } #[inline] fn from_u32(n: u32) -> Option<$T> { n.$to_ty() } #[inline] fn from_u64(n: u64) -> Option<$T> { n.$to_ty() } - - #[inline] fn from_f32(n: f32) -> Option<$T> { n.$to_ty() } - #[inline] fn from_f64(n: f64) -> Option<$T> { n.$to_ty() } } ) } diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 9fb37e64..9f666729 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -115,11 +115,11 @@ impl<'de> Deserialize<'de> for bool { /////////////////////////////////////////////////////////////////////////////// -macro_rules! impl_deserialize_num_method { - ($ty:ident, $src_ty:ident, $method:ident, $from_method:ident, $group:ident, $group_ty:ident) => { +macro_rules! visit_integer_method { + ($src_ty:ident, $method:ident, $from_method:ident, $group:ident, $group_ty:ident) => { #[inline] - fn $method(self, v: $src_ty) -> Result<$ty, E> - where E: Error, + fn $method(self, v: $src_ty) -> Result + where E: Error { match FromPrimitive::$from_method(v) { Some(v) => Ok(v), @@ -129,6 +129,17 @@ macro_rules! impl_deserialize_num_method { } } +macro_rules! visit_float_method { + ($src_ty:ident, $method:ident) => { + #[inline] + fn $method(self, v: $src_ty) -> Result + where E: Error + { + Ok(v as Self::Value) + } + } +} + macro_rules! impl_deserialize_num { ($ty:ident, $method:ident, $($visit:ident),*) => { impl<'de> Deserialize<'de> for $ty { @@ -155,18 +166,18 @@ macro_rules! impl_deserialize_num { } }; (integer $ty:ident) => { - impl_deserialize_num_method!($ty, i8, visit_i8, from_i8, Signed, i64); - impl_deserialize_num_method!($ty, i16, visit_i16, from_i16, Signed, i64); - impl_deserialize_num_method!($ty, i32, visit_i32, from_i32, Signed, i64); - impl_deserialize_num_method!($ty, i64, visit_i64, from_i64, Signed, i64); - impl_deserialize_num_method!($ty, u8, visit_u8, from_u8, Unsigned, u64); - impl_deserialize_num_method!($ty, u16, visit_u16, from_u16, Unsigned, u64); - impl_deserialize_num_method!($ty, u32, visit_u32, from_u32, Unsigned, u64); - impl_deserialize_num_method!($ty, u64, visit_u64, from_u64, Unsigned, u64); + visit_integer_method!(i8, visit_i8, from_i8, Signed, i64); + visit_integer_method!(i16, visit_i16, from_i16, Signed, i64); + visit_integer_method!(i32, visit_i32, from_i32, Signed, i64); + visit_integer_method!(i64, visit_i64, from_i64, Signed, i64); + visit_integer_method!(u8, visit_u8, from_u8, Unsigned, u64); + visit_integer_method!(u16, visit_u16, from_u16, Unsigned, u64); + visit_integer_method!(u32, visit_u32, from_u32, Unsigned, u64); + visit_integer_method!(u64, visit_u64, from_u64, Unsigned, u64); }; (float $ty:ident) => { - impl_deserialize_num_method!($ty, f32, visit_f32, from_f32, Float, f64); - impl_deserialize_num_method!($ty, f64, visit_f64, from_f64, Float, f64); + visit_float_method!(f32, visit_f32); + visit_float_method!(f64, visit_f64); }; }