From 1a63cbccb21a59e5f18624d30ab613b2421ee780 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 5 Apr 2017 12:29:05 -0700 Subject: [PATCH] Remove unused ToPrimitive trait --- serde/src/de/from_primitive.rs | 212 ++++++++++----------------------- 1 file changed, 62 insertions(+), 150 deletions(-) diff --git a/serde/src/de/from_primitive.rs b/serde/src/de/from_primitive.rs index 75f56b47..d881cd78 100644 --- a/serde/src/de/from_primitive.rs +++ b/serde/src/de/from_primitive.rs @@ -51,49 +51,7 @@ bounded_impl!(i16, i16::MIN, i16::MAX); bounded_impl!(i32, i32::MIN, i32::MAX); bounded_impl!(i64, i64::MIN, i64::MAX); -bounded_impl!(f32, f32::MIN, f32::MAX); -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`. - fn to_isize(&self) -> Option; - - /// Converts the value of `self` to an `i8`. - fn to_i8(&self) -> Option; - - /// Converts the value of `self` to an `i16`. - fn to_i16(&self) -> Option; - - /// Converts the value of `self` to an `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`. - fn to_usize(&self) -> Option; - - /// Converts the value of `self` to an `u8`. - fn to_u8(&self) -> Option; - - /// Converts the value of `self` to an `u16`. - fn to_u16(&self) -> Option; - - /// Converts the value of `self` to an `u32`. - fn to_u32(&self) -> Option; - - /// Converts the value of `self` to an `u64`. - fn to_u64(&self) -> Option; - - /// Converts the value of `self` to an `f32`. - fn to_f32(&self) -> Option; - - /// Converts the value of `self` to an `f64`. - fn to_f64(&self) -> Option; -} - -macro_rules! impl_to_primitive_int_to_int { +macro_rules! int_to_int { ($SrcT:ty, $DstT:ty, $slf:expr) => ( { if size_of::<$SrcT>() <= size_of::<$DstT>() { @@ -112,7 +70,7 @@ macro_rules! impl_to_primitive_int_to_int { ) } -macro_rules! impl_to_primitive_int_to_uint { +macro_rules! int_to_uint { ($SrcT:ty, $DstT:ty, $slf:expr) => ( { let zero: $SrcT = 0; @@ -126,46 +84,7 @@ macro_rules! impl_to_primitive_int_to_uint { ) } -macro_rules! impl_to_primitive_int { - ($T:ty) => ( - impl ToPrimitive for $T { - #[inline] - fn to_isize(&self) -> Option { impl_to_primitive_int_to_int!($T, isize, *self) } - #[inline] - fn to_i8(&self) -> Option { impl_to_primitive_int_to_int!($T, i8, *self) } - #[inline] - fn to_i16(&self) -> Option { impl_to_primitive_int_to_int!($T, i16, *self) } - #[inline] - fn to_i32(&self) -> Option { impl_to_primitive_int_to_int!($T, i32, *self) } - #[inline] - fn to_i64(&self) -> Option { impl_to_primitive_int_to_int!($T, i64, *self) } - - #[inline] - fn to_usize(&self) -> Option { impl_to_primitive_int_to_uint!($T, usize, *self) } - #[inline] - fn to_u8(&self) -> Option { impl_to_primitive_int_to_uint!($T, u8, *self) } - #[inline] - fn to_u16(&self) -> Option { impl_to_primitive_int_to_uint!($T, u16, *self) } - #[inline] - fn to_u32(&self) -> Option { impl_to_primitive_int_to_uint!($T, u32, *self) } - #[inline] - fn to_u64(&self) -> Option { impl_to_primitive_int_to_uint!($T, u64, *self) } - - #[inline] - fn to_f32(&self) -> Option { Some(*self as f32) } - #[inline] - fn to_f64(&self) -> Option { Some(*self as f64) } - } - ) -} - -impl_to_primitive_int! { isize } -impl_to_primitive_int! { i8 } -impl_to_primitive_int! { i16 } -impl_to_primitive_int! { i32 } -impl_to_primitive_int! { i64 } - -macro_rules! impl_to_primitive_uint_to_int { +macro_rules! uint_to_int { ($DstT:ty, $slf:expr) => ( { let max_value: $DstT = Bounded::max_value(); @@ -178,7 +97,7 @@ macro_rules! impl_to_primitive_uint_to_int { ) } -macro_rules! impl_to_primitive_uint_to_uint { +macro_rules! uint_to_uint { ($SrcT:ty, $DstT:ty, $slf:expr) => ( { if size_of::<$SrcT>() <= size_of::<$DstT>() { @@ -196,47 +115,6 @@ macro_rules! impl_to_primitive_uint_to_uint { ) } -macro_rules! impl_to_primitive_uint { - ($T:ty) => ( - impl ToPrimitive for $T { - #[inline] - fn to_isize(&self) -> Option { impl_to_primitive_uint_to_int!(isize, *self) } - #[inline] - fn to_i8(&self) -> Option { impl_to_primitive_uint_to_int!(i8, *self) } - #[inline] - fn to_i16(&self) -> Option { impl_to_primitive_uint_to_int!(i16, *self) } - #[inline] - fn to_i32(&self) -> Option { impl_to_primitive_uint_to_int!(i32, *self) } - #[inline] - fn to_i64(&self) -> Option { impl_to_primitive_uint_to_int!(i64, *self) } - - #[inline] - fn to_usize(&self) -> Option { - impl_to_primitive_uint_to_uint!($T, usize, *self) - } - #[inline] - fn to_u8(&self) -> Option { impl_to_primitive_uint_to_uint!($T, u8, *self) } - #[inline] - fn to_u16(&self) -> Option { impl_to_primitive_uint_to_uint!($T, u16, *self) } - #[inline] - fn to_u32(&self) -> Option { impl_to_primitive_uint_to_uint!($T, u32, *self) } - #[inline] - fn to_u64(&self) -> Option { impl_to_primitive_uint_to_uint!($T, u64, *self) } - - #[inline] - fn to_f32(&self) -> Option { Some(*self as f32) } - #[inline] - fn to_f64(&self) -> Option { Some(*self as f64) } - } - ) -} - -impl_to_primitive_uint! { usize } -impl_to_primitive_uint! { u8 } -impl_to_primitive_uint! { u16 } -impl_to_primitive_uint! { u32 } -impl_to_primitive_uint! { u64 } - pub trait FromPrimitive: Sized { fn from_isize(n: isize) -> Option; fn from_i8(n: i8) -> Option; @@ -250,32 +128,66 @@ pub trait FromPrimitive: Sized { fn from_u64(n: u64) -> Option; } -macro_rules! impl_from_primitive { - ($T:ty, $to_ty:ident) => ( +macro_rules! impl_from_primitive_for_int { + ($T:ty) => ( 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_isize(n: isize) -> Option { int_to_int!(isize, $T, n) } + #[inline] fn from_i8(n: i8) -> Option { int_to_int!(i8, $T, n) } + #[inline] fn from_i16(n: i16) -> Option { int_to_int!(i16, $T, n) } + #[inline] fn from_i32(n: i32) -> Option { int_to_int!(i32, $T, n) } + #[inline] fn from_i64(n: i64) -> Option { int_to_int!(i64, $T, n) } + #[inline] fn from_usize(n: usize) -> Option { uint_to_int!($T, n) } + #[inline] fn from_u8(n: u8) -> Option { uint_to_int!($T, n) } + #[inline] fn from_u16(n: u16) -> Option { uint_to_int!($T, n) } + #[inline] fn from_u32(n: u32) -> Option { uint_to_int!($T, n) } + #[inline] fn from_u64(n: u64) -> Option { uint_to_int!($T, n) } } ) } -impl_from_primitive! { isize, to_isize } -impl_from_primitive! { i8, to_i8 } -impl_from_primitive! { i16, to_i16 } -impl_from_primitive! { i32, to_i32 } -impl_from_primitive! { i64, to_i64 } -impl_from_primitive! { usize, to_usize } -impl_from_primitive! { u8, to_u8 } -impl_from_primitive! { u16, to_u16 } -impl_from_primitive! { u32, to_u32 } -impl_from_primitive! { u64, to_u64 } -impl_from_primitive! { f32, to_f32 } -impl_from_primitive! { f64, to_f64 } +macro_rules! impl_from_primitive_for_uint { + ($T:ty) => ( + impl FromPrimitive for $T { + #[inline] fn from_isize(n: isize) -> Option { int_to_uint!(isize, $T, n) } + #[inline] fn from_i8(n: i8) -> Option { int_to_uint!(i8, $T, n) } + #[inline] fn from_i16(n: i16) -> Option { int_to_uint!(i16, $T, n) } + #[inline] fn from_i32(n: i32) -> Option { int_to_uint!(i32, $T, n) } + #[inline] fn from_i64(n: i64) -> Option { int_to_uint!(i64, $T, n) } + #[inline] fn from_usize(n: usize) -> Option { uint_to_uint!(usize, $T, n) } + #[inline] fn from_u8(n: u8) -> Option { uint_to_uint!(u8, $T, n) } + #[inline] fn from_u16(n: u16) -> Option { uint_to_uint!(u16, $T, n) } + #[inline] fn from_u32(n: u32) -> Option { uint_to_uint!(u32, $T, n) } + #[inline] fn from_u64(n: u64) -> Option { uint_to_uint!(u64, $T, n) } + } + ) +} + +macro_rules! impl_from_primitive_for_float { + ($T:ty) => ( + impl FromPrimitive for $T { + #[inline] fn from_isize(n: isize) -> Option { Some(n as Self) } + #[inline] fn from_i8(n: i8) -> Option { Some(n as Self) } + #[inline] fn from_i16(n: i16) -> Option { Some(n as Self) } + #[inline] fn from_i32(n: i32) -> Option { Some(n as Self) } + #[inline] fn from_i64(n: i64) -> Option { Some(n as Self) } + #[inline] fn from_usize(n: usize) -> Option { Some(n as Self) } + #[inline] fn from_u8(n: u8) -> Option { Some(n as Self) } + #[inline] fn from_u16(n: u16) -> Option { Some(n as Self) } + #[inline] fn from_u32(n: u32) -> Option { Some(n as Self) } + #[inline] fn from_u64(n: u64) -> Option { Some(n as Self) } + } + ) +} + +impl_from_primitive_for_int!(isize); +impl_from_primitive_for_int!(i8); +impl_from_primitive_for_int!(i16); +impl_from_primitive_for_int!(i32); +impl_from_primitive_for_int!(i64); +impl_from_primitive_for_uint!(usize); +impl_from_primitive_for_uint!(u8); +impl_from_primitive_for_uint!(u16); +impl_from_primitive_for_uint!(u32); +impl_from_primitive_for_uint!(u64); +impl_from_primitive_for_float!(f32); +impl_from_primitive_for_float!(f64);