PrimitiveFloatToInt

Trait PrimitiveFloatToInt 

Source
pub trait PrimitiveFloatToInt<Int> { }
Expand description

Trait for conversions supported by PrimitiveFloat::to_int_unchecked.

This is effectively the same as the unstable core::convert::FloatToInt, implemented for all combinations of PrimitiveFloat and PrimitiveInteger.

§Examples

PrimitiveFloatToInt<{integer}> is a supertrait of PrimitiveFloat for all primitive integers, so you do not need to use this trait directly with concrete integer types.

use num_primitive::PrimitiveFloat;

fn pi<Float: PrimitiveFloat>() -> i32 {
    // SAFETY: π is finite, and truncated to 3 fits any int
    unsafe { Float::PI.to_int_unchecked() }
}

assert_eq!(pi::<f32>(), 3i32);
assert_eq!(pi::<f64>(), 3i32);

However, if the integer type is also generic, an explicit type constraint is needed.

use num_primitive::{PrimitiveFloat, PrimitiveFloatToInt};

fn tau<Float, Int>() -> Int
where
    Float: PrimitiveFloat + PrimitiveFloatToInt<Int>,
{
    // SAFETY: τ is finite, and truncated to 6 fits any int
    unsafe { Float::TAU.to_int_unchecked() }
}

assert_eq!(tau::<f32, i64>(), 6i64);
assert_eq!(tau::<f64, u8>(), 6u8);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl PrimitiveFloatToInt<i8> for f32

Source§

impl PrimitiveFloatToInt<i8> for f64

Source§

impl PrimitiveFloatToInt<i16> for f32

Source§

impl PrimitiveFloatToInt<i16> for f64

Source§

impl PrimitiveFloatToInt<i32> for f32

Source§

impl PrimitiveFloatToInt<i32> for f64

Source§

impl PrimitiveFloatToInt<i64> for f32

Source§

impl PrimitiveFloatToInt<i64> for f64

Source§

impl PrimitiveFloatToInt<i128> for f32

Source§

impl PrimitiveFloatToInt<i128> for f64

Source§

impl PrimitiveFloatToInt<isize> for f32

Source§

impl PrimitiveFloatToInt<isize> for f64

Source§

impl PrimitiveFloatToInt<u8> for f32

Source§

impl PrimitiveFloatToInt<u8> for f64

Source§

impl PrimitiveFloatToInt<u16> for f32

Source§

impl PrimitiveFloatToInt<u16> for f64

Source§

impl PrimitiveFloatToInt<u32> for f32

Source§

impl PrimitiveFloatToInt<u32> for f64

Source§

impl PrimitiveFloatToInt<u64> for f32

Source§

impl PrimitiveFloatToInt<u64> for f64

Source§

impl PrimitiveFloatToInt<u128> for f32

Source§

impl PrimitiveFloatToInt<u128> for f64

Source§

impl PrimitiveFloatToInt<usize> for f32

Source§

impl PrimitiveFloatToInt<usize> for f64

Implementors§