pub trait CastApprox<T> {
type Error: Into<RangeError> + Error;
// Required methods
fn try_cast_approx(self) -> Result<T, Self::Error>;
fn cast_approx(self) -> T;
}Expand description
Like Into, but for ConvApprox
This trait supports Into- and TryInto-like conversions, but allowing
approximation:
- Conversions may be fallible, like
TryInto. - Conversions may be lossy, provided that the result is close to the
input value (see
§ Limits of approximation). - Conversions must be value-preserving, like
Into. For example,-1_i8and-1_i32are conceptually the same value while255_u8is conceptually a different value.
The rounding mode used is implementation-defined. Conversions provided by
this crate use the same behaviour as as numeric casts: float-to-int
conversions round towards zero while conversions to floating-point formats
produce the closest possible float (rounding ties to even).
Use CastTo where specific rounding is required.
This trait is automatically implemented for every implementation of
ConvApprox.
Required Associated Types§
Sourcetype Error: Into<RangeError> + Error
type Error: Into<RangeError> + Error
Conversion error type
This should be either Infallible or RangeError.
Required Methods§
Sourcefn try_cast_approx(self) -> Result<T, Self::Error>
fn try_cast_approx(self) -> Result<T, Self::Error>
Try approximate conversion from Self to T
Sourcefn cast_approx(self) -> T
fn cast_approx(self) -> T
Cast approximately from Self to T
Use this method only when success is expected. On error, this method may panic or may exhibit § Fallback behaviour.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".