@@ -662,6 +662,11 @@ impl<T: ?Sized> *const T {
662
662
/// will only affect the pointer part, whereas for (thin) pointers to
663
663
/// sized types, this has the same effect as a simple assignment.
664
664
///
665
+ /// The resulting pointer will have provenance of `val`, i.e., for a fat
666
+ /// pointer, this operation is semantically the same as creating a new
667
+ /// fat pointer with the data pointer value of `val` but the metadata of
668
+ /// `self`.
669
+ ///
665
670
/// # Examples
666
671
///
667
672
/// This function is primarily useful for allowing byte-wise pointer
@@ -673,13 +678,17 @@ impl<T: ?Sized> *const T {
673
678
/// let arr: [i32; 3] = [1, 2, 3];
674
679
/// let mut ptr = &arr[0] as *const dyn Debug;
675
680
/// 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);
681
+ /// unsafe {
682
+ /// ptr = ptr.set_ptr_value(thin.add(8));
683
+ /// # assert_eq!(*(ptr as *const i32), 3);
684
+ /// println!("{:?}", &*ptr); // will print "3"
685
+ /// }
678
686
/// ```
679
687
#[ unstable( feature = "set_ptr_value" , issue = "75091" ) ]
688
+ #[ must_use = "returns a new pointer rather than modifying its argument" ]
680
689
#[ inline]
681
- pub fn set_ptr_value ( mut self , val : * const ( ) ) -> Self {
682
- let thin = & mut self as * mut * const T as * mut * const ( ) ;
690
+ pub fn set_ptr_value ( mut self , val : * const u8 ) -> Self {
691
+ let thin = & mut self as * mut * const T as * mut * const u8 ;
683
692
// SAFETY: In case of a thin pointer, this operations is identical
684
693
// to a simple assignment. In case of a fat pointer, with the current
685
694
// fat pointer layout implementation, the first field of such a
0 commit comments