zerocopy

Struct Unalign

Source
#[repr(C, packed(1))]
pub struct Unalign<T>(/* private fields */);
Expand description

A type with no alignment requirement.

An Unalign wraps a T, removing any alignment requirement. Unalign<T> has the same size and bit validity as T, but not necessarily the same alignment or ABI. 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. There are five ways to access the inner T:

Implementations§

Source§

impl<T> Unalign<T>

Source

pub const fn new(val: T) -> Unalign<T>

Constructs a new Unalign.

Source

pub const fn into_inner(self) -> T

Consumes self, returning the inner T.

Source

pub fn try_deref(&self) -> Option<&T>

Attempts to return a reference to the wrapped T, failing if self is not properly aligned.

If self does not satisfy mem::align_of::<T>(), then it is unsound to return a reference to the wrapped T, and try_deref returns None.

If T: Unaligned, then Unalign<T> implements Deref, and callers may prefer Deref::deref, which is infallible.

Source

pub fn try_deref_mut(&mut self) -> Option<&mut T>

Attempts to return a mutable reference to the wrapped T, failing if self is not properly aligned.

If self does not satisfy mem::align_of::<T>(), then it is unsound to return a reference to the wrapped T, and try_deref_mut returns None.

If T: Unaligned, then Unalign<T> implements DerefMut, and callers may prefer DerefMut::deref_mut, which is infallible.

Source

pub const unsafe fn deref_unchecked(&self) -> &T

Returns a reference to the wrapped T without checking alignment.

If T: Unaligned, then Unalign<T> implements Deref, and callers may prefer Deref::deref, which is safe.

§Safety

If self does not satisfy mem::align_of::<T>(), then self.deref_unchecked() may cause undefined behavior.

Source

pub unsafe fn deref_mut_unchecked(&mut self) -> &mut T

Returns a mutable reference to the wrapped T without checking alignment.

If T: Unaligned, then Unalign<T> implements DerefMut, and callers may prefer DerefMut::deref_mut, which is safe.

§Safety

If self does not satisfy mem::align_of::<T>(), then self.deref_mut_unchecked() may cause undefined behavior.

Source

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.

Source

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.

Source

pub fn set(&mut self, t: T)

Sets the inner T, dropping the previous value.

Source

pub fn update<O, F: FnOnce(&mut T) -> O>(&mut self, f: F) -> O

Updates the inner T by calling a function on it.

If T: Unaligned, then Unalign<T> implements DerefMut, and that impl should be preferred over this method when performing updates, as it will usually be faster and more ergonomic.

For large types, this method may be expensive, as it requires copying 2 * size_of::<T>() bytes. [1]

[1] Since the inner T may not be aligned, it would not be sound to invoke f on it directly. Instead, update moves it into a properly-aligned location in the local stack frame, calls f on it, and then moves it back to its original location in self.

Source§

impl<T: Copy> Unalign<T>

Source

pub fn get(&self) -> T

Gets a copy of the inner T.

Trait Implementations§

Source§

impl<T> AsBytes for Unalign<T>
where T: AsBytes,

Source§

fn as_bytes(&self) -> &[u8]

Gets the bytes of this value. Read more
Source§

fn as_bytes_mut(&mut self) -> &mut [u8]
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
Source§

fn write_to(&self, bytes: &mut [u8]) -> Option<()>

Writes a copy of self to bytes. Read more
Source§

fn write_to_prefix(&self, bytes: &mut [u8]) -> Option<()>

Writes a copy of self to the prefix of bytes. Read more
Source§

fn write_to_suffix(&self, bytes: &mut [u8]) -> Option<()>

Writes a copy of self to the suffix of bytes. Read more
Source§

impl<T: Copy> Clone for Unalign<T>

Source§

fn clone(&self) -> Unalign<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Unaligned + Debug> Debug for Unalign<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for Unalign<T>

Source§

fn default() -> Unalign<T>

Returns the “default value” for a type. Read more
Source§

impl<T: Unaligned> Deref for Unalign<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T: Unaligned> DerefMut for Unalign<T>

Source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
Source§

impl<T: Unaligned + Display> Display for Unalign<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> FromBytes for Unalign<T>
where T: FromBytes,

