Trait easy_cast::traits::CastApprox
source · pub trait CastApprox<T> {
fn try_cast_approx(self) -> Result<T>;
fn cast_approx(self) -> T;
}
Expand description
Like Into
, but for ConvApprox
On success, the result must be approximately the same as the input value:
the difference must be smaller than the precision of the target type.
For example, one may have 1.9f32.cast_approx() = 1
.
Precise rounding mode should usually be truncation (round towards zero),
but this is not required. Use CastFloat
where a specific rounding mode
is required.
This trait is automatically implemented for every implementation of
ConvApprox
.
Required Methods§
sourcefn try_cast_approx(self) -> Result<T>
fn try_cast_approx(self) -> Result<T>
Try approximate conversion from Self
to T
Use this method to explicitly handle errors.
sourcefn cast_approx(self) -> T
fn cast_approx(self) -> T
Cast approximately from Self
to T
Use this method 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
feature flag is used, otherwise conversions have the
same behaviour as the as
keyword.