Trait safe_transmute::trivial::TriviallyTransmutable [−][src]
Type that can be constructed from any combination of bytes.
A type T
implementing this trait means that any arbitrary slice of bytes
of length size_of::<T>()
can be safely interpreted as a value of that
type with support for unaligned memory access. In most (but not all)
cases this is a POD class or a
trivially copyable class.
This serves as a marker trait for all functions in this module.
Enable the const_generics
feature to implement this for arbitrary [T: TriviallyTransmutable, N]
arrays,
instead of just 1-32.
This, of course, requires a sufficiently fresh rustc (at least 1.51).
Warning: if you transmute into a floating-point type you will have a chance to create a signaling NaN,
which, while not illegal, can be unwieldy. Check out util::designalise_f{32,64}()
for a remedy.
Nota bene: bool
is not TriviallyTransmutable
because they’re restricted to
being 0
or 1
, which means that an additional value check is required.
Safety
It is only safe to implement TriviallyTransmutable
for a type T
if it
is safe to read or write a value T
at the pointer of an arbitrary slice
&[u8]
, of length size_of<T>()
, as long as the same slice is
well aligned in memory for reading and writing a T
.
Consult the Transmutes section of the Nomicon for more details.