diff options
| author | Tyler Mandry <tmandry@gmail.com> | 2020-08-11 12:28:37 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-11 12:28:37 -0700 |
| commit | 06eb274bfcedeb69fc60068bdc2da14f57a442d1 (patch) | |
| tree | 589c25fa8a5d17377f79bea25b408b5d963950d2 | |
| parent | d38997e4d91b5c9d730e3c00ab00df93bc1412c8 (diff) | |
| parent | 19c9674966273750c757eaae2b8d7b2246dcbe3f (diff) | |
| download | rust-06eb274bfcedeb69fc60068bdc2da14f57a442d1.tar.gz rust-06eb274bfcedeb69fc60068bdc2da14f57a442d1.zip | |
Rollup merge of #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 #74774 (tracking issue #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
| -rw-r--r-- | library/core/src/ptr/const_ptr.rs | 17 | ||||
| -rw-r--r-- | library/core/src/ptr/mut_ptr.rs | 17 |
2 files changed, 26 insertions, 8 deletions
diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index a16970e9fd1..ac20897d258 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -662,6 +662,11 @@ impl<T: ?Sized> *const T { /// will only affect the pointer part, whereas for (thin) pointers to /// sized types, this has the same effect as a simple assignment. /// + /// The resulting pointer will have provenance of `val`, i.e., for a fat + /// pointer, this operation is semantically the same as creating a new + /// fat pointer with the data pointer value of `val` but the metadata of + /// `self`. + /// /// # Examples /// /// This function is primarily useful for allowing byte-wise pointer @@ -673,13 +678,17 @@ impl<T: ?Sized> *const T { /// let arr: [i32; 3] = [1, 2, 3]; /// let mut ptr = &arr[0] as *const dyn Debug; /// let thin = ptr as *const u8; - /// ptr = ptr.set_ptr_value(unsafe { thin.add(8).cast() }); - /// assert_eq!(unsafe { *(ptr as *const i32) }, 3); + /// unsafe { + /// ptr = ptr.set_ptr_value(thin.add(8)); + /// # assert_eq!(*(ptr as *const i32), 3); + /// println!("{:?}", &*ptr); // will print "3" + /// } /// ``` #[unstable(feature = "set_ptr_value", issue = "75091")] + #[must_use = "returns a new pointer rather than modifying its argument"] #[inline] - pub fn set_ptr_value(mut self, val: *const ()) -> Self { - let thin = &mut self as *mut *const T as *mut *const (); + pub fn set_ptr_value(mut self, val: *const u8) -> Self { + let thin = &mut self as *mut *const T as *mut *const u8; // SAFETY: In case of a thin pointer, this operations is identical // to a simple assignment. In case of a fat pointer, with the current // fat pointer layout implementation, the first field of such a diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index b47f90c5996..df00139118a 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -718,6 +718,11 @@ impl<T: ?Sized> *mut T { /// will only affect the pointer part, whereas for (thin) pointers to /// sized types, this has the same effect as a simple assignment. /// + /// The resulting pointer will have provenance of `val`, i.e., for a fat + /// pointer, this operation is semantically the same as creating a new + /// fat pointer with the data pointer value of `val` but the metadata of + /// `self`. + /// /// # Examples /// /// This function is primarily useful for allowing byte-wise pointer @@ -729,13 +734,17 @@ impl<T: ?Sized> *mut T { /// let mut arr: [i32; 3] = [1, 2, 3]; /// let mut ptr = &mut arr[0] as *mut dyn Debug; /// let thin = ptr as *mut u8; - /// ptr = ptr.set_ptr_value(unsafe { thin.add(8).cast() }); - /// assert_eq!(unsafe { *(ptr as *mut i32) }, 3); + /// unsafe { + /// ptr = ptr.set_ptr_value(thin.add(8)); + /// # assert_eq!(*(ptr as *mut i32), 3); + /// println!("{:?}", &*ptr); // will print "3" + /// } /// ``` #[unstable(feature = "set_ptr_value", issue = "75091")] + #[must_use = "returns a new pointer rather than modifying its argument"] #[inline] - pub fn set_ptr_value(mut self, val: *mut ()) -> Self { - let thin = &mut self as *mut *mut T as *mut *mut (); + pub fn set_ptr_value(mut self, val: *mut u8) -> Self { + let thin = &mut self as *mut *mut T as *mut *mut u8; // SAFETY: In case of a thin pointer, this operations is identical // to a simple assignment. In case of a fat pointer, with the current // fat pointer layout implementation, the first field of such a |
