Skip to main content

ConvApprox

Trait ConvApprox 

Source
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_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 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§

Source

type Error: Into<RangeError> + Error

Conversion error type

This should be either Infallible or RangeError.

Required Methods§

Source

fn try_conv_approx(s: S) -> Result<Self, Self::Error>

Try converting from S to Self, allowing approximation

Provided Methods§

Source

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

Implementors§

Source§

impl<S, T: ConvTo<S, Approx>> ConvApprox<S> for T

Source§

type Error = <T as ConvTo<S, Approx>>::Error