Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl more traits for ptr::Alignment, add mask method #115249

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion library/core/src/ptr/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl Alignment {
/// This provides the same numerical value as [`mem::align_of`],
/// but in an `Alignment` instead of a `usize`.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[rustc_const_unstable(feature = "ptr_alignment_type", issue = "102070")]
#[inline]
pub const fn of<T>() -> Self {
// SAFETY: rustc ensures that type alignment is always a power of two.
Expand All @@ -53,6 +54,7 @@ impl Alignment {
///
/// Note that `0` is not a power of two, nor a valid alignment.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[rustc_const_unstable(feature = "ptr_alignment_type", issue = "102070")]
#[inline]
pub const fn new(align: usize) -> Option<Self> {
if align.is_power_of_two() {
Expand Down Expand Up @@ -98,6 +100,7 @@ impl Alignment {

/// Returns the alignment as a [`NonZeroUsize`]
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[rustc_const_unstable(feature = "ptr_alignment_type", issue = "102070")]
#[inline]
pub const fn as_nonzero(self) -> NonZeroUsize {
// SAFETY: All the discriminants are non-zero.
Expand All @@ -118,10 +121,42 @@ impl Alignment {
/// assert_eq!(Alignment::new(1024).unwrap().log2(), 10);
/// ```
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[rustc_const_unstable(feature = "ptr_alignment_type", issue = "102070")]
#[inline]
pub fn log2(self) -> u32 {
pub const fn log2(self) -> u32 {
self.as_nonzero().trailing_zeros()
}

/// 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);
/// ```
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[rustc_const_unstable(feature = "ptr_alignment_type", issue = "102070")]
#[inline]
pub const fn mask(self) -> usize {
// SAFETY: The alignment is always nonzero, and therefore decrementing won't overflow.
!(unsafe { self.as_usize().unchecked_sub(1) })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, unchecked_sub(1) is useless on unsigned types llvm/llvm-project#53377

I guess it's fine to leave it, though, since it's clearly correct. And trying to do it in isize would be UB.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's unfortunate. I even was considering at some point adding a decrement() method to NonZeroU* that could use unchecked_sub since it would never overflow, but I guess that that also wouldn't work.

}
}

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
Expand Down Expand Up @@ -193,6 +228,14 @@ impl hash::Hash for Alignment {
}
}

/// Returns [`Alignment::MIN`], which is valid for any type.
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
impl Default for Alignment {
fn default() -> Alignment {
Alignment::MIN
}
}

#[cfg(target_pointer_width = "16")]
clarfonthey marked this conversation as resolved.
Show resolved Hide resolved
type AlignmentEnum = AlignmentEnum16;
#[cfg(target_pointer_width = "32")]
Expand Down
Loading