pub struct Alignment { /* private fields */ }ptr_alignment_type #102070)Expand description
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§
Source§impl Alignment
impl Alignment
Sourcepub const MIN: Alignment
🔬This is a nightly-only experimental API. (ptr_alignment_type #102070)
pub const MIN: Alignment
ptr_alignment_type #102070)Sourcepub const fn of<T>() -> Alignment
🔬This is a nightly-only experimental API. (ptr_alignment_type #102070)
pub const fn of<T>() -> Alignment
ptr_alignment_type #102070)Returns the alignment for a type.
This provides the same numerical value as align_of,
but in an Alignment instead of a usize.
Sourcepub const fn of_val<T>(val: &T) -> Alignmentwhere
T: ?Sized,
🔬This is a nightly-only experimental API. (ptr_alignment_type #102070)
pub const fn of_val<T>(val: &T) -> Alignmentwhere
T: ?Sized,
ptr_alignment_type #102070)Sourcepub const unsafe fn of_val_raw<T>(val: *const T) -> Alignmentwhere
T: ?Sized,
🔬This is a nightly-only experimental API. (ptr_alignment_type #102070)
pub const unsafe fn of_val_raw<T>(val: *const T) -> Alignmentwhere
T: ?Sized,
ptr_alignment_type #102070)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
TisSized, this function is always safe to call. - If the unsized tail of
Tis:- 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_valon a reference to a type with an extern type tail. - otherwise, it is conservatively not allowed to call this function.
- 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
§Examples
Sourcepub const fn new(align: usize) -> Option<Alignment>
🔬This is a nightly-only experimental API. (ptr_alignment_type #102070)
pub const fn new(align: usize) -> Option<Alignment>
ptr_alignment_type #102070)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.
Sourcepub const unsafe fn new_unchecked(align: usize) -> Alignment
🔬This is a nightly-only experimental API. (ptr_alignment_type #102070)
pub const unsafe fn new_unchecked(align: usize) -> Alignment
ptr_alignment_type #102070)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.
Sourcepub const fn as_usize(self) -> usize
🔬This is a nightly-only experimental API. (ptr_alignment_type #102070)
pub const fn as_usize(self) -> usize
ptr_alignment_type #102070)Returns the alignment as a usize.
Sourcepub const fn as_nonzero(self) -> NonZero<usize>
🔬This is a nightly-only experimental API. (ptr_alignment_type #102070)
pub const fn as_nonzero(self) -> NonZero<usize>
ptr_alignment_type #102070)Sourcepub const fn log2(self) -> u32
🔬This is a nightly-only experimental API. (ptr_alignment_type #102070)
pub const fn log2(self) -> u32
ptr_alignment_type #102070)Returns the base-2 logarithm of the alignment.
This is always exact, as self represents a power of two.
§Examples
Sourcepub const fn mask(self) -> usize
🔬This is a nightly-only experimental API. (ptr_alignment_type #102070)
pub const fn mask(self) -> usize
ptr_alignment_type #102070)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);Trait Implementations§
Source§impl Default for Alignment
Returns Alignment::MIN, which is valid for any type.
impl Default for Alignment
Returns Alignment::MIN, which is valid for any type.