Struct Alignment

struct Alignment { ... }

A type storing a usize which is a power of two, and thus represents a possible alignment in the Rust abstract machine.

Note that particularly large alignments, while representable in this type, are likely not to be supported by actual allocators and linkers.

Implementations

impl Alignment

const fn of<T>() -> Self

Returns the alignment for a type.

This provides the same numerical value as align_of, but in an Alignment instead of a usize.

const fn of_val<T: MetaSized>(val: &T) -> Self

Returns the ABI-required minimum alignment of the type of the value that val points to.

Every reference to a value of the type T must be a multiple of this number.

Examples

#![feature(ptr_alignment_type)]
use std::ptr::Alignment;

assert_eq!(Alignment::of_val(&5i32).as_usize(), 4);
unsafe const fn of_val_raw<T: MetaSized>(val: *const T) -> Self

Returns the ABI-required minimum alignment of the type of the value that val points to.

Every reference to a value of the type T must be a multiple of this number.

Safety

This function is only safe to call if the following conditions hold:

  • If T is Sized, this function is always safe to call.
  • If the unsized tail of T is:
    • a [slice], then the length of the slice tail must be an initialized integer, and the size of the entire value (dynamic tail length + statically sized prefix) must fit in isize. For the special case where the dynamic tail length is 0, this function is safe to call.
    • a trait object, then the vtable part of the pointer must point to a valid vtable acquired by an unsizing coercion, and the size of the entire value (dynamic tail length + statically sized prefix) must fit in isize.
    • an (unstable) extern type, then this function is always safe to call, but may panic or otherwise return the wrong value, as the extern type's layout is not known. This is the same behavior as Alignment::of_val on a reference to a type with an extern type tail.
    • otherwise, it is conservatively not allowed to call this function.

Examples

#![feature(ptr_alignment_type)]
use std::ptr::Alignment;

assert_eq!(unsafe { Alignment::of_val_raw(&5i32) }.as_usize(), 4);
const fn new(align: usize) -> Option<Self>

Creates an Alignment from a usize, or returns None if it's not a power of two.

Note that 0 is not a power of two, nor a valid alignment.

unsafe const fn new_unchecked(align: usize) -> Self

Creates an Alignment from a power-of-two usize.

Safety

align must be a power of two.

Equivalently, it must be 1 << exp for some exp in 0..usize::BITS. It must not be zero.

const fn as_usize(self: Self) -> usize

Returns the alignment as a usize.

const fn as_nonzero(self: Self) -> NonZero<usize>

Returns the alignment as a [NonZero]<[usize]>.

const fn log2(self: Self) -> u32

Returns the base-2 logarithm of the alignment.

This is always exact, as self represents a power of two.

Examples

#![feature(ptr_alignment_type)]
use std::ptr::Alignment;

assert_eq!(Alignment::of::<u8>().log2(), 0);
assert_eq!(Alignment::new(1024).unwrap().log2(), 10);
const fn mask(self: Self) -> usize

Returns a bit mask that can be used to match this alignment.

This is equivalent to !(self.as_usize() - 1).

Examples

#![feature(ptr_alignment_type)]
#![feature(ptr_mask)]
use std::ptr::{Alignment, NonNull};

#[repr(align(1))] struct Align1(u8);
#[repr(align(2))] struct Align2(u16);
#[repr(align(4))] struct Align4(u32);
let one = <NonNull<Align1>>::dangling().as_ptr();
let two = <NonNull<Align2>>::dangling().as_ptr();
let four = <NonNull<Align4>>::dangling().as_ptr();

assert_eq!(four.mask(Alignment::of::<Align1>().mask()), four);
assert_eq!(four.mask(Alignment::of::<Align2>().mask()), four);
assert_eq!(four.mask(Alignment::of::<Align4>().mask()), four);
assert_ne!(one.mask(Alignment::of::<Align4>().mask()), one);

impl Clone for Alignment

fn clone(self: &Self) -> Alignment

impl Copy for Alignment

impl Debug for Alignment

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

impl Default for Alignment

fn default() -> Alignment

impl Eq for Alignment

impl Freeze for Alignment

impl Hash for Alignment

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

impl Ord for Alignment

fn cmp(self: &Self, other: &Self) -> Ordering

impl PartialEq for Alignment

fn eq(self: &Self, other: &Alignment) -> bool

impl PartialOrd for Alignment

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

impl RefUnwindSafe for Alignment

impl Send for Alignment

impl StructuralPartialEq for Alignment

impl Sync for Alignment

impl TryFrom for Alignment

fn try_from(align: usize) -> Result<Alignment, <Self as >::Error>

impl TryFrom for Alignment

fn try_from(align: NonZero<usize>) -> Result<Alignment, <Self as >::Error>

impl Unpin for Alignment

impl UnsafeUnpin for Alignment

impl UnwindSafe for Alignment

impl<T> Any for Alignment

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Alignment

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Alignment

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

impl<T> CloneToUninit for Alignment

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for Alignment

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for Alignment

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for Alignment

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

impl<T, U> TryInto for Alignment

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