From f249edf546ad38514897bdee3db665c3c8b75139 Mon Sep 17 00:00:00 2001 From: Yoan Lecoq Date: Sat, 29 Apr 2017 08:04:59 +0200 Subject: [PATCH] impl ToPrimitive, FromPrimitive and NumCast for Wrapping --- traits/src/cast.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/traits/src/cast.rs b/traits/src/cast.rs index 8ff03fd..eda58f5 100644 --- a/traits/src/cast.rs +++ b/traits/src/cast.rs @@ -1,4 +1,5 @@ use std::mem::size_of; +use std::num::Wrapping; use identities::Zero; use bounds::Bounded; @@ -385,6 +386,17 @@ impl_from_primitive!(u64, to_u64); impl_from_primitive!(f32, to_f32); impl_from_primitive!(f64, to_f64); + +impl ToPrimitive for Wrapping { + fn to_i64(&self) -> Option { self.0.to_i64() } + fn to_u64(&self) -> Option { self.0.to_u64() } +} +impl FromPrimitive for Wrapping { + fn from_u64(n: u64) -> Option { T::from_u64(n).map(Wrapping) } + fn from_i64(n: i64) -> Option { T::from_i64(n).map(Wrapping) } +} + + /// Cast from one machine scalar to another. /// /// # Examples @@ -434,6 +446,11 @@ impl_num_cast!(isize, to_isize); impl_num_cast!(f32, to_f32); impl_num_cast!(f64, to_f64); +impl NumCast for Wrapping { + fn from(n: U) -> Option { + T::from(n).map(Wrapping) + } +} #[test] fn to_primitive_float() {