Struct Alignment
struct Alignment { ... }
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
const fn of<T>() -> SelfReturns the alignment for a type.
This provides the same numerical value as
align_of, but in anAlignmentinstead of ausize.const fn of_val<T: MetaSized>(val: &T) -> SelfReturns the ABI-required minimum alignment of the type of the value that
valpoints to.Every reference to a value of the type
Tmust be a multiple of this number.Examples
use Alignment; assert_eq!;unsafe const fn of_val_raw<T: MetaSized>(val: *const T) -> SelfReturns the ABI-required minimum alignment of the type of the value that
valpoints to.Every reference to a value of the type
Tmust be a multiple of this number.Safety
This function is only safe to call if the following conditions hold:
- If
TisSized, this function is always safe to call. - If the unsized tail of
Tis:- a [slice], then the length of the slice tail must be an initialized
integer, and the size of the entire value
(dynamic tail length + statically sized prefix) must fit in
isize. For the special case where the dynamic tail length is 0, this function is safe to call. - a trait object, then the vtable part of the pointer must point
to a valid vtable acquired by an unsizing coercion, and the size
of the entire value (dynamic tail length + statically sized prefix)
must fit in
isize. - an (unstable) extern type, then this function is always safe to
call, but may panic or otherwise return the wrong value, as the
extern type's layout is not known. This is the same behavior as
Alignment::of_valon a reference to a type with an extern type tail. - otherwise, it is conservatively not allowed to call this function.
- a [slice], then the length of the slice tail must be an initialized
integer, and the size of the entire value
(dynamic tail length + statically sized prefix) must fit in
Examples
use Alignment; assert_eq!;- If
const fn new(align: usize) -> Option<Self>Creates an
Alignmentfrom ausize, or returnsNoneif it's not a power of two.Note that
0is not a power of two, nor a valid alignment.unsafe const fn new_unchecked(align: usize) -> SelfCreates an
Alignmentfrom a power-of-twousize.Safety
alignmust be a power of two.Equivalently, it must be
1 << expfor someexpin0..usize::BITS. It must not be zero.const fn as_usize(self: Self) -> usizeReturns the alignment as a
usize.const fn as_nonzero(self: Self) -> NonZero<usize>Returns the alignment as a
[NonZero]<[usize]>.const fn log2(self: Self) -> u32Returns the base-2 logarithm of the alignment.
This is always exact, as
selfrepresents a power of two.Examples
use Alignment; assert_eq!; assert_eq!;const fn mask(self: Self) -> usizeReturns a bit mask that can be used to match this alignment.
This is equivalent to
!(self.as_usize() - 1).Examples
use ; ; ; ; let one = dangling.as_ptr; let two = dangling.as_ptr; let four = dangling.as_ptr; assert_eq!; assert_eq!; assert_eq!; assert_ne!;
impl Clone for Alignment
fn clone(self: &Self) -> Alignment
impl Copy for Alignment
impl Debug for Alignment
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Default for Alignment
fn default() -> Alignment
impl Eq for Alignment
impl Freeze for Alignment
impl Hash for Alignment
fn hash<H: hash::Hasher>(self: &Self, state: &mut H)
impl Ord for Alignment
fn cmp(self: &Self, other: &Self) -> Ordering
impl PartialEq for Alignment
fn eq(self: &Self, other: &Alignment) -> bool
impl PartialOrd for Alignment
fn partial_cmp(self: &Self, other: &Self) -> Option<Ordering>
impl RefUnwindSafe for Alignment
impl Send for Alignment
impl StructuralPartialEq for Alignment
impl Sync for Alignment
impl TryFrom for Alignment
fn try_from(align: usize) -> Result<Alignment, <Self as >::Error>
impl TryFrom for Alignment
fn try_from(align: NonZero<usize>) -> Result<Alignment, <Self as >::Error>
impl Unpin for Alignment
impl UnsafeUnpin for Alignment
impl UnwindSafe for Alignment
impl<T> Any for Alignment
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Alignment
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Alignment
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Alignment
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Alignment
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into for Alignment
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Alignment
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Alignment
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>