File tree Expand file tree Collapse file tree 3 files changed +41
-0
lines changed
Expand file tree Collapse file tree 3 files changed +41
-0
lines changed Original file line number Diff line number Diff 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>()`
Original file line number Diff line number Diff 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 ///
Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments