Struct zerocopy::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 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>

§

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 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 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 + PartialOrd,

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method 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

This method 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

This method 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

This method 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: Copy,

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> CloneToUninit for T
where T: Clone,

source§

default 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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>,

§

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.