Skip to content

Commit 96887a1

Browse files
authored
Unrolled build for #142315
Rollup merge of #142315 - lolbinarycat:core-dedup-ptr-docs-139190-pt3, r=workingjubilee core::ptr: deduplicate docs for as_ref, addr, and as_uninit_ref also add INFO.md file explaining the purpose of the ptr/docs dir, and give some pointers (heh) to future maintainers. follow up to #142101 part of #139190 r? `@workingjubilee`
2 parents 5e33838 + d50a80e commit 96887a1

File tree

6 files changed

+111
-134
lines changed

6 files changed

+111
-134
lines changed

library/core/src/ptr/const_ptr.rs

Lines changed: 13 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -146,28 +146,7 @@ impl<T: PointeeSized> *const T {
146146
self as _
147147
}
148148

149-
/// Gets the "address" portion of the pointer.
150-
///
151-
/// This is similar to `self as usize`, except that the [provenance][crate::ptr#provenance] of
152-
/// the pointer is discarded and not [exposed][crate::ptr#exposed-provenance]. This means that
153-
/// casting the returned address back to a pointer yields a [pointer without
154-
/// provenance][without_provenance], which is undefined behavior to dereference. To properly
155-
/// restore the lost information and obtain a dereferenceable pointer, use
156-
/// [`with_addr`][pointer::with_addr] or [`map_addr`][pointer::map_addr].
157-
///
158-
/// If using those APIs is not possible because there is no way to preserve a pointer with the
159-
/// required provenance, then Strict Provenance might not be for you. Use pointer-integer casts
160-
/// or [`expose_provenance`][pointer::expose_provenance] and [`with_exposed_provenance`][with_exposed_provenance]
161-
/// instead. However, note that this makes your code less portable and less amenable to tools
162-
/// that check for compliance with the Rust memory model.
163-
///
164-
/// On most platforms this will produce a value with the same bytes as the original
165-
/// pointer, because all the bytes are dedicated to describing the address.
166-
/// Platforms which need to store additional information in the pointer may
167-
/// perform a change of representation to produce a value containing only the address
168-
/// portion of the pointer. What that means is up to the platform to define.
169-
///
170-
/// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
149+
#[doc = include_str!("./docs/addr.md")]
171150
#[must_use]
172151
#[inline(always)]
173152
#[stable(feature = "strict_provenance", since = "1.84.0")]
@@ -254,23 +233,16 @@ impl<T: PointeeSized> *const T {
254233
(self.cast(), metadata(self))
255234
}
256235

