Skip to main content

CastFloat

Trait CastFloat 

Source
pub trait CastFloat<T> {
    // Required methods
    fn cast_trunc(self) -> T;
    fn cast_nearest(self) -> T;
    fn cast_floor(self) -> T;
    fn cast_ceil(self) -> T;
    fn try_cast_trunc(self) -> Result<T>;
    fn try_cast_nearest(self) -> Result<T>;
    fn try_cast_floor(self) -> Result<T>;
    fn try_cast_ceil(self) -> Result<T>;
}
Expand description

Like Into, but for ConvFloat

Use:

  • try_cast_* methods to explicitly handle errors

  • cast_* methods only where success is expected. Implementations are permitted to panic or silently return a different (safe, defined) value on error.

    In debug builds, implementations must panic.

    Implementations by this library will panic in debug builds or if the always_assert or assert_float feature flag is used, otherwise conversions have similar behaviour to the as keyword.

This trait is automatically implemented for every implementation of ConvFloat.

Required Methods§

Source

fn cast_trunc(self) -> T

Cast to integer, truncating

Rounds towards zero (same as as).

Source

fn cast_nearest(self) -> T

Cast to the nearest integer

Half-way cases are rounded away from 0.

Source

fn cast_floor(self) -> T

Cast the floor to an integer

Returns the largest integer less than or equal to self.

Source

fn cast_ceil(self) -> T

Cast the ceiling to an integer

Returns the smallest integer greater than or equal to self.

Source

fn try_cast_trunc(self) -> Result<T>

Try converting to integer with truncation

Rounds towards zero (same as as).

Source

fn try_cast_nearest(self) -> Result<T>

Try converting to the nearest integer

Half-way cases are rounded away from 0.

Source

fn try_cast_floor(self) -> Result<T>

Try converting the floor to an integer

Returns the largest integer less than or equal to x.

Source

fn try_cast_ceil(self) -> Result<T>

Try convert the ceiling to an integer

Returns the smallest integer greater than or equal to x.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<S, T: ConvFloat<S>> CastFloat<T> for S

Available on crate features libm or std only.