Skip to content

Commit 571348b

Browse files
committed
create a new section on pointer to reference conversion
also start deduplicating the docs that are getting moved to this section.
1 parent 09bda4f commit 571348b

File tree

2 files changed

+28
-36
lines changed

2 files changed

+28
-36
lines changed

Diff for: core/src/ptr/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,30 @@
5656
//! has size 0, i.e., even if memory is not actually touched. Consider using
5757
//! [`NonNull::dangling`] in such cases.
5858
//!
59+
//! ## Pointer to reference conversion
60+
//! When converting a pointer to a reference using `&*`, there are several
61+
//! rules that must be followed:
62+
//! * The pointer must be properly aligned.
63+
//!
64+
//! * It must be "dereferenceable" in the sense defined above
65+
//!
66+
//! * The pointer must point to an initialized instance of `T`.
67+
//!
68+
//! * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
69+
//! arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
70+
//! In particular, while this reference exists, the memory the pointer points to must
71+
//! not get accessed (read or written) through any other pointer.
72+
//!
73+
//! If a pointer follows all of these rules, it is said to be
74+
//! *convertable to a reference*.
75+
//!
76+
//! These apply even if the result is unused!
77+
//! (The part about being initialized is not yet fully decided, but until
78+
//! it is, the only safe approach is to ensure that they are indeed initialized.)
79+
//!
80+
//! An example of the implications of the above rules is that an expression such
81+
//! as `unsafe { &*(0 as *const u8) }` is Immediate Undefined Behavior.
82+
//!
5983
//! ## Allocated object
6084
//!
6185
//! An *allocated object* is a subset of program memory which is addressable

Diff for: core/src/ptr/mut_ptr.rs

+4-36
Original file line numberDiff line numberDiff line change
@@ -247,24 +247,7 @@ impl<T: ?Sized> *mut T {
247247
/// # Safety
248248
///
249249
/// When calling this method, you have to ensure that *either* the pointer is null *or*
250-
/// all of the following is true:
251-
///
252-
/// * The pointer must be properly aligned.
253-
///
254-
/// * It must be "dereferenceable" in the sense defined in [the module documentation].
255-
///
256-
/// * The pointer must point to an initialized instance of `T`.
257-
///
258-
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
259-
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
260-
/// In particular, while this reference exists, the memory the pointer points to must
261-
/// not get mutated (except inside `UnsafeCell`).
262-
///
263-
/// This applies even if the result of this method is unused!
264-
/// (The part about being initialized is not yet fully decided, but until
265-
/// it is, the only safe approach is to ensure that they are indeed initialized.)
266-
///
267-
/// [the module documentation]: crate::ptr#safety
250+
/// the pointer is [convirtible to a reference](crate::ptr#pointer-to-reference-conversion)
268251
///
269252
/// # Examples
270253
///
@@ -609,25 +592,10 @@ impl<T: ?Sized> *mut T {
609592
///
610593
/// # Safety
611594
///
612-
/// When calling this method, you have to ensure that *either* the pointer is null *or*
613-
/// all of the following is true:
614-
///
615-
/// * The pointer must be properly aligned.
616-
///
617-
/// * It must be "dereferenceable" in the sense defined in [the module documentation].
618-
///
619-
/// * The pointer must point to an initialized instance of `T`.
620-
///
621-
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
622-
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
623-
/// In particular, while this reference exists, the memory the pointer points to must
624-
/// not get accessed (read or written) through any other pointer.
595+
/// When calling this method, you have to ensure that *either*
596+
/// the pointer is null *or*
597+
/// the pointer is [convirtible to a reference](crate::ptr#pointer-to-reference-conversion)
625598
///
626-
/// This applies even if the result of this method is unused!
627-
/// (The part about being initialized is not yet fully decided, but until
628-
/// it is, the only safe approach is to ensure that they are indeed initialized.)
629-
///
630-
/// [the module documentation]: crate::ptr#safety
631599
///
632600
/// # Examples
633601
///

0 commit comments

Comments
 (0)