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)const: unstable · sourcepub fn of<T>() -> Alignment
🔬This is a nightly-only experimental API. (ptr_alignment_type
#102070)
pub fn of<T>() -> Alignment
ptr_alignment_type
#102070)Returns the alignment for a type.
This provides the same numerical value as mem::align_of
,
but in an Alignment
instead of a usize
.
const: unstable · sourcepub fn new(align: usize) -> Option<Alignment>
🔬This is a nightly-only experimental API. (ptr_alignment_type
#102070)
pub 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.
const: unstable · sourcepub unsafe fn new_unchecked(align: usize) -> Alignment
🔬This is a nightly-only experimental API. (ptr_alignment_type
#102070)
pub 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.
const: unstable · sourcepub fn as_usize(self) -> usize
🔬This is a nightly-only experimental API. (ptr_alignment_type
#102070)
pub fn as_usize(self) -> usize
ptr_alignment_type
#102070)Returns the alignment as a usize
const: unstable · sourcepub fn as_nonzero(self) -> NonZero<usize>
🔬This is a nightly-only experimental API. (ptr_alignment_type
#102070)
pub fn as_nonzero(self) -> NonZero<usize>
ptr_alignment_type
#102070)Returns the alignment as a NonZeroUsize
const: unstable · sourcepub fn log2(self) -> u32
🔬This is a nightly-only experimental API. (ptr_alignment_type
#102070)
pub fn log2(self) -> u32
ptr_alignment_type
#102070)const: unstable · sourcepub fn mask(self) -> usize
🔬This is a nightly-only experimental API. (ptr_alignment_type
#102070)
pub 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);
RunTrait Implementations§
source§impl Default for Alignment
impl Default for Alignment
Returns Alignment::MIN
, which is valid for any type.
const: unstable · source§impl Ord for Alignment
impl Ord for Alignment
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl PartialEq for Alignment
impl PartialEq for Alignment
const: unstable · source§impl PartialOrd for Alignment
impl PartialOrd for Alignment
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more