Skip to main content

CastApprox

Trait CastApprox 

Source
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_i8 and -1_i32 are conceptually the same value while 255_u8 is 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§

Source

type Error: Into<RangeError> + Error

Conversion error type

This should be either Infallible or RangeError.

Required Methods§

Source

fn try_cast_approx(self) -> Result<T, Self::Error>

Try approximate conversion from Self to T

Source

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".

Implementors§

Source§

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

Source§

type Error = <T as ConvApprox<S>>::Error