Trait extprim::traits::ToExtraPrimitive [] [src]

pub trait ToExtraPrimitive: ToPrimitive {
    fn to_u128(&self) -> Option<u128>;
    fn to_i128(&self) -> Option<i128>;
}

Trait for converting itself into the extra primitive types.

Note

Converting f32/f64 to u128/i128 will always succeed, even if they represent values outside of the u128/i128 ranges. They will just return Some(0) on overflow. This is similar to how num_traits::ToPrimitive treat the float conversions.

use extprim::traits::ToExtraPrimitive;
use extprim::u128::u128;
use std::f64;

assert_eq!(680.0f64.to_u128(), Some(u128::new(680)));
assert_eq!(2.0f64.powi(64).to_u128(), Some(u128::from_parts(1, 0)));

// The following examples overflow, but they all still convert to 0.
assert_eq!(2.0f64.powi(128).to_u128(), Some(u128::zero()));
assert_eq!(f64::MAX.to_u128(), Some(u128::zero()));
assert_eq!(f64::INFINITY.to_u128(), Some(u128::zero()));
assert_eq!(f64::NAN.to_u128(), Some(u128::zero()));

Required Methods

fn to_u128(&self) -> Option<u128>

Tries to convert itself into an unsigned 128-bit integer.

fn to_i128(&self) -> Option<i128>

Tries to convert itself into a signed 128-bit integer.

Implementors