Skip to main content

Cast

Trait Cast 

Source
pub trait Cast<T> {
    type Error: Into<Error> + Error;

    // Required methods
    fn try_cast(self) -> Result<T, Self::Error>;
    fn cast(self) -> T;
}
Expand description

Like Into, but for Conv

This trait has similarities to Into and TryInto, but limited to numeric conversions:

  • Conversions may be fallible, like TryInto.
  • Conversions must be lossless, like Into; this corresponds to the Exact “rounding” mode. (See also CastApprox.)
  • 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.

This trait is automatically implemented for every implementation of Conv.

Required Associated Types§

Source

type Error: Into<Error> + Error

Conversion error type

This should be one of Infallible, RangeError or Error.

Required Methods§

Source

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

Try converting from Self to T

Source

fn cast(self) -> T

Cast 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: Conv<S>> Cast<T> for S

Source§

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