Skip to main content

RefCast

Trait RefCast 

Source
pub trait RefCast {
    type From: ?Sized;

    // Required methods
    fn ref_cast(from: &Self::From) -> &Self;
    fn ref_cast_mut(from: &mut Self::From) -> &mut Self;
}
Expand description

Safely cast &T to &U where the struct U contains a single field of type T.

// `&String` can be cast to `&U`.
#[derive(RefCast)]
#[repr(transparent)]
struct U(String);

// `&T` can be cast to `&V<T>`.
#[derive(RefCast)]
#[repr(transparent)]
struct V<T> {
    t: T,
}

See the crate-level documentation for usage examples!

Required Associated Types§

Required Methods§

Source

fn ref_cast(from: &Self::From) -> &Self

Source

fn ref_cast_mut(from: &mut Self::From) -> &mut Self

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.

Implementations on Foreign Types§

Source§

impl RefCast for PublicKey

Source§

impl RefCast for Signature

Source§

impl RefCast for MachineId

Source§

impl RefCast for Measurement

Source§

impl RefCast for MrShort

Source§

type From = [u8; 4]

Source§

fn ref_cast(_from: &<MrShort as RefCast>::From) -> &MrShort

Source§

fn ref_cast_mut(_from: &mut <MrShort as RefCast>::From) -> &mut MrShort

Source§

impl RefCast for Hash

Source§

type From = [u8; 32]

Source§

fn ref_cast(_from: &<Hash as RefCast>::From) -> &Hash

Source§

fn ref_cast_mut(_from: &mut <Hash as RefCast>::From) -> &mut Hash

Implementors§