#[repr(C, packed)]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
sourceimpl<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 const fn get_ptr(&self) -> *const T
pub const 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 those which 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
sourceimpl<T: AsBytes> AsBytes for Unalign<T>
impl<T: AsBytes> AsBytes for Unalign<T>
sourcefn write_to<B: ByteSliceMut>(&self, bytes: B) -> Option<()>
fn write_to<B: ByteSliceMut>(&self, bytes: B) -> Option<()>
sourcefn write_to_prefix<B: ByteSliceMut>(&self, bytes: B) -> Option<()>
fn write_to_prefix<B: ByteSliceMut>(&self, bytes: B) -> Option<()>
sourcefn write_to_suffix<B: ByteSliceMut>(&self, bytes: B) -> Option<()>
fn write_to_suffix<B: ByteSliceMut>(&self, bytes: B) -> Option<()>
sourceimpl<T> FromBytes for Unalign<T>where
T: FromBytes,
impl<T> FromBytes for Unalign<T>where
T: FromBytes,
sourcefn read_from_prefix<B: ByteSlice>(bytes: B) -> Option<Self>where
Self: Sized,
fn read_from_prefix<B: ByteSlice>(bytes: B) -> Option<Self>where
Self: Sized,
sourcefn read_from_suffix<B: ByteSlice>(bytes: B) -> Option<Self>where
Self: Sized,
fn read_from_suffix<B: ByteSlice>(bytes: B) -> Option<Self>where
Self: Sized,
sourcefn new_zeroed() -> Selfwhere
Self: Sized,
fn new_zeroed() -> Selfwhere
Self: Sized,
Self
from zeroed bytes.sourcefn new_box_zeroed() -> Box<Self>where
Self: Sized,
fn new_box_zeroed() -> Box<Self>where
Self: Sized,
Box<Self>
from zeroed bytes. Read more