Skip to content

Commit b8ece8b

Browse files
committed
Add pointer masking convenience functions
This commit adds the following functions all of which have a signature `pointer, usize -> pointer`: - `<*mut T>::mask` - `<*const T>::mask` - `intrinsics::ptr_mask` These functions are equivalent to `.map_addr(|a| a & mask)` but they utilize `llvm.ptrmask` llvm intrinsic. *masks your pointers*
1 parent bb865e7 commit b8ece8b

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

core/src/intrinsics.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,17 @@ extern "rust-intrinsic" {
13081308
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
13091309
pub fn arith_offset<T>(dst: *const T, offset: isize) -> *const T;
13101310

1311+
/// Masks out bits of the pointer according to a mask.
1312+
///
1313+
/// Note that, unlike most intrinsics, this is safe to call;
1314+
/// it does not require an `unsafe` block.
1315+
/// Therefore, implementations must not require the user to uphold
1316+
/// any safety invariants.
1317+
///
1318+
/// Consider using [`pointer::mask`] instead.
1319+
#[cfg(not(bootstrap))]
1320+
pub fn ptr_mask<T>(ptr: *const T, mask: usize) -> *const T;
1321+
13111322
/// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
13121323
/// a size of `count` * `size_of::<T>()` and an alignment of
13131324
/// `min_align_of::<T>()`

core/src/ptr/const_ptr.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,21 @@ impl<T: ?Sized> *const T {
559559
from_raw_parts::<T>(self.cast::<u8>().wrapping_offset(count).cast::<()>(), metadata(self))
560560
}
561561

562+
/// Masks out bits of the pointer according to a mask.
563+
///
564+
/// This is convenience for `ptr.map_addr(|a| a & mask)`.
565+
///
566+
/// For non-`Sized` pointees this operation changes only the data pointer,
567+
/// leaving the metadata untouched.
568+
#[cfg(not(bootstrap))]
569+
#[unstable(feature = "ptr_mask", issue = "none")]
570+
#[must_use = "returns a new pointer rather than modifying its argument"]
571+
#[inline(always)]
572+
pub fn mask(self, mask: usize) -> *const T {
573+
let this = intrinsics::ptr_mask(self.cast::<()>(), mask);
574+
from_raw_parts::<T>(this, metadata(self))
575+
}
576+
562577
/// Calculates the distance between two pointers. The returned value is in
563578
/// units of T: the distance in bytes divided by `mem::size_of::<T>()`.
564579
///

core/src/ptr/mut_ptr.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,21 @@ impl<T: ?Sized> *mut T {
575575
)
576576
}
577577

578+
/// Masks out bits of the pointer according to a mask.
579+
///
580+
/// This is convenience for `ptr.map_addr(|a| a & mask)`.
581+
///
582+
/// For non-`Sized` pointees this operation changes only the data pointer,
583+
/// leaving the metadata untouched.
584+
#[cfg(not(bootstrap))]
585+
#[unstable(feature = "ptr_mask", issue = "none")]
586+
#[must_use = "returns a new pointer rather than modifying its argument"]
587+
#[inline(always)]
588+
pub fn mask(self, mask: usize) -> *mut T {
589+
let this = intrinsics::ptr_mask(self.cast::<()>(), mask) as *mut ();
590+
from_raw_parts_mut::<T>(this, metadata(self))
591+
}
592+
578593
/// Returns `None` if the pointer is null, or else returns a unique reference to
579594
/// the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_mut`]
580595
/// must be used instead.

0 commit comments

Comments
 (0)