pub trait Conv<T>: Sized {
    fn try_conv(v: T) -> Result<Self>;

    fn conv(v: T) -> Self { ... }
}
Expand description

Like From, but supports fallible conversions

This trait is intented to be an extension over From, also supporting fallible conversions of numeric types. Since Rust does not yet have stable support for handling conflicting implementations (specialization or otherwise), only conversions between the most important numeric types are supported for now.

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

Required Methods

Try converting from T to Self

This method must fail on inexact conversions.

Provided Methods

Convert from T to Self

This method must return the same result as Self::try_conv 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 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.

Implementations on Foreign Types

Implementors