Function safe_transmute::base::from_bytes_pedantic
source · pub unsafe fn from_bytes_pedantic<T: Copy>(
bytes: &[u8]
) -> Result<T, Error<'_, u8, T>>
Expand description
Convert a byte slice into a single instance of a Copy
able type.
The byte slice must have exactly the expected number of bytes to fill a single instance of a type, without trailing space.
§Safety
- This function does not perform memory alignment checks. The beginning of
the slice data must be properly aligned for accessing the value of type
T
. - The byte data needs to correspond to a valid
T
value.
Failure to fulfill any of the requirements above may result in undefined behavior.
§Errors
An error is returned if the slice’s length is not equal to the size of a
single value T
.
§Examples
// Little-endian
unsafe {
assert_eq!(from_bytes_pedantic::<u32>(&[0x00, 0x00, 0x00, 0x01])?, 0x0100_0000);
}