easy_cast

Trait ConvApprox

Source
pub trait ConvApprox<T>: Sized {
    // Required method
    fn try_conv_approx(x: T) -> Result<Self>;

    // Provided method
    fn conv_approx(x: T) -> Self { ... }
}
Expand description

Like From, but for approximate numerical conversions

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 i32::conv_approx(1.9f32) = 1 or f32::conv_approx(1f64 + (f32::EPSILON as f64) / 2.0) = 1.0.

Precise rounding mode should usually be truncation (round towards zero), but this is not required. Use ConvFloat where a specific rounding mode is required.

The sister-trait CastApprox supports “into” style usage.

Required Methods§

Source

fn try_conv_approx(x: T) -> Result<Self>

Try converting from T to Self, allowing approximation of value

This conversion may truncate excess precision not supported by the target type, so long as the value is approximately equal, from the point of view of precision of the target type.

This method should allow approximate conversion, but fail on input not (approximately) in the target’s range.

Provided Methods§

Source

fn conv_approx(x: T) -> Self

Converting from T to Self, allowing approximation of value

This method must return the same result as Self::try_conv_approx where that method succeeds, but differs in the handling of errors:

  • In debug builds the method panics on error
  • Otherwise, the method may panic or may return a different value, but like with the as keyword all results must be well-defined and safe.

Default implementations use Self::try_conv_approx and panic on error. Implementations provided by this library will panic in debug builds or if the always_assert feature flag is used, and otherwise will behave identically to the as keyword.

This mirrors the behaviour of Rust’s overflow checks on integer arithmetic in that it is a tool for diagnosing logic errors where success is expected.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ConvApprox<f32> for i8

Source§

impl ConvApprox<f32> for i16

Source§

impl ConvApprox<f32> for i32

Source§

impl ConvApprox<f32> for i64

Source§

impl ConvApprox<f32> for i128

Source§

impl ConvApprox<f32> for isize

Source§

impl ConvApprox<f32> for u8

Source§

impl ConvApprox<f32> for u16

Source§

impl ConvApprox<f32> for u32

Source§

impl ConvApprox<f32> for u64

Source§

impl ConvApprox<f32> for u128

Available on crate features std or libm only.
Source§

impl ConvApprox<f32> for usize

Source§

impl ConvApprox<f64> for f32

Source§

impl ConvApprox<f64> for i8

Source§

impl ConvApprox<f64> for i16

Source§

impl ConvApprox<f64> for i32

Source§

impl ConvApprox<f64> for i64

Source§

impl ConvApprox<f64> for i128

Source§

impl ConvApprox<f64> for isize

Source§

impl ConvApprox<f64> for u8

Source§

impl ConvApprox<f64> for u16

Source§

impl ConvApprox<f64> for u32

Source§

impl ConvApprox<f64> for u64

Source§

impl ConvApprox<f64> for u128

Source§

impl ConvApprox<f64> for usize

Implementors§

Source§

impl<S, T: Conv<S>> ConvApprox<S> for T