Function safe_transmute::trivial::transmute_trivial_pedantic
source · pub unsafe fn transmute_trivial_pedantic<T: TriviallyTransmutable>(
bytes: &[u8]
) -> Result<T, Error<'_, u8, T>>
Expand description
Transmute a byte slice into a single instance of a trivially transmutable type.
The byte slice must have exactly enough bytes to fill a single instance of a type.
§Errors
An error is returned in one of the following situations:
- The data does not have a memory alignment compatible with
T
. You will have to make a copy anyway, or modify how the data was originally made. - The data does not have enough bytes for a single value
T
. - The data has more bytes than those required to produce a single value
T
.
§Safety
This function invokes undefined behavior if the data does not have a memory
alignment compatible with T
. If this cannot be ensured, you will have to
make a copy of the data, or change how it was originally made.
§Examples
// Little-endian
unsafe {
assert_eq!(transmute_trivial_pedantic::<u16>(&[0x0F, 0x0E])?, 0x0E0F);
}