@@ -656,6 +656,38 @@ impl<T: ?Sized> *const T {
656
656
self . wrapping_offset ( ( count as isize ) . wrapping_neg ( ) )
657
657
}
658
658
659
+ /// Sets the pointer value to `ptr`.
660
+ ///
661
+ /// In case `self` is a (fat) pointer to an unsized type, this operation
662
+ /// will only affect the pointer part, whereas for (thin) pointers to
663
+ /// sized types, this has the same effect as a simple assignment.
664
+ ///
665
+ /// # Examples
666
+ ///
667
+ /// This function is primarily useful for allowing byte-wise pointer
668
+ /// arithmetic on potentially fat pointers:
669
+ ///
670
+ /// ```
671
+ /// #![feature(set_ptr_value)]
672
+ /// # use core::fmt::Debug;
673
+ /// let arr: [i32; 3] = [1, 2, 3];
674
+ /// let mut ptr = &arr[0] as *const dyn Debug;
675
+ /// let thin = ptr as *const u8;
676
+ /// ptr = ptr.set_ptr_value(unsafe { thin.add(8).cast() });
677
+ /// assert_eq!(unsafe { *(ptr as *const i32) }, 3);
678
+ /// ```
679
+ #[ unstable( feature = "set_ptr_value" , issue = "75091" ) ]
680
+ #[ inline]
681
+ pub fn set_ptr_value ( mut self , val : * const ( ) ) -> Self {
682
+ let thin = & mut self as * mut * const T as * mut * const ( ) ;
683
+ // SAFETY: In case of a thin pointer, this operations is identical
684
+ // to a simple assignment. In case of a fat pointer, with the current
685
+ // fat pointer layout implementation, the first field of such a
686
+ // pointer is always the data pointer, which is likewise assigned.
687
+ unsafe { * thin = val } ;
688
+ self
689
+ }
690
+
659
691
/// Reads the value from `self` without moving it. This leaves the
660
692
/// memory in `self` unchanged.
661
693
///
0 commit comments