@@ -71,6 +71,16 @@ impl<T: Sized> NonNull<T> {
71
71
/// a `T`, which means this must not be used as a "not yet initialized"
72
72
/// sentinel value. Types that lazily allocate must track initialization by
73
73
/// some other means.
74
+ ///
75
+ /// # Examples
76
+ ///
77
+ /// ```
78
+ /// use std::ptr::NonNull;
79
+ ///
80
+ /// let ptr = NonNull::<u32>::dangling();
81
+ /// // Important: don't try to access the value of `ptr` without
82
+ /// // initializing it first! The pointer is not null but isn't valid either!
83
+ /// ```
74
84
#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
75
85
#[ rustc_const_stable( feature = "const_nonnull_dangling" , since = "1.36.0" ) ]
76
86
#[ inline]
@@ -155,6 +165,18 @@ impl<T: ?Sized> NonNull<T> {
155
165
/// # Safety
156
166
///
157
167
/// `ptr` must be non-null.
168
+ ///
169
+ /// # Examples
170
+ ///
171
+ /// ```
172
+ /// use std::ptr::NonNull;
173
+ ///
174
+ /// let mut x = 0u32;
175
+ /// let ptr = unsafe { NonNull::new_unchecked(&mut x as *mut _) };
176
+ ///
177
+ /// // NEVER DO THAT!!!
178
+ /// let ptr = unsafe { NonNull::<u32>::new_unchecked(std::ptr::null_mut()) };
179
+ /// ```
158
180
#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
159
181
#[ rustc_const_stable( feature = "const_nonnull_new_unchecked" , since = "1.25.0" ) ]
160
182
#[ inline]
@@ -164,6 +186,19 @@ impl<T: ?Sized> NonNull<T> {
164
186
}
165
187
166
188
/// Creates a new `NonNull` if `ptr` is non-null.
189
+ ///
190
+ /// # Examples
191
+ ///
192
+ /// ```
193
+ /// use std::ptr::NonNull;
194
+ ///
195
+ /// let mut x = 0u32;
196
+ /// let ptr = NonNull::<u32>::new(&mut x as *mut _).expect("ptr is null!");
197
+ ///
198
+ /// if let Some(ptr) = NonNull::<u32>::new(std::ptr::null_mut()) {
199
+ /// unreachable!();
200
+ /// }
201
+ /// ```
167
202
#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
168
203
#[ inline]
169
204
pub fn new ( ptr : * mut T ) -> Option < Self > {
@@ -205,6 +240,22 @@ impl<T: ?Sized> NonNull<T> {
205
240
}
206
241
207
242
/// Acquires the underlying `*mut` pointer.
243
+ ///
244
+ /// # Examples
245
+ ///
246
+ /// ```
247
+ /// use std::ptr::NonNull;
248
+ ///
249
+ /// let mut x = 0u32;
250
+ /// let ptr = NonNull::new(&mut x).expect("ptr is null!");
251
+ ///
252
+ /// let x_value = unsafe { *ptr.as_ptr() };
253
+ /// assert_eq!(x_value, 0);
254
+ ///
255
+ /// unsafe { *ptr.as_ptr() += 2; }
256
+ /// let x_value = unsafe { *ptr.as_ptr() };
257
+ /// assert_eq!(x_value, 2);
258
+ /// ```
208
259
#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
209
260
#[ rustc_const_stable( feature = "const_nonnull_as_ptr" , since = "1.32.0" ) ]
210
261
#[ inline]
@@ -239,6 +290,18 @@ impl<T: ?Sized> NonNull<T> {
239
290
/// (The part about being initialized is not yet fully decided, but until
240
291
/// it is, the only safe approach is to ensure that they are indeed initialized.)
241
292
///
293
+ /// # Examples
294
+ ///
295
+ /// ```
296
+ /// use std::ptr::NonNull;
297
+ ///
298
+ /// let mut x = 0u32;
299
+ /// let ptr = NonNull::new(&mut x as *mut _).expect("ptr is null!");
300
+ ///
301
+ /// let ref_x = unsafe { ptr.as_ref() };
302
+ /// println!("{}", ref_x);
303
+ /// ```
304
+ ///
242
305
/// [the module documentation]: crate::ptr#safety
243
306
#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
244
307
#[ inline]
@@ -274,6 +337,19 @@ impl<T: ?Sized> NonNull<T> {
274
337
/// This applies even if the result of this method is unused!
275
338
/// (The part about being initialized is not yet fully decided, but until
276
339
/// it is, the only safe approach is to ensure that they are indeed initialized.)
340
+ /// # Examples
341
+ ///
342
+ /// ```
343
+ /// use std::ptr::NonNull;
344
+ ///
345
+ /// let mut x = 0u32;
346
+ /// let mut ptr = NonNull::new(&mut x).expect("null pointer");
347
+ ///
348
+ /// let x_ref = unsafe { ptr.as_mut() };
349
+ /// assert_eq!(*x_ref, 0);
350
+ /// *x_ref += 2;
351
+ /// assert_eq!(*x_ref, 2);
352
+ /// ```
277
353
///
278
354
/// [the module documentation]: crate::ptr#safety
279
355
#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
@@ -285,6 +361,18 @@ impl<T: ?Sized> NonNull<T> {
285
361
}
286
362
287
363
/// Casts to a pointer of another type.
364
+ ///
365
+ /// # Examples
366
+ ///
367
+ /// ```
368
+ /// use std::ptr::NonNull;
369
+ ///
370
+ /// let mut x = 0u32;
371
+ /// let ptr = NonNull::new(&mut x as *mut _).expect("null pointer");
372
+ ///
373
+ /// let casted_ptr = ptr.cast::<i8>();
374
+ /// let raw_ptr: *mut i8 = casted_ptr.as_ptr();
375
+ /// ```
288
376
#[ stable( feature = "nonnull_cast" , since = "1.27.0" ) ]
289
377
#[ rustc_const_stable( feature = "const_nonnull_cast" , since = "1.36.0" ) ]
290
378
#[ inline]
0 commit comments