257-
/// Returns `None` if the pointer is null, or else returns a shared reference to
258-
/// the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_ref`]
259-
/// must be used instead.
260-
///
261-
/// [`as_uninit_ref`]: #method.as_uninit_ref
262-
///
263-
/// # Safety
264-
///
265-
/// When calling this method, you have to ensure that *either* the pointer is null *or*
266-
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
236+
#[doc = include_str!("./docs/as_ref.md")]
267237
///
268-
/// # Panics during const evaluation
269-
///
270-
/// This method will panic during const evaluation if the pointer cannot be
271-
/// determined to be null or not. See [`is_null`] for more information.
238+
/// ```
239+
/// let ptr: *const u8 = &10u8 as *const u8;
272240
///
273-
/// [`is_null`]: #method.is_null
241+
/// unsafe {
242+
/// let val_back = &*ptr;
243+
/// assert_eq!(val_back, &10);
244+
/// }
245+
/// ```
274246
///
275247
/// # Examples
276248
///
@@ -284,20 +256,9 @@ impl<T: PointeeSized> *const T {
284256
/// }
285257
/// ```
286258
///
287-
/// # Null-unchecked version
288259
///
289-
/// If you are sure the pointer can never be null and are looking for some kind of
290-
/// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>`, know that you can
291-
/// dereference the pointer directly.
292-
///
293-
/// ```
294-
/// let ptr: *const u8 = &10u8 as *const u8;
295-
///
296-
/// unsafe {
297-
/// let val_back = &*ptr;
298-
/// assert_eq!(val_back, &10);
299-
/// }
300-
/// ```
260+
/// [`is_null`]: #method.is_null
261+
/// [`as_uninit_ref`]: #method.as_uninit_ref
301262
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
302263
#[rustc_const_stable(feature = "const_ptr_is_null", since = "1.84.0")]
303264
#[inline]
@@ -338,23 +299,10 @@ impl<T: PointeeSized> *const T {
338299
unsafe { &*self }
339300
}
340301

341-
/// Returns `None` if the pointer is null, or else returns a shared reference to
342-
/// the value wrapped in `Some`. In contrast to [`as_ref`], this does not require
343-
/// that the value has to be initialized.
344-
///
345-
/// [`as_ref`]: #method.as_ref
346-
///
347-
/// # Safety
348-
///
349-
/// When calling this method, you have to ensure that *either* the pointer is null *or*
350-
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
351-
///
352-
/// # Panics during const evaluation
353-
///
354-
/// This method will panic during const evaluation if the pointer cannot be
355-
/// determined to be null or not. See [`is_null`] for more information.
302+
#[doc = include_str!("./docs/as_uninit_ref.md")]
356303
///
357304
/// [`is_null`]: #method.is_null
305+
/// [`as_ref`]: #method.as_ref
358306
///
359307
/// # Examples
360308
///

library/core/src/ptr/docs/INFO.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
This directory holds method documentation that otherwise
2+
would be duplicated across mutable and immutable pointers.
3+
4+
Note that most of the docs here are not the complete docs
5+
for their corresponding method. This is for a few reasons:
6+
7+
1. Examples need to be different for mutable/immutable
8+
pointers, in order to actually call the correct method.
9+
2. Link reference definitions are frequently different
10+
between mutable/immutable pointers, in order to link to
11+
the correct method.
12+
For example, `<*const T>::as_ref` links to
13+
`<*const T>::is_null`, while `<*mut T>::as_ref` links to
14+
`<*mut T>::is_null`.
15+
3. Many methods on mutable pointers link to an alternate
16+
version that returns a mutable reference instead of
17+
a shared reference.
18+
19+
Always review the rendered docs manually when making
20+
changes to these files to make sure you're not accidentally
21+
splitting up a section.

library/core/src/ptr/docs/addr.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Gets the "address" portion of the pointer.
2+
3+
This is similar to `self as usize`, except that the [provenance][crate::ptr#provenance] of
4+
the pointer is discarded and not [exposed][crate::ptr#exposed-provenance]. This means that
5+
casting the returned address back to a pointer yields a [pointer without
6+
provenance][without_provenance], which is undefined behavior to dereference. To properly
7+
restore the lost information and obtain a dereferenceable pointer, use
8+
[`with_addr`][pointer::with_addr] or [`map_addr`][pointer::map_addr].
9+
10+
If using those APIs is not possible because there is no way to preserve a pointer with the
11+
required provenance, then Strict Provenance might not be for you. Use pointer-integer casts
12+
or [`expose_provenance`][pointer::expose_provenance] and [`with_exposed_provenance`][with_exposed_provenance]
13+
instead. However, note that this makes your code less portable and less amenable to tools
14+
that check for compliance with the Rust memory model.
15+
16+
On most platforms this will produce a value with the same bytes as the original
17+
pointer, because all the bytes are dedicated to describing the address.
18+
Platforms which need to store additional information in the pointer may
19+
perform a change of representation to produce a value containing only the address
20+
portion of the pointer. What that means is up to the platform to define.
21+
22+
This is a [Strict Provenance][crate::ptr#strict-provenance] API.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Returns `None` if the pointer is null, or else returns a shared reference to
2+
the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_ref`]
3+
must be used instead.
4+
5+
# Safety
6+
7+
When calling this method, you have to ensure that *either* the pointer is null *or*
8+
the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
9+
10+
# Panics during const evaluation
11+
12+
This method will panic during const evaluation if the pointer cannot be
13+
determined to be null or not. See [`is_null`] for more information.
14+
15+
# Null-unchecked version
16+
17+
If you are sure the pointer can never be null and are looking for some kind of
18+
`as_ref_unchecked` that returns the `&T` instead of `Option<&T>`, know that you can
19+
dereference the pointer directly.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Returns `None` if the pointer is null, or else returns a shared reference to
2+
the value wrapped in `Some`. In contrast to [`as_ref`], this does not require
3+
that the value has to be initialized.
4+
5+
# Safety
6+
7+
When calling this method, you have to ensure that *either* the pointer is null *or*
8+
the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
9+
Note that because the created reference is to `MaybeUninit<T>`, the
10+
source pointer can point to uninitialized memory.
11+
12+
# Panics during const evaluation
13+
14+
This method will panic during const evaluation if the pointer cannot be
15+
determined to be null or not. See [`is_null`] for more information.

library/core/src/ptr/mut_ptr.rs

Lines changed: 21 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -135,28 +135,9 @@ impl<T: PointeeSized> *mut T {
135135
self as _
136136
}
137137

138-
/// Gets the "address" portion of the pointer.
139-
///
140-
/// This is similar to `self as usize`, except that the [provenance][crate::ptr#provenance] of
141-
/// the pointer is discarded and not [exposed][crate::ptr#exposed-provenance]. This means that
142-
/// casting the returned address back to a pointer yields a [pointer without
143-
/// provenance][without_provenance_mut], which is undefined behavior to dereference. To properly
144-
/// restore the lost information and obtain a dereferenceable pointer, use
145-
/// [`with_addr`][pointer::with_addr] or [`map_addr`][pointer::map_addr].
146-
///
147-
/// If using those APIs is not possible because there is no way to preserve a pointer with the
148-
/// required provenance, then Strict Provenance might not be for you. Use pointer-integer casts
149-
/// or [`expose_provenance`][pointer::expose_provenance] and [`with_exposed_provenance`][with_exposed_provenance]
150-
/// instead. However, note that this makes your code less portable and less amenable to tools
151-
/// that check for compliance with the Rust memory model.
152-
///
153-
/// On most platforms this will produce a value with the same bytes as the original
154-
/// pointer, because all the bytes are dedicated to describing the address.
155-
/// Platforms which need to store additional information in the pointer may
156-
/// perform a change of representation to produce a value containing only the address
157-
/// portion of the pointer. What that means is up to the platform to define.
138+
#[doc = include_str!("./docs/addr.md")]
158139
///
159-
/// This is a [Strict Provenance][crate::ptr#strict-provenance] API.
140+
/// [without_provenance]: without_provenance_mut
160141
#[must_use]
161142
#[inline(always)]
162143
#[stable(feature = "strict_provenance", since = "1.84.0")]
@@ -243,26 +224,16 @@ impl<T: PointeeSized> *mut T {
243224
(self.cast(), super::metadata(self))
244225
}
245226

246-
/// Returns `None` if the pointer is null, or else returns a shared reference to
247-
/// the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_ref`]
248-
/// must be used instead.
249-
///
250-
/// For the mutable counterpart see [`as_mut`].
227+
#[doc = include_str!("./docs/as_ref.md")]
251228
///
252-
/// [`as_uninit_ref`]: pointer#method.as_uninit_ref-1
253-
/// [`as_mut`]: #method.as_mut
254-
///
255-
/// # Safety
256-
///
257-
/// When calling this method, you have to ensure that *either* the pointer is null *or*
258-
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
259-
///
260-
/// # Panics during const evaluation
261-
///
262-
/// This method will panic during const evaluation if the pointer cannot be
263-
/// determined to be null or not. See [`is_null`] for more information.
229+
/// ```
230+
/// let ptr: *mut u8 = &mut 10u8 as *mut u8;
264231
///
265-
/// [`is_null`]: #method.is_null-1
232+
/// unsafe {
233+
/// let val_back = &*ptr;
234+
/// println!("We got back the value: {val_back}!");
235+
/// }
236+
/// ```
266237
///
267238
/// # Examples
268239
///
@@ -276,20 +247,14 @@ impl<T: PointeeSized> *mut T {
276247
/// }
277248
/// ```
278249
///
279-
/// # Null-unchecked version
280-
///
281-
/// If you are sure the pointer can never be null and are looking for some kind of
282-
/// `as_ref_unchecked` that returns the `&T` instead of `Option<&T>`, know that you can
283-
/// dereference the pointer directly.
250+
/// # See Also
284251
///
285-
/// ```
286-
/// let ptr: *mut u8 = &mut 10u8 as *mut u8;
252+
/// For the mutable counterpart see [`as_mut`].
287253
///
288-
/// unsafe {
289-
/// let val_back = &*ptr;
290-
/// println!("We got back the value: {val_back}!");
291-
/// }
292-
/// ```
254+
/// [`is_null`]: #method.is_null-1
255+
/// [`as_uninit_ref`]: pointer#method.as_uninit_ref-1
256+
/// [`as_mut`]: #method.as_mut
257+
293258
#[stable(feature = "ptr_as_ref", since = "1.9.0")]
294259
#[rustc_const_stable(feature = "const_ptr_is_null", since = "1.84.0")]
295260
#[inline]
@@ -332,28 +297,15 @@ impl<T: PointeeSized> *mut T {
332297
unsafe { &*self }
333298
}
334299

335-
/// Returns `None` if the pointer is null, or else returns a shared reference to
336-
/// the value wrapped in `Some`. In contrast to [`as_ref`], this does not require
337-
/// that the value has to be initialized.
338-
///
339-
/// For the mutable counterpart see [`as_uninit_mut`].
300+
#[doc = include_str!("./docs/as_uninit_ref.md")]
340301
///
302+
/// [`is_null`]: #method.is_null-1
341303
/// [`as_ref`]: pointer#method.as_ref-1
342-
/// [`as_uninit_mut`]: #method.as_uninit_mut
343-
///
344-
/// # Safety
345-
///
346-
/// When calling this method, you have to ensure that *either* the pointer is null *or*
347-
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
348-
/// Note that because the created reference is to `MaybeUninit<T>`, the
349-
/// source pointer can point to uninitialized memory.
350-
///
351-
/// # Panics during const evaluation
352304
///
353-
/// This method will panic during const evaluation if the pointer cannot be
354-
/// determined to be null or not. See [`is_null`] for more information.
305+
/// # See Also
306+
/// For the mutable counterpart see [`as_uninit_mut`].
355307
///
356-
/// [`is_null`]: #method.is_null-1
308+
/// [`as_uninit_mut`]: #method.as_uninit_mut
357309
///
358310
/// # Examples
359311
///

0 commit comments

Comments
 (0)