Function read_unaligned
unsafe const fn read_unaligned<T>(src: *const T) -> T
Reads the value from src without moving it. This leaves the
memory in src unchanged.
Unlike read, read_unaligned works with unaligned pointers.
Safety
Behavior is undefined if any of the following conditions are violated:
-
srcmust be valid for reads. -
srcmust point to a properly initialized value of typeT.
Like read, read_unaligned creates a bitwise copy of T, regardless of
whether T is Copy. If T is not Copy, using both the returned
value and the value at *src can violate memory safety.
On packed structs
Attempting to create a raw pointer to an unaligned struct field with
an expression such as &packed.unaligned as *const FieldType creates an
intermediate unaligned reference before converting that to a raw pointer.
That this reference is temporary and immediately cast is inconsequential
as the compiler always expects references to be properly aligned.
As a result, using &packed.unaligned as *const FieldType causes immediate
undefined behavior in your program.
Instead you must use the &raw const syntax to create the pointer.
You may use that constructed pointer together with this function.
An example of what not to do and how this relates to read_unaligned is:
let packed = Packed ;
// Take the address of a 32-bit integer which is not aligned.
// In contrast to `&packed.unaligned as *const _`, this has no undefined behavior.
let unaligned = &raw const packed.unaligned;
let v = unsafe ;
assert_eq!;
Accessing unaligned fields directly with e.g. packed.unaligned is safe however.
Examples
Read a usize value from a byte buffer: