pub trait CastFloat<T> {
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
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
orassert_float
feature flag is used, otherwise conversions have similar behaviour to theas
keyword.
This trait is automatically implemented for every implementation of
ConvFloat
.
Required Methods§
sourcefn cast_trunc(self) -> T
fn cast_trunc(self) -> T
Cast to integer, truncating
Rounds towards zero (same as as
).
sourcefn cast_nearest(self) -> T
fn cast_nearest(self) -> T
Cast to the nearest integer
Half-way cases are rounded away from 0
.
sourcefn cast_floor(self) -> T
fn cast_floor(self) -> T
Cast the floor to an integer
Returns the largest integer less than or equal to self
.
sourcefn cast_ceil(self) -> T
fn cast_ceil(self) -> T
Cast the ceiling to an integer
Returns the smallest integer greater than or equal to self
.
sourcefn try_cast_trunc(self) -> Result<T>
fn try_cast_trunc(self) -> Result<T>
Try converting to integer with truncation
Rounds towards zero (same as as
).
sourcefn try_cast_nearest(self) -> Result<T>
fn try_cast_nearest(self) -> Result<T>
Try converting to the nearest integer
Half-way cases are rounded away from 0
.
sourcefn try_cast_floor(self) -> Result<T>
fn try_cast_floor(self) -> Result<T>
Try converting the floor to an integer
Returns the largest integer less than or equal to x
.
sourcefn try_cast_ceil(self) -> Result<T>
fn try_cast_ceil(self) -> Result<T>
Try convert the ceiling to an integer
Returns the smallest integer greater than or equal to x
.