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
Selfmust have the same size and alignment asSelf::Repr.Selfmust be transmutable toSelf::Repr.- Neither
SelfnorSelf::Reprcontains interior mutability.
The above basically says that &Self can be transmuted to &Self::Repr.
Required Associated Types§
Provided Methods§
Sourcefn as_repr(this: &Self) -> &Self::Repr
fn as_repr(this: &Self) -> &Self::Repr
Convert from &Self to &Self::Repr.
Sourcefn into_repr(this: Self) -> Self::Repr
fn into_repr(this: Self) -> Self::Repr
Convert from Self to Self::Repr.
Sourceunsafe fn from_repr_unchecked(repr: Self::Repr) -> Self
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".