Function safe_transmute::base::from_bytes

source ·
pub unsafe fn from_bytes<T: Copy>(bytes: &[u8]) -> Result<T, Error<'_, u8, T>>
Expand description

Convert a byte slice into a single instance of a Copyable type.

The byte slice must have at least enough bytes to fill a single instance of a type, extraneous data is ignored.

§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 does not have enough bytes for a single value T.

§Examples

// Little-endian
unsafe {
    assert_eq!(from_bytes::<u32>(&[0x00, 0x00, 0x00, 0x01])?, 0x0100_0000);
}