Skip to content

Commit cb75fea

Browse files
authored
Rollup merge of #75266 - aticu:master, r=RalfJung
Add safety section to `NonNull::as_*` method docs This basically adds the safety section of `*mut T::as_{ref,mut}` to the same methods on `NonNull` with minor modifications to fit the differences. Part of #48929.
2 parents cbc6914 + c2099b5 commit cb75fea

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

library/core/src/ptr/non_null.rs

+36
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ impl<T: ?Sized> NonNull<T> {
117117
/// The resulting lifetime is bound to self so this behaves "as if"
118118
/// it were actually an instance of T that is getting borrowed. If a longer
119119
/// (unbound) lifetime is needed, use `&*my_ptr.as_ptr()`.
120+
///
121+
/// # Safety
122+
///
123+
/// When calling this method, you have to ensure that all of the following is true:
124+
/// - `self` is properly aligned
125+
/// - `self` must point to an initialized instance of T; in particular, the pointer must be
126+
/// "dereferencable" in the sense defined [here].
127+
///
128+
/// This applies even if the result of this method is unused!
129+
/// (The part about being initialized is not yet fully decided, but until
130+
/// it is, the only safe approach is to ensure that they are indeed initialized.)
131+
///
132+
/// Additionally, the lifetime of `self` does not necessarily reflect the actual
133+
/// lifetime of the data. *You* must enforce Rust's aliasing rules. In particular,
134+
/// for the duration of this lifetime, the memory the pointer points to must not
135+
/// get mutated (except inside `UnsafeCell`).
136+
///
137+
/// [here]: crate::ptr#safety
120138
#[stable(feature = "nonnull", since = "1.25.0")]
121139
#[inline]
122140
pub unsafe fn as_ref(&self) -> &T {
@@ -130,6 +148,24 @@ impl<T: ?Sized> NonNull<T> {
130148
/// The resulting lifetime is bound to self so this behaves "as if"
131149
/// it were actually an instance of T that is getting borrowed. If a longer
132150
/// (unbound) lifetime is needed, use `&mut *my_ptr.as_ptr()`.
151+
///
152+
/// # Safety
153+
///
154+
/// When calling this method, you have to ensure that all of the following is true:
155+
/// - `self` is properly aligned
156+
/// - `self` must point to an initialized instance of T; in particular, the pointer must be
157+
/// "dereferenceable" in the sense defined [here].
158+
///
159+
/// This applies even if the result of this method is unused!
160+
/// (The part about being initialized is not yet fully decided, but until
161+
/// it is the only safe approach is to ensure that they are indeed initialized.)
162+
///
163+
/// Additionally, the lifetime of `self` does not necessarily reflect the actual
164+
/// lifetime of the data. *You* must enforce Rust's aliasing rules. In particular,
165+
/// for the duration of this lifetime, the memory this pointer points to must not
166+
/// get accessed (read or written) through any other pointer.
167+
///
168+
/// [here]: crate::ptr#safety
133169
#[stable(feature = "nonnull", since = "1.25.0")]
134170
#[inline]
135171
pub unsafe fn as_mut(&mut self) -> &mut T {

0 commit comments

Comments
 (0)