pub struct Unalign<T>(_);Expand description
A type with no alignment requirement.
A Unalign wraps a T, removing any alignment requirement. Unalign<T>
has the same size and ABI as T, but not necessarily the same alignment.
This is useful if a type with an alignment requirement needs to be read from
a chunk of memory which provides no alignment guarantees.
Since Unalign has no alignment requirement, the inner T may not be
properly aligned in memory, and so Unalign provides no way of getting a
reference to the inner T. Instead, the T may only be obtained by value
(see get and into_inner).
Implementations§
source§impl<T> Unalign<T>
impl<T> Unalign<T>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes self, returning the inner T.
sourcepub fn get_ptr(&self) -> *const T
pub fn get_ptr(&self) -> *const T
Gets an unaligned raw pointer to the inner T.
Safety
The returned raw pointer is not necessarily aligned to
align_of::<T>(). Most functions which operate on raw pointers require
those pointers to be aligned, so calling those functions with the result
of get_ptr will be undefined behavior if alignment is not guaranteed
using some out-of-band mechanism. In general, the only functions which
are safe to call with this pointer are which that are explicitly
documented as being sound to use with an unaligned pointer, such as
read_unaligned.
sourcepub fn get_mut_ptr(&mut self) -> *mut T
pub fn get_mut_ptr(&mut self) -> *mut T
Gets an unaligned mutable raw pointer to the inner T.
Safety
The returned raw pointer is not necessarily aligned to
align_of::<T>(). Most functions which operate on raw pointers require
those pointers to be aligned, so calling those functions with the result
of get_ptr will be undefined behavior if alignment is not guaranteed
using some out-of-band mechanism. In general, the only functions which
are safe to call with this pointer are those which are explicitly
documented as being sound to use with an unaligned pointer, such as
read_unaligned.
Trait Implementations§
source§impl<T> AsBytes for Unalign<T>where
T: AsBytes,
impl<T> AsBytes for Unalign<T>where T: AsBytes,
source§fn write_to_prefix<B>(&self, bytes: B) -> Option<()>where
B: ByteSliceMut,
fn write_to_prefix<B>(&self, bytes: B) -> Option<()>where B: ByteSliceMut,
source§fn write_to_suffix<B>(&self, bytes: B) -> Option<()>where
B: ByteSliceMut,
fn write_to_suffix<B>(&self, bytes: B) -> Option<()>where B: ByteSliceMut,
source§impl<T> FromBytes for Unalign<T>where
T: FromBytes,
impl<T> FromBytes for Unalign<T>where T: FromBytes,
source§fn new_zeroed() -> Selfwhere
Self: Sized,
fn new_zeroed() -> Selfwhere Self: Sized,
Self from zeroed bytes.