Skip to content

Commit 06eb274

Browse files
authored
Rollup merge of rust-lang#75407 - oliver-giersch:set_ptr_value, r=RalfJung
Requested changes to [*mut T|*const T]::set_ptr_value This is a follow-up to PR rust-lang#74774 (tracking issue rust-lang#75091), acting on some change requests made after approval: - adds `#[must_use]` attribute - changes type of `val` pointer argument from `()` to `u8` - adjusts documentation mentioning pointer provenance
2 parents d38997e + 19c9674 commit 06eb274

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

library/core/src/ptr/const_ptr.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,11 @@ impl<T: ?Sized> *const T {
662662
/// will only affect the pointer part, whereas for (thin) pointers to
663663
/// sized types, this has the same effect as a simple assignment.
664664
///
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+
///
665670
/// # Examples
666671
///
667672
/// This function is primarily useful for allowing byte-wise pointer
@@ -673,13 +678,17 @@ impl<T: ?Sized> *const T {
673678
/// let arr: [i32; 3] = [1, 2, 3];
674679
/// let mut ptr = &arr[0] as *const dyn Debug;
675680
/// 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+
/// }
678686
/// ```
679687
#[unstable(feature = "set_ptr_value", issue = "75091")]
688+
#[must_use = "returns a new pointer rather than modifying its argument"]
680689
#[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;
683692
// SAFETY: In case of a thin pointer, this operations is identical
684693
// to a simple assignment. In case of a fat pointer, with the current
685694
// fat pointer layout implementation, the first field of such a

library/core/src/ptr/mut_ptr.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,11 @@ impl<T: ?Sized> *mut T {
718718
/// will only affect the pointer part, whereas for (thin) pointers to
719719
/// sized types, this has the same effect as a simple assignment.
720720
///
721+
/// The resulting pointer will have provenance of `val`, i.e., for a fat
722+
/// pointer, this operation is semantically the same as creating a new
723+
/// fat pointer with the data pointer value of `val` but the metadata of
724+
/// `self`.
725+
///
721726
/// # Examples
722727
///
723728
/// This function is primarily useful for allowing byte-wise pointer
@@ -729,13 +734,17 @@ impl<T: ?Sized> *mut T {
729734
/// let mut arr: [i32; 3] = [1, 2, 3];
730735
/// let mut ptr = &mut arr[0] as *mut dyn Debug;
731736
/// let thin = ptr as *mut u8;
732-
/// ptr = ptr.set_ptr_value(unsafe { thin.add(8).cast() });
733-
/// assert_eq!(unsafe { *(ptr as *mut i32) }, 3);
737+
/// unsafe {
738+
/// ptr = ptr.set_ptr_value(thin.add(8));
739+
/// # assert_eq!(*(ptr as *mut i32), 3);
740+
/// println!("{:?}", &*ptr); // will print "3"
741+
/// }
734742
/// ```
735743
#[unstable(feature = "set_ptr_value", issue = "75091")]
744+
#[must_use = "returns a new pointer rather than modifying its argument"]
736745
#[inline]
737-
pub fn set_ptr_value(mut self, val: *mut ()) -> Self {
738-
let thin = &mut self as *mut *mut T as *mut *mut ();
746+
pub fn set_ptr_value(mut self, val: *mut u8) -> Self {
747+
let thin = &mut self as *mut *mut T as *mut *mut u8;
739748
// SAFETY: In case of a thin pointer, this operations is identical
740749
// to a simple assignment. In case of a fat pointer, with the current
741750
// fat pointer layout implementation, the first field of such a

0 commit comments

Comments
 (0)