Trait ConvTo Copy item path Source pub trait ConvTo<S, R: Rounding >: Sized {
type Error : Into <R::MaximumError > + Error ;
// Required method
fn try_conv_to (mode: R, s: S) -> Result <Self, Self::Error >;
// Provided method
fn conv_to (mode: R, s: S) -> Self { ... }
}Expand description Generic “from” conversion trait with specified rounding mode
This trait supports From - and TryFrom -like conversions, but with a
specified rounding mode:
Conversions may be fallible , like TryFrom .
Conversions may be lossy , according to the Rounding mode used.
Conversions must be value-preserving , like From . For example,
-1_i8 and -1_i32 are conceptually the same value while 255_u8 is
conceptually a different value.
The Rounding mode must be specified:
assert_eq! (i32::conv_to(Nearest, 7.6f32 ), 8 );
assert_eq! (f32::conv_to(Exact, 20 ), 20.0 );
Usage with Exact and Approx is equivalent to usage of Conv and
ConvApprox respectively.
The sister-trait CastTo supports “into” style usage.
§ Implementing conversions
Implement conversions which cannot lose precision using ConvExact and
other conversions using this trait. Separate implementations may be provided
for each Rounding mode.
§ Example
use easy_cast::{Approx, ConvTo, Error, Exact, RangeError};
struct MyFloat { }
impl ConvTo<MyFloat, Exact> for i32 {
type Error = Error;
fn try_conv_to(_ : Exact, f: MyFloat) -> Result <Self , Self ::Error> {
if f.is_in_range_of::<i32>() {
if f.is_integral() {
Ok (todo! ())
} else {
Err (Error::Inexact)
}
} else {
Err (Error::Range)
}
}
}
impl ConvTo<MyFloat, Approx> for i32 {
type Error = RangeError;
fn try_conv_to(_ : Approx, f: MyFloat) -> Result <Self , Self ::Error> {
if f.is_in_range_of::<i32>() {
Ok (todo! ())
} else {
Err (RangeError)
}
}
}
Try converting from S to Self, rounding according to mode
Convert from S to Self, rounding according to mode
Use this method only when success is expected. On error, this method may
panic or may exhibit § Fallback behaviour .
§ Implementing
Implementing this method directly (with fallback behaviour) is optional.
In debug builds, this method must panic on error.
This trait is not dyn compatible .
In older versions of Rust, dyn compatibility was called "object safety".
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.
Source § Available on crate features libm or std only.