impl ToPrimitive, FromPrimitive and NumCast for Wrapping<T>

This commit is contained in:
Yoan Lecoq 2017-04-29 08:04:59 +02:00
parent 35a9bcba19
commit f249edf546
1 changed files with 17 additions and 0 deletions

View File

@ -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<T: ToPrimitive> ToPrimitive for Wrapping<T> {
fn to_i64(&self) -> Option<i64> { self.0.to_i64() }
fn to_u64(&self) -> Option<u64> { self.0.to_u64() }
}
impl<T: FromPrimitive> FromPrimitive for Wrapping<T> {
fn from_u64(n: u64) -> Option<Self> { T::from_u64(n).map(Wrapping) }
fn from_i64(n: i64) -> Option<Self> { 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<T: NumCast> NumCast for Wrapping<T> {
fn from<U: ToPrimitive>(n: U) -> Option<Self> {
T::from(n).map(Wrapping)
}
}
#[test]
fn to_primitive_float() {