On this page
Struct std::ptr::Alignment
pub struct Alignment(/* private fields */);
ptr_alignment_type
#102070)
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
impl Alignment
pub const MIN: Alignment = _
ptr_alignment_type
#102070)
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);
pub const 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
.
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.
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.
pub fn as_usize(self) -> usize
ptr_alignment_type
#102070)
Returns the alignment as a usize
pub const fn as_nonzero(self) -> NonZeroUsize
ptr_alignment_type
#102070)
Returns the alignment as a NonZeroUsize
pub 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
#![feature(ptr_alignment_type)]
use std::ptr::Alignment;
assert_eq!(Alignment::of::<u8>().log2(), 0);
assert_eq!(Alignment::new(1024).unwrap().log2(), 10);
Trait Implementations
impl Clone for Alignment
fn clone(&self) -> Alignment
fn clone_from(&mut self, source: &Self)
source
. Read more
impl Debug for Alignment
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
impl From<Alignment> for NonZeroUsize
fn from(align: Alignment) -> NonZeroUsize
impl From<Alignment> for usize
fn from(align: Alignment) -> usize
impl Hash for Alignment
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
fn hash_slice<H>(data: &[Self], state: &mut H)
where
H: Hasher,
Self: Sized,
impl Ord for Alignment
fn cmp(&self, other: &Alignment) -> Ordering
fn max(self, other: Self) -> Self
where
Self: Sized,
fn min(self, other: Self) -> Self
where
Self: Sized,
fn clamp(self, min: Self, max: Self) -> Self
where
Self: Sized + PartialOrd,
impl PartialEq for Alignment
fn eq(&self, other: &Alignment) -> bool
self
and other
values to be equal, and is used by ==
.
fn ne(&self, other: &Rhs) -> bool
!=
. The default implementation is almost always sufficient, and should not be overridden without very good reason.
impl PartialOrd for Alignment
fn partial_cmp(&self, other: &Alignment) -> Option<Ordering>
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
self
and other
) and is used by the >=
operator. Read more
impl TryFrom<NonZeroUsize> for Alignment
type Error = TryFromIntError
fn try_from(
align: NonZeroUsize
) -> Result<Alignment, <Alignment as TryFrom<NonZeroUsize>>::Error>
impl TryFrom<usize> for Alignment
type Error = TryFromIntError
fn try_from(
align: usize
) -> Result<Alignment, <Alignment as TryFrom<usize>>::Error>
impl Copy for Alignment
impl Eq for Alignment
impl StructuralEq for Alignment
impl StructuralPartialEq for Alignment
Auto Trait Implementations
impl RefUnwindSafe for Alignment
impl Send for Alignment
impl Sync for Alignment
impl Unpin for Alignment
impl UnwindSafe for Alignment
Blanket Implementations
impl<T> Any for T
where
T: 'static + ?Sized,
impl<T> Borrow<T> for T
where
T: ?Sized,
impl<T> BorrowMut<T> for T
where
T: ?Sized,
impl<T> From<T> for T
fn from(t: T) -> T
Returns the argument unchanged.
impl<T, U> Into<U> for T
where
U: From<T>,
fn into(self) -> U
Calls U::from(self)
.
That is, this conversion is whatever the implementation of From<T> for U
chooses to do.
impl<T> ToOwned for T
where
T: Clone,
type Owned = T
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)
impl<T, U> TryFrom<U> for T
where
U: Into<T>,
type Error = Infallible
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for T
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/ptr/struct.Alignment.html