@@ -116,10 +116,11 @@ impl<T: ?Sized> *const T {
116
116
117
117
/// Gets the "address" portion of the pointer.
118
118
///
119
- /// This is similar to `self as usize`, which semantically discards *provenance* and
120
- /// *address-space* information. However, unlike `self as usize`, casting the returned address
121
- /// back to a pointer yields a [pointer without provenance][without_provenance], which is undefined behavior to dereference. To
122
- /// properly restore the lost information and obtain a dereferenceable pointer, use
119
+ /// This is similar to `self as usize`, except that the [provenance][crate::ptr#provenance] of
120
+ /// the pointer is discarded and not [exposed][crate::ptr#exposed-provenance]. This means that
121
+ /// casting the returned address back to a pointer yields a [pointer without
122
+ /// provenance][without_provenance], which is undefined behavior to dereference. To properly
123
+ /// restore the lost information and obtain a dereferenceable pointer, use
123
124
/// [`with_addr`][pointer::with_addr] or [`map_addr`][pointer::map_addr].
124
125
///
125
126
/// If using those APIs is not possible because there is no way to preserve a pointer with the
@@ -134,67 +135,61 @@ impl<T: ?Sized> *const T {
134
135
/// perform a change of representation to produce a value containing only the address
135
136
/// portion of the pointer. What that means is up to the platform to define.
136
137
///
137
- /// This API and its claimed semantics are part of the Strict Provenance experiment, and as such
138
- /// might change in the future (including possibly weakening this so it becomes wholly
139
- /// equivalent to `self as usize`). See the [module documentation][crate::ptr] for details.
138
+ /// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
140
139
#[ must_use]
141
140
#[ inline( always) ]
142
- #[ unstable ( feature = "strict_provenance" , issue = "95228 " ) ]
141
+ #[ stable ( feature = "strict_provenance" , since = "CURRENT_RUSTC_VERSION " ) ]
143
142
pub fn addr ( self ) -> usize {
144
- // FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
143
+ // A pointer-to-integer transmute has exactly the right semantics: it returns
144
+ // the address without exposing the provenance.
145
145
// SAFETY: Pointer-to-integer transmutes are valid (if you are okay with losing the
146
146
// provenance).
147
147
unsafe { mem:: transmute ( self . cast :: < ( ) > ( ) ) }
148
148
}
149
149
150
- /// Exposes the "provenance" part of the pointer for future use in
151
- /// [`with_exposed_provenance`][] and returns the "address" portion.
150
+ /// Exposes the [ "provenance"][crate::ptr#provenance] part of the pointer for future use in
151
+ /// [`with_exposed_provenance`] and returns the "address" portion.
152
152
///
153
- /// This is equivalent to `self as usize`, which semantically discards *provenance* and
154
- /// *address-space* information. Furthermore, this (like the `as` cast) has the implicit
155
- /// side-effect of marking the provenance as 'exposed', so on platforms that support it you can
156
- /// later call [`with_exposed_provenance`][] to reconstitute the original pointer including its
157
- /// provenance. (Reconstructing address space information, if required, is your responsibility.)
153
+ /// This is equivalent to `self as usize`, which semantically discards provenance information.
154
+ /// Furthermore, this (like the `as` cast) has the implicit side-effect of marking the
155
+ /// provenance as 'exposed', so on platforms that support it you can later call
156
+ /// [`with_exposed_provenance`] to reconstitute the original pointer including its provenance.
158
157
///
159
- /// Using this method means that code is *not* following [Strict
160
- /// Provenance][super#strict-provenance] rules. Supporting
161
- /// [`with_exposed_provenance`][] complicates specification and reasoning and may not be supported by
162
- /// tools that help you to stay conformant with the Rust memory model, so it is recommended to
163
- /// use [`addr`][pointer::addr] wherever possible.
158
+ /// Due to its inherent ambiguity, [`with_exposed_provenance`] may not be supported by tools
159
+ /// that help you to stay conformant with the Rust memory model. It is recommended to use
160
+ /// [Strict Provenance][self#strict-provenance] APIs such as [`with_addr`][pointer::with_addr]
161
+ /// wherever possible, in which case [`addr`][pointer::addr] should be used instead of `expose_provenance`.
164
162
///
165
163
/// On most platforms this will produce a value with the same bytes as the original pointer,
166
164
/// because all the bytes are dedicated to describing the address. Platforms which need to store
167
165
/// additional information in the pointer may not support this operation, since the 'expose'
168
- /// side-effect which is required for [`with_exposed_provenance`][] to work is typically not
166
+ /// side-effect which is required for [`with_exposed_provenance`] to work is typically not
169
167
/// available.
170
168
///
171
- /// It is unclear whether this method can be given a satisfying unambiguous specification. This
172
- /// API and its claimed semantics are part of [Exposed Provenance][super#exposed-provenance].
169
+ /// This is an [Exposed Provenance][crate::ptr#exposed-provenance] API.
173
170
///
174
171
/// [`with_exposed_provenance`]: with_exposed_provenance
175
172
#[ must_use]
176
173
#[ inline( always) ]
177
- #[ unstable ( feature = "exposed_provenance" , issue = "95228 " ) ]
174
+ #[ stable ( feature = "exposed_provenance" , since = "CURRENT_RUSTC_VERSION " ) ]
178
175
pub fn expose_provenance ( self ) -> usize {
179
- // FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
180
176
self . cast :: < ( ) > ( ) as usize
181
177
}
182
178
183
- /// Creates a new pointer with the given address.
179
+ /// Creates a new pointer with the given address and the [provenance][crate::ptr#provenance] of
180
+ /// `self`.
184
181
///
185
- /// This performs the same operation as an `addr as ptr` cast, but copies
186
- /// the *address-space* and *provenance* of `self` to the new pointer.
187
- /// This allows us to dynamically preserve and propagate this important
188
- /// information in a way that is otherwise impossible with a unary cast.
182
+ /// This is similar to a `addr as *const T` cast, but copies
183
+ /// the *provenance* of `self` to the new pointer.
184
+ /// This avoids the inherent ambiguity of the unary cast.
189
185
///
190
186
/// This is equivalent to using [`wrapping_offset`][pointer::wrapping_offset] to offset
191
187
/// `self` to the given address, and therefore has all the same capabilities and restrictions.
192
188
///
193
- /// This API and its claimed semantics are part of the Strict Provenance experiment,
194
- /// see the [module documentation][crate::ptr] for details.
189
+ /// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
195
190
#[ must_use]
196
191
#[ inline]
197
- #[ unstable ( feature = "strict_provenance" , issue = "95228 " ) ]
192
+ #[ stable ( feature = "strict_provenance" , since = "CURRENT_RUSTC_VERSION " ) ]
198
193
pub fn with_addr ( self , addr : usize ) -> Self {
199
194
// FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
200
195
//
@@ -209,15 +204,15 @@ impl<T: ?Sized> *const T {
209
204
self . wrapping_byte_offset ( offset)
210
205
}
211
206
212
- /// Creates a new pointer by mapping `self`'s address to a new one.
207
+ /// Creates a new pointer by mapping `self`'s address to a new one, preserving the
208
+ /// [provenance][crate::ptr#provenance] of `self`.
213
209
///
214
210
/// This is a convenience for [`with_addr`][pointer::with_addr], see that method for details.
215
211
///
216
- /// This API and its claimed semantics are part of the Strict Provenance experiment,
217
- /// see the [module documentation][crate::ptr] for details.
212
+ /// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
218
213
#[ must_use]
219
214
#[ inline]
220
- #[ unstable ( feature = "strict_provenance" , issue = "95228 " ) ]
215
+ #[ stable ( feature = "strict_provenance" , since = "CURRENT_RUSTC_VERSION " ) ]
221
216
pub fn map_addr ( self , f : impl FnOnce ( usize ) -> usize ) -> Self {
222
217
self . with_addr ( f ( self . addr ( ) ) )
223
218
}
0 commit comments