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: Self
๐ฌThis is a nightly-only experimental API. (ptr_alignment_type
#102070)Available on non-crate feature ferrocene_certified
only.
pub const MIN: Self
ptr_alignment_type
#102070)ferrocene_certified
only.Sourcepub const fn of<T>() -> Self
๐ฌThis is a nightly-only experimental API. (ptr_alignment_type
#102070)Available on non-crate feature ferrocene_certified
only.
pub const fn of<T>() -> Self
ptr_alignment_type
#102070)ferrocene_certified
only.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 new(align: usize) -> Option<Self>
๐ฌThis is a nightly-only experimental API. (ptr_alignment_type
#102070)
pub const fn new(align: usize) -> Option<Self>
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) -> Self
๐ฌThis is a nightly-only experimental API. (ptr_alignment_type
#102070)
pub const unsafe fn new_unchecked(align: usize) -> Self
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)Available on non-crate feature ferrocene_certified
only.
pub const fn as_nonzero(self) -> NonZero<usize>
ptr_alignment_type
#102070)ferrocene_certified
only.Sourcepub const fn log2(self) -> u32
๐ฌThis is a nightly-only experimental API. (ptr_alignment_type
#102070)Available on non-crate feature ferrocene_certified
only.
pub const fn log2(self) -> u32
ptr_alignment_type
#102070)ferrocene_certified
only.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)Available on non-crate feature ferrocene_certified
only.
pub const fn mask(self) -> usize
ptr_alignment_type
#102070)ferrocene_certified
only.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
Available on non-crate feature ferrocene_certified
only.Returns Alignment::MIN
, which is valid for any type.
impl Default for Alignment
ferrocene_certified
only.Returns Alignment::MIN
, which is valid for any type.
Sourceยงimpl From<Alignment> for NonZero<usize>
Available on non-crate feature ferrocene_certified
only.
impl From<Alignment> for NonZero<usize>
ferrocene_certified
only.Sourceยงimpl Ord for Alignment
Available on non-crate feature ferrocene_certified
only.
impl Ord for Alignment
ferrocene_certified
only.Sourceยงimpl PartialOrd for Alignment
Available on non-crate feature ferrocene_certified
only.
impl PartialOrd for Alignment
ferrocene_certified
only.Sourceยงimpl TryFrom<NonZero<usize>> for Alignment
Available on non-crate feature ferrocene_certified
only.
impl TryFrom<NonZero<usize>> for Alignment
ferrocene_certified
only.impl Copy for Alignment
impl Eq for Alignment
impl StructuralPartialEq for Alignment
Auto Trait Implementationsยง
impl Freeze for Alignment
impl RefUnwindSafe for Alignment
impl Send for Alignment
impl Sync for Alignment
impl Unpin for Alignment
impl UnwindSafe for Alignment
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
ferrocene_certified
only.