Source§

fn ref_from(bytes: &[u8]) -> Option<&Self>
where Self: Sized,

Interprets the given bytes as a &Self without copying. Read more
Source§

fn ref_from_prefix(bytes: &[u8]) -> Option<&Self>
where Self: Sized,

Interprets the prefix of the given bytes as a &Self without copying. Read more
Source§

fn ref_from_suffix(bytes: &[u8]) -> Option<&Self>
where Self: Sized,

Interprets the suffix of the given bytes as a &Self without copying. Read more
Source§

fn mut_from(bytes: &mut [u8]) -> Option<&mut Self>
where Self: Sized + AsBytes,

Interprets the given bytes as a &mut Self without copying. Read more
Source§

fn mut_from_prefix(bytes: &mut [u8]) -> Option<&mut Self>
where Self: Sized + AsBytes,

Interprets the prefix of the given bytes as a &mut Self without copying. Read more
Source§

fn mut_from_suffix(bytes: &mut [u8]) -> Option<&mut Self>
where Self: Sized + AsBytes,

Interprets the suffix of the given bytes as a &mut Self without copying. Read more
Source§

fn slice_from(bytes: &[u8]) -> Option<&[Self]>
where Self: Sized,

Interprets the given bytes as a &[Self] without copying. Read more
Source§

fn slice_from_prefix(bytes: &[u8], count: usize) -> Option<(&[Self], &[u8])>
where Self: Sized,

Interprets the prefix of the given bytes as a &[Self] with length equal to count without copying. Read more
Source§

fn slice_from_suffix(bytes: &[u8], count: usize) -> Option<(&[u8], &[Self])>
where Self: Sized,

Interprets the suffix of the given bytes as a &[Self] with length equal to count without copying. Read more
Source§

fn mut_slice_from(bytes: &mut [u8]) -> Option<&mut [Self]>
where Self: Sized + AsBytes,

Interprets the given bytes as a &mut [Self] without copying. Read more
Source§

fn mut_slice_from_prefix( bytes: &mut [u8], count: usize, ) -> Option<(&mut [Self], &mut [u8])>
where Self: Sized + AsBytes,

Interprets the prefix of the given bytes as a &mut [Self] with length equal to count without copying. Read more
Source§

fn mut_slice_from_suffix( bytes: &mut [u8], count: usize, ) -> Option<(&mut [u8], &mut [Self])>
where Self: Sized + AsBytes,

Interprets the suffix of the given bytes as a &mut [Self] with length equal to count without copying. Read more
Source§

fn read_from(bytes: &[u8]) -> Option<Self>
where Self: Sized,

Reads a copy of Self from bytes. Read more
Source§

fn read_from_prefix(bytes: &[u8]) -> Option<Self>
where Self: Sized,

Reads a copy of Self from the prefix of bytes. Read more
Source§

fn read_from_suffix(bytes: &[u8]) -> Option<Self>
where Self: Sized,

Reads a copy of Self from the suffix of bytes. Read more
Source§

impl<T> FromZeroes for Unalign<T>
where T: FromZeroes,

Source§

fn zero(&mut self)

Overwrites self with zeroes. Read more
Source§

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
Source§

impl<T: Unaligned + Hash> Hash for Unalign<T>

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Unaligned + Ord> Ord for Unalign<T>

Source§

fn cmp(&self, other: &Unalign<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: Unaligned + PartialEq> PartialEq for Unalign<T>

Source§

fn eq(&self, other: &Unalign<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Unaligned + PartialOrd> PartialOrd for Unalign<T>

Source§

fn partial_cmp(&self, other: &Unalign<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Copy> Copy for Unalign<T>

Source§

impl<T: Unaligned + Eq> Eq for Unalign<T>

Source§

impl<T> Unaligned for Unalign<T>

Auto Trait Implementations§

§

impl<T> Freeze for Unalign<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Unalign<T>
where T: RefUnwindSafe,

§

impl<T> Send for Unalign<T>
where T: Send,

§

impl<T> Sync for Unalign<T>
where T: Sync,

§

impl<T> Unpin for Unalign<T>
where T: Unpin,

§

impl<T> UnwindSafe for Unalign<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.