Skip to content

Commit 74c9c3b

Browse files
committed
fix missing doc in as_ref
1 parent f525052 commit 74c9c3b

File tree

2 files changed

+67
-37
lines changed

2 files changed

+67
-37
lines changed

library/core/src/ptr/mut_ptr.rs

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -233,25 +233,30 @@ impl<T: ?Sized> *mut T {
233233
}
234234

235235
/// Returns `None` if the pointer is null, or else returns a shared reference to
236-
/// the value wrapped in `Some`. If the value may be uninitialized, [`as_uninit_ref`]
237-
/// must be used instead.
236+
/// the value wrapped in `Some`.
238237
///
239238
/// For the mutable counterpart see [`as_mut`].
240239
///
241-
/// [`as_uninit_ref`]: pointer#method.as_uninit_ref-1
242240
/// [`as_mut`]: #method.as_mut
243241
///
244242
/// # Safety
245243
///
246-
/// When calling this method, you have to ensure that *either* the pointer is null *or*
247-
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
244+
/// When calling this method, you have to ensure that:
245+
///
246+
/// * *Either* the pointer is null *or* the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
247+
///
248+
/// * The value of memory pointed by the ptr must be initialized. If not, [`as_uninit_ref`] must be used instead.
249+
///
250+
/// * Note that after obtaining the reference, the original pointer must not
251+
/// be mutated until the reference's lifetime ends (except inside `UnsafeCell`).
248252
///
249253
/// # Panics during const evaluation
250254
///
251255
/// This method will panic during const evaluation if the pointer cannot be
252256
/// determined to be null or not. See [`is_null`] for more information.
253257
///
254258
/// [`is_null`]: #method.is_null-1
259+
/// [`as_uninit_ref`]: pointer#method.as_uninit_ref-1
255260
///
256261
/// # Examples
257262
///
@@ -289,18 +294,25 @@ impl<T: ?Sized> *mut T {
289294
}
290295

