Trait az::Az

source ·
pub trait Az {
    // Required method
    fn az<Dst>(self) -> Dst
       where Self: Cast<Dst>;
}
Expand description

Used to cast values.

This is a convenience trait to enable writing src.az::<Dst>(). This would not work with the Cast::cast method because the Cast trait is generic while its cast method is not generic.

This trait’s method is suitable for chaining.

If there is an implementation of Cast<Dst> for &Src but not for Src, and the variable src is of type Src, then src.az::<Dst>() would not work and (&src).az::<Dst>() is not easy to use with chaining, but src.borrow().az::<Dst>() works.

§Panics

When debug assertions are enabled, this trait’s method panics if the value does not fit in the destination. When debug assertions are not enabled (usual in release mode), the wrapped value can be returned, but it is not considered a breaking change if in the future it panics; if wrapping is required use WrappingAs instead.

This trait’s method also panics with no debug assertions if the value does not fit and cannot be wrapped, for example when trying to cast floating-point ∞ into an integer type.

§Examples

use az::Az;
assert_eq!(5i32.az::<u32>(), 5);
assert_eq!(17.1f32.az::<u8>(), 17);

The following example shows how this trait can be used when Cast is implemented for a reference type.

use az::{Az, Cast};
use core::borrow::Borrow;
struct I(i32);
impl Cast<i64> for &'_ I {
    fn cast(self) -> i64 { self.0.cast() }
}

let r = &I(-5);
assert_eq!(r.az::<i64>(), -5);
let owned = I(12);
assert_eq!(owned.borrow().az::<i64>(), 12);

Required Methods§

source

fn az<Dst>(self) -> Dst
where Self: Cast<Dst>,

Casts the value.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T> Az for T