#[repr(transparent)]pub struct Alignment(AlignmentEnum);
ptr_alignment_type
)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.
Tuple Fields§
§0: AlignmentEnum
ptr_alignment_type
)Implementations§
source§impl Alignment
impl Alignment
sourcepub const MIN: Alignment = _
🔬This is a nightly-only experimental API. (ptr_alignment_type
)
pub const MIN: Alignment = _
ptr_alignment_type
)The smallest possible alignment, 1.
All addresses are always aligned at least this much.
§Examples
#![feature(ptr_alignment_type)]
use std::ptr::Alignment;
assert_eq!(Alignment::MIN.as_usize(), 1);
sourcepub const fn of<T>() -> Alignment
🔬This is a nightly-only experimental API. (ptr_alignment_type
)
pub const fn of<T>() -> Alignment
ptr_alignment_type
)Returns the alignment for a type.
This provides the same numerical value as mem::align_of
,
but in an Alignment
instead of a usize
.
sourcepub const fn new(align: usize) -> Option<Alignment>
🔬This is a nightly-only experimental API. (ptr_alignment_type
)
pub const fn new(align: usize) -> Option<Alignment>
ptr_alignment_type
)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
)
pub const unsafe fn new_unchecked(align: usize) -> Alignment
ptr_alignment_type
)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
)
pub const fn as_usize(self) -> usize
ptr_alignment_type
)Returns the alignment as a usize
.
sourcepub const fn as_nonzero(self) -> NonZero<usize>
🔬This is a nightly-only experimental API. (ptr_alignment_type
)
pub const fn as_nonzero(self) -> NonZero<usize>
ptr_alignment_type
)sourcepub const fn log2(self) -> u32
🔬This is a nightly-only experimental API. (ptr_alignment_type
)
pub const fn log2(self) -> u32
ptr_alignment_type
)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);
sourcepub const fn mask(self) -> usize
🔬This is a nightly-only experimental API. (ptr_alignment_type
)
pub const fn mask(self) -> usize
ptr_alignment_type
)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
impl Default for Alignment
Returns Alignment::MIN
, which is valid for any type.
source§impl Ord for Alignment
impl Ord for Alignment
source§impl PartialEq for Alignment
impl PartialEq for Alignment
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 moreimpl 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
source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> Filterable for T
impl<T> Filterable for T
source§fn filterable(
self,
filter_name: &'static str,
) -> RequestFilterDataProvider<T, fn(_: DataRequest<'_>) -> bool>
fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(_: DataRequest<'_>) -> bool>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> MaybeBoxed<Box<T>> for T
impl<T> MaybeBoxed<Box<T>> for T
source§fn maybe_boxed(self) -> Box<T>
fn maybe_boxed(self) -> Box<T>
source§impl<T> MaybeBoxed<T> for T
impl<T> MaybeBoxed<T> for T
source§fn maybe_boxed(self) -> T
fn maybe_boxed(self) -> T
source§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian()
.