pub trait ConvApprox<S>: Sized {
type Error: Into<RangeError> + Error;
// Required method
fn try_conv_approx(s: S) -> Result<Self, Self::Error>;
// Provided method
fn conv_approx(s: S) -> Self { ... }
}Expand description
Like From, but for approximate numerical conversions
This trait supports From- and TryFrom-like conversions, but allowing
approximation:
- Conversions may be fallible, like
TryFrom. - Conversions may be lossy, provided that the result is close to the
input value (see
§ Limits of approximation). - Conversions must be value-preserving, like
From. 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 ConvTo where specific rounding is required.
The sister-trait CastApprox supports “into” style usage.
This trait should not be implemented directly; instead implement ConvTo
using the Approx rounding mode.
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_conv_approx(s: S) -> Result<Self, Self::Error>
fn try_conv_approx(s: S) -> Result<Self, Self::Error>
Try converting from S to Self, allowing approximation
Provided Methods§
Sourcefn conv_approx(s: S) -> Self
fn conv_approx(s: S) -> Self
Convert from S to Self, allowing approximation
Use this method only when success is expected. On error, this method may panic or may exhibit § Fallback behaviour.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".