Skip to content

Commit 1bbe618

Browse files
Add missing examples for NonNull
1 parent aea2e44 commit 1bbe618

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

library/core/src/ptr/non_null.rs

+88
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ impl<T: Sized> NonNull<T> {
7171
/// a `T`, which means this must not be used as a "not yet initialized"
7272
/// sentinel value. Types that lazily allocate must track initialization by
7373
/// 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+
/// ```
7484
#[stable(feature = "nonnull", since = "1.25.0")]
7585
#[rustc_const_stable(feature = "const_nonnull_dangling", since = "1.36.0")]
7686
#[inline]
@@ -155,6 +165,18 @@ impl<T: ?Sized> NonNull<T> {
155165
/// # Safety
156166
///
157167
/// `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+
/// ```
158180
#[stable(feature = "nonnull", since = "1.25.0")]
159181
#[rustc_const_stable(feature = "const_nonnull_new_unchecked", since = "1.25.0")]
160182
#[inline]
@@ -164,6 +186,19 @@ impl<T: ?Sized> NonNull<T> {
164186
}
165187

166188
/// 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+
/// ```
167202
#[stable(feature = "nonnull", since = "1.25.0")]
168203
#[inline]
169204
pub fn new(ptr: *mut T) -> Option<Self> {
@@ -205,6 +240,22 @@ impl<T: ?Sized> NonNull<T> {
205240
}
206241

207242
/// 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+
/// ```
208259
#[stable(feature = "nonnull", since = "1.25.0")]
209260
#[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")]
210261
#[inline]
@@ -239,6 +290,18 @@ impl<T: ?Sized> NonNull<T> {
239290
/// (The part about being initialized is not yet fully decided, but until
240291
/// it is, the only safe approach is to ensure that they are indeed initialized.)
241292
///
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+
///
242305
/// [the module documentation]: crate::ptr#safety
243306
#[stable(feature = "nonnull", since = "1.25.0")]
244307
#[inline]
@@ -274,6 +337,19 @@ impl<T: ?Sized> NonNull<T> {
274337
/// This applies even if the result of this method is unused!
275338
/// (The part about being initialized is not yet fully decided, but until
276339
/// 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+
/// ```
277353
///
278354
/// [the module documentation]: crate::ptr#safety
279355
#[stable(feature = "nonnull", since = "1.25.0")]
@@ -285,6 +361,18 @@ impl<T: ?Sized> NonNull<T> {
285361
}
286362

287363
/// 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+
/// ```
288376
#[stable(feature = "nonnull_cast", since = "1.27.0")]
289377
#[rustc_const_stable(feature = "const_nonnull_cast", since = "1.36.0")]
290378
#[inline]

0 commit comments

Comments
 (0)