From dd7900d62fc28e0aecb5d643f442b2cff58e5403 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 20 Jun 2018 13:10:41 -0700 Subject: [PATCH] Avoid closures in default `ToPrimitive` methods In `to_f64()`, we also try `to_u64()` if `to_i64()` failed. --- src/cast.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/cast.rs b/src/cast.rs index eb1fa70..e10b5e5 100644 --- a/src/cast.rs +++ b/src/cast.rs @@ -13,25 +13,25 @@ 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()) + self.to_i64().as_ref().and_then(ToPrimitive::to_isize) } /// Converts the value of `self` to an `i8`. #[inline] fn to_i8(&self) -> Option { - self.to_i64().and_then(|x| x.to_i8()) + self.to_i64().as_ref().and_then(ToPrimitive::to_i8) } /// Converts the value of `self` to an `i16`. #[inline] fn to_i16(&self) -> Option { - self.to_i64().and_then(|x| x.to_i16()) + self.to_i64().as_ref().and_then(ToPrimitive::to_i16) } /// Converts the value of `self` to an `i32`. #[inline] fn to_i32(&self) -> Option { - self.to_i64().and_then(|x| x.to_i32()) + self.to_i64().as_ref().and_then(ToPrimitive::to_i32) } /// Converts the value of `self` to an `i64`. @@ -52,25 +52,25 @@ pub trait ToPrimitive { /// Converts the value of `self` to a `usize`. #[inline] fn to_usize(&self) -> Option { - self.to_u64().and_then(|x| x.to_usize()) + self.to_u64().as_ref().and_then(ToPrimitive::to_usize) } /// Converts the value of `self` to an `u8`. #[inline] fn to_u8(&self) -> Option { - self.to_u64().and_then(|x| x.to_u8()) + self.to_u64().as_ref().and_then(ToPrimitive::to_u8) } /// Converts the value of `self` to an `u16`. #[inline] fn to_u16(&self) -> Option { - self.to_u64().and_then(|x| x.to_u16()) + self.to_u64().as_ref().and_then(ToPrimitive::to_u16) } /// Converts the value of `self` to an `u32`. #[inline] fn to_u32(&self) -> Option { - self.to_u64().and_then(|x| x.to_u32()) + self.to_u64().as_ref().and_then(ToPrimitive::to_u32) } /// Converts the value of `self` to an `u64`. @@ -92,13 +92,16 @@ pub trait ToPrimitive { /// Converts the value of `self` to an `f32`. #[inline] fn to_f32(&self) -> Option { - self.to_f64().and_then(|x| x.to_f32()) + self.to_f64().as_ref().and_then(ToPrimitive::to_f32) } /// Converts the value of `self` to an `f64`. #[inline] fn to_f64(&self) -> Option { - self.to_i64().and_then(|x| x.to_f64()) + match self.to_i64() { + Some(i) => i.to_f64(), + None => self.to_u64().as_ref().and_then(ToPrimitive::to_f64), + } } }