StrictCastFrom

Trait StrictCastFrom 

Source
pub trait StrictCastFrom<Src> {
    // Required method
    fn strict_cast_from(src: Src) -> Self;
}
Expand description

Used to cast values, panicking if the value does not fit.

This trait enables trait constraints for casting in the opposite direction to StrictCast.

§Examples

use az::StrictCastFrom;
trait Tr {
    type Assoc: StrictCastFrom<u8>;
    fn strict_assoc_from_u8(a: u8) -> Self::Assoc {
        StrictCastFrom::strict_cast_from(a)
    }
}
impl Tr for () {
    type Assoc = i8;
}
assert_eq!(<() as Tr>::strict_assoc_from_u8(5u8), 5i8);

The following assertion would panic because of overflow.

let _overflow = <() as Tr>::strict_assoc_from_u8(255u8);

Required Methods§

Source

fn strict_cast_from(src: Src) -> Self

Casts the value.

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.

Implementors§

Source§

impl<Src: StrictCast<Dst>, Dst> StrictCastFrom<Src> for Dst