291296
/// Returns a shared reference to the value behind the pointer.
292-
/// If the pointer may be null or the value may be uninitialized, [`as_uninit_ref`] must be used instead.
293-
/// If the pointer may be null, but the value is known to have been initialized, [`as_ref`] must be used instead.
294297
///
295298
/// For the mutable counterpart see [`as_mut_unchecked`].
296299
///
297-
/// [`as_ref`]: #method.as_ref
298-
/// [`as_uninit_ref`]: #method.as_uninit_ref
299300
/// [`as_mut_unchecked`]: #method.as_mut_unchecked
300301
///
301302
/// # Safety
302303
///
303-
/// When calling this method, you have to ensure that the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
304+
/// When calling this method, you have to ensure that:
305+
///
306+
/// * The pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
307+
///
308+
/// * If the pointer may be null or the value may be uninitialized, [`as_uninit_ref`] must be used instead.
309+
/// If the pointer may be null, but the value is known to have been initialized, [`as_ref`] must be used instead.
310+
///
311+
/// * Note that after obtaining the reference, the original pointer must not
312+
/// be mutated until the reference's lifetime ends (except inside `UnsafeCell`).
313+
///
314+
/// [`as_ref`]: #method.as_ref
315+
/// [`as_uninit_ref`]: #method.as_uninit_ref
304316
///
305317
/// # Examples
306318
///
@@ -332,10 +344,10 @@ impl<T: ?Sized> *mut T {
332344
///
333345
/// # Safety
334346
///
335-
/// When calling this method, you have to ensure that *either* the pointer is null *or*
336-
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
337-
/// Note that because the created reference is to `MaybeUninit<T>`, the
338-
/// source pointer can point to uninitialized memory.
347+
/// * The pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
348+
///
349+
/// * Note that after obtaining the reference, the original pointer must not
350+
/// be mutated until the reference's lifetime ends (except inside `UnsafeCell`).
339351
///
340352
/// # Panics during const evaluation
341353
///
@@ -607,7 +619,8 @@ impl<T: ?Sized> *mut T {
607619
///
608620
/// * The value of memory pointed by the ptr must be initialized. If not, [`as_uninit_mut`] must be used instead.
609621
///
610-
/// * Note that multiple calls to this API may create multiple mutable references simultaneously, violating the exclusive mutable reference principle.
622+
/// * Note that after obtaining the mutable reference, the original pointer
623+
/// must not be used to access the data until the reference's lifetime ends.
611624
///
612625
/// # Panics during const evaluation
613626
///
@@ -653,19 +666,25 @@ impl<T: ?Sized> *mut T {
653666
}
654667

655668
/// Returns a unique reference to the value behind the pointer.
656-
/// If the pointer may be null or the value may be uninitialized, [`as_uninit_mut`] must be used instead.
657-
/// If the pointer may be null, but the value is known to have been initialized, [`as_mut`] must be used instead.
658669
///
659670
/// For the shared counterpart see [`as_ref_unchecked`].
660671
///
661-
/// [`as_mut`]: #method.as_mut
662-
/// [`as_uninit_mut`]: #method.as_uninit_mut
663672
/// [`as_ref_unchecked`]: #method.as_mut_unchecked
664673
///
665674
/// # Safety
666675
///
667-
/// When calling this method, you have to ensure that
668-
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
676+
/// When calling this method, you have to ensure that:
677+
///
678+
/// * The pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
679+
///
680+
/// * If the pointer may be null or the value may be uninitialized, [`as_uninit_mut`] must be used instead.
681+
/// If the pointer may be null, but the value is known to have been initialized, [`as_mut`] must be used instead.
682+
///
683+
/// * Note that after obtaining the mutable reference, the original pointer
684+
/// must not be used to access the data until the reference's lifetime ends.
685+
///
686+
/// [`as_mut`]: #method.as_mut
687+
/// [`as_uninit_mut`]: #method.as_uninit_mut
669688
///
670689
/// # Examples
671690
///
@@ -698,8 +717,10 @@ impl<T: ?Sized> *mut T {
698717
///
699718
/// # Safety
700719
///
701-
/// When calling this method, you have to ensure that *either* the pointer is null *or*
702-
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
720+
/// * The pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
721+
///
722+
/// * Note that after obtaining the mutable reference, the original pointer must not
723+
/// be used to access the data until the reference's lifetime ends.
703724
///
704725
/// # Panics during const evaluation
705726
///

library/core/src/ptr/non_null.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@ impl<T: Sized> NonNull<T> {
152152
///
153153
/// # Safety
154154
///
155-
/// When calling this method, you have to ensure that
156-
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
157-
/// Note that because the created reference is to `MaybeUninit<T>`, the
158-
/// source pointer can point to uninitialized memory.
155+
/// * The pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
156+
///
157+
/// * Note that after obtaining the reference, the original pointer must not
158+
/// be mutated until the reference's lifetime ends (except inside `UnsafeCell`).
159+
///
159160
#[inline]
160161
#[must_use]
161162
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
@@ -175,10 +176,11 @@ impl<T: Sized> NonNull<T> {
175176
///
176177
/// # Safety
177178
///
178-
/// When calling this method, you have to ensure that
179-
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
180-
/// Note that because the created reference is to `MaybeUninit<T>`, the
181-
/// source pointer can point to uninitialized memory.
179+
/// * The pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
180+
///
181+
/// * Note that after obtaining the mutable reference, the original pointer must not
182+
/// be used to access the data until the reference's lifetime ends.
183+
///
182184
#[inline]
183185
#[must_use]
184186
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
@@ -391,18 +393,24 @@ impl<T: ?Sized> NonNull<T> {
391393
unsafe { mem::transmute::<Self, *mut T>(self) }
392394
}
393395

394-
/// Returns a shared reference to the value. If the value may be uninitialized, [`as_uninit_ref`]
395-
/// must be used instead.
396+
/// Returns a shared reference to the value.
396397
///
397398
/// For the mutable counterpart see [`as_mut`].
398399
///
399-
/// [`as_uninit_ref`]: NonNull::as_uninit_ref
400400
/// [`as_mut`]: NonNull::as_mut
401401
///
402402
/// # Safety
403403
///
404-
/// When calling this method, you have to ensure that
405-
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
404+
/// When calling this method, you have to ensure that:
405+
///
406+
/// * The pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
407+
///
408+
/// * The value of memory pointed by the ptr must be initialized. If not, [`as_uninit_ref`] must be used instead.
409+
///
410+
/// * Note that after obtaining the mutable reference, the original pointer must not
411+
/// be mutated until the reference's lifetime ends (except inside `UnsafeCell`).
412+
///
413+
/// [`as_uninit_ref`]: NonNull::as_uninit_ref
406414
///
407415
/// # Examples
408416
///
@@ -442,7 +450,8 @@ impl<T: ?Sized> NonNull<T> {
442450
///
443451
/// * The value of memory pointed by the ptr must be initialized. If not, [`as_uninit_mut`] must be used instead.
444452
///
445-
/// * Note that multiple calls to this API may create multiple mutable references simultaneously, violating the exclusive mutable reference principle.
453+
/// * Note that after obtaining the mutable reference, the original pointer
454+
/// must not be used to access the data until the reference's lifetime ends.
446455
///
447456
/// [`as_uninit_mut`]: NonNull::as_uninit_mut
448457
///

0 commit comments

Comments
 (0)