pub struct Alignment(/* private fields */);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.
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 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
Returns Alignment::MIN, which is valid for any type.
impl Default for Alignment
Returns Alignment::MIN, which is valid for any type.
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 PartialOrd for Alignment
impl PartialOrd for Alignment
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<S> AssignWithType for S
impl<S> AssignWithType for S
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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Sourceยงimpl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Sourceยงimpl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
impl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
Sourceยงtype Val = <C as Collection>::Val
type Val = <C as Collection>::Val
Entry in complex collections.
For example, in a HashMap, while Entry might be a ( key, value ) tuple, Val might only be the value part.Sourceยงfn entry_to_val(self) -> <E as EntryToVal<C>>::Val
fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
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<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> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Sourceยงimpl<'a, T> ToStringWith<WithDebug> for T
impl<'a, T> ToStringWith<WithDebug> for T
Sourceยงfn to_string_with<'s>(&'s self) -> Cow<'s, str>
fn to_string_with<'s>(&'s self) -> Cow<'s, str>
Converts the type to a string using Debug formatting.
Sourceยงimpl<'a, T> ToStringWith<WithDebugMultiline> for T
impl<'a, T> ToStringWith<WithDebugMultiline> for T
Sourceยงfn to_string_with<'s>(&'s self) -> Cow<'s, str>
fn to_string_with<'s>(&'s self) -> Cow<'s, str>
Converts the type to a string using Debug formatting.
Sourceยงimpl<Initial, Error, Final> TransitiveTryFrom<Error, Initial> for Final
impl<Initial, Error, Final> TransitiveTryFrom<Error, Initial> for Final
Sourceยงimpl<Error, Final, Initial> TransitiveTryInto<Error, Final> for Initial
impl<Error, Final, Initial> TransitiveTryInto<Error, Final> for Initial
Sourceยงimpl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
Sourceยงfn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
Invokes the val_to_entry function of the CollectionValToEntry trait to convert the value to an entry.