Trait safe_transmute::trivial::TriviallyTransmutable

source ·
pub unsafe trait TriviallyTransmutable: Copy { }
Expand description

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.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl TriviallyTransmutable for f32

source§

impl TriviallyTransmutable for f64

source§

impl TriviallyTransmutable for i8

source§

impl TriviallyTransmutable for i16

source§

impl TriviallyTransmutable for i32

source§

impl TriviallyTransmutable for i64

source§

impl TriviallyTransmutable for isize

source§

impl TriviallyTransmutable for u8

source§

impl TriviallyTransmutable for u16

source§

impl TriviallyTransmutable for u32

source§

impl TriviallyTransmutable for u64

source§

impl TriviallyTransmutable for usize

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 1]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 2]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 3]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 4]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 5]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 6]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 7]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 8]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 9]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 10]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 11]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 12]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 13]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 14]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 15]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 16]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 17]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 18]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 19]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 20]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 21]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 22]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 23]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 24]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 25]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 26]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 27]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 28]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 29]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 30]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 31]

source§

impl<T: TriviallyTransmutable> TriviallyTransmutable for [T; 32]

Implementors§