Alignment in std::ptr - Rust (original) (raw)

pub struct Alignment(/* private fields */);

🔬This is a nightly-only experimental API. (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.

Source§

Source

🔬This is a nightly-only experimental API. (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);

Source

🔬This is a nightly-only experimental API. (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.

Source

🔬This is a nightly-only experimental API. (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.

Source

🔬This is a nightly-only experimental API. (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.

Source

🔬This is a nightly-only experimental API. (ptr_alignment_type #102070)

Returns the alignment as a usize.

Source

🔬This is a nightly-only experimental API. (ptr_alignment_type #102070)

Source

🔬This is a nightly-only experimental API. (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);

Source

🔬This is a nightly-only experimental API. (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);

Source§

Source§

Source§

Source§

Source§

Converts to this type from the input type.

Source§

Source§

Converts to this type from the input type.

Source§

Source§

Source§

Source§

Tests for self and other values to be equal, and is used by ==.

1.0.0 · Source§

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Source§

Source§

This method returns an ordering between self and other values if one exists. Read more

1.0.0 · Source§

Tests less than (for self and other) and is used by the < operator. Read more

1.0.0 · Source§

Tests less than or equal to (for self and other) and is used by the<= operator. Read more

1.0.0 · Source§

Tests greater than (for self and other) and is used by the >operator. Read more

1.0.0 · Source§

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Source§

Source§

The type returned in the event of a conversion error.

Source§

Performs the conversion.

Source§

Source§

The type returned in the event of a conversion error.

Source§

Performs the conversion.

Source§

Source§

Source§

§

§

§

§

§

§