Function safe_transmute::base::transmute_many
source · pub unsafe fn transmute_many<T, G: Guard>(
bytes: &[u8]
) -> Result<&[T], Error<'_, u8, T>>
Expand description
View a byte slice as a slice of an arbitrary type.
The required byte length of the slice depends on the chosen boundary guard. Please see the Guard API.
§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. TypesT
with aDrop
implementation are unlikely to be safe in this regard.
Failure to fulfill any of the requirements above may result in undefined behavior.
§Errors
An error is returned if the data does not comply with the policies of the
given guard G
.
§Examples
// Little-endian
unsafe {
assert_eq!(
transmute_many::<u16, SingleManyGuard>(&[0x00, 0x01, 0x00, 0x02])?,
&[0x0100, 0x0200]
);
}