ref_cast

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.

Implementors§