Function safe_transmute::base::transmute_many_permissive

source ·
pub unsafe fn transmute_many_permissive<T>(bytes: &[u8]) -> &[T]
Expand description

View a byte slice as a slice of an arbitrary type.

The resulting slice will have as many instances of a type as will fit, rounded down. The permissive guard is a no-op, which makes it possible for this function to return a slice directly. It is therefore equivalent to transmute_many::<_, PermissiveGuard>(bytes).unwrap().

§Safety

  • This function does not perform memory alignment checks. The beginning of the slice data must be properly aligned for accessing vlues of type T.
  • The byte data needs to correspond to a valid contiguous sequence of T values. Types T with a Drop implementation are unlikely to be safe in this regard.

Failure to fulfill any of the requirements above may result in undefined behavior.

§Examples

// Little-endian
unsafe {
    assert_eq!(
        transmute_many_permissive::<u16>(&[0x00, 0x01, 0x00, 0x02]),
        &[0x0100, 0x0200]
    );
}