Transmuting bytes to an arbitrary type can cause undefined behavior.
The safety requirements:
T must be Sized.
- The length of bytes must be equal to
size_of::<T>().
- The alignment of bytes must be equal to
align_of::<T>().
T must be valid for any binary representation.
|
let val_array: Box<[u8]> = val.into_boxed_slice(); |
|
let val_raw: *mut T = Box::into_raw(val_array) as *mut T; |
|
let val_box: Box<T> = unsafe { Box::from_raw(val_raw) }; |
Transmuting bytes to an arbitrary type can cause undefined behavior.
The safety requirements:
Tmust beSized.size_of::<T>().align_of::<T>().Tmust be valid for any binary representation.sysctl-rs/src/unix/funcs.rs
Lines 367 to 369 in 57c05e1