@@ -117,6 +117,24 @@ impl<T: ?Sized> NonNull<T> {
117
117
/// The resulting lifetime is bound to self so this behaves "as if"
118
118
/// it were actually an instance of T that is getting borrowed. If a longer
119
119
/// (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
120
138
#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
121
139
#[ inline]
122
140
pub unsafe fn as_ref ( & self ) -> & T {
@@ -130,6 +148,24 @@ impl<T: ?Sized> NonNull<T> {
130
148
/// The resulting lifetime is bound to self so this behaves "as if"
131
149
/// it were actually an instance of T that is getting borrowed. If a longer
132
150
/// (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
133
169
#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
134
170
#[ inline]
135
171
pub unsafe fn as_mut ( & mut self ) -> & mut T {
0 commit comments