Skip to main content

AsRepr

Trait AsRepr 

Source
pub unsafe trait AsRepr: Sized {
    type Repr;

    // Provided methods
    fn as_repr(this: &Self) -> &Self::Repr { ... }
    fn into_repr(this: Self) -> Self::Repr { ... }
    unsafe fn from_repr_unchecked(repr: Self::Repr) -> Self { ... }
}
Expand description

Type that is layout-compatible with a primitive representation.

§Safety

The above basically says that &Self can be transmuted to &Self::Repr.

Required Associated Types§

Source

type Repr

Primitive representation of this type.

Provided Methods§

Source

fn as_repr(this: &Self) -> &Self::Repr

Convert from &Self to &Self::Repr.

Source

fn into_repr(this: Self) -> Self::Repr

Convert from Self to Self::Repr.

Source

unsafe fn from_repr_unchecked(repr: Self::Repr) -> Self

Convert from Self::Repr to Self.

§Safety

repr must be a valid bit pattern of Self and satisfy type-specific invariants of it.

Alternatively, if repr is previously obtained using Self::into_repr, and each from_repr_unchecked should correspond to a unique into_repr call, then it is safe to call as well (this means that we’re undoing a into_repr call getting the exact bytes back).

No guarantee is made if the result of a into_repr is passed to multiple from_repr_unchecked (i.e. copies are made), to allow for cases where Repr is a pointer and the user of the API wants ownership transfer. Users that want the ability to call from_repr_unchecked after copying can require Copy bound explicitly.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl AsRepr for bool

Source§

impl AsRepr for i8

Source§

impl AsRepr for i16

Source§

impl AsRepr for i32

Source§

impl AsRepr for i64

Source§

impl AsRepr for isize

Source§

impl AsRepr for u8

Source§

impl AsRepr for u16

Source§

impl AsRepr for u32

Source§

impl AsRepr for u64

Source§

impl AsRepr for usize

Source§

impl<T> AsRepr for *const T

Source§

impl<T> AsRepr for *mut T

Implementors§