]> git.proxmox.com Git - rustc.git/blob - library/core/src/ptr/non_null.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / library / core / src / ptr / non_null.rs
1 use crate::cmp::Ordering;
2 use crate::convert::From;
3 use crate::fmt;
4 use crate::hash;
5 use crate::intrinsics::assert_unsafe_precondition;
6 use crate::marker::Unsize;
7 use crate::mem::{self, MaybeUninit};
8 use crate::num::NonZeroUsize;
9 use crate::ops::{CoerceUnsized, DispatchFromDyn};
10 use crate::ptr::Unique;
11 use crate::slice::{self, SliceIndex};
12
13 /// `*mut T` but non-zero and [covariant].
14 ///
15 /// This is often the correct thing to use when building data structures using
16 /// raw pointers, but is ultimately more dangerous to use because of its additional
17 /// properties. If you're not sure if you should use `NonNull<T>`, just use `*mut T`!
18 ///
19 /// Unlike `*mut T`, the pointer must always be non-null, even if the pointer
20 /// is never dereferenced. This is so that enums may use this forbidden value
21 /// as a discriminant -- `Option<NonNull<T>>` has the same size as `*mut T`.
22 /// However the pointer may still dangle if it isn't dereferenced.
23 ///
24 /// Unlike `*mut T`, `NonNull<T>` was chosen to be covariant over `T`. This makes it
25 /// possible to use `NonNull<T>` when building covariant types, but introduces the
26 /// risk of unsoundness if used in a type that shouldn't actually be covariant.
27 /// (The opposite choice was made for `*mut T` even though technically the unsoundness
28 /// could only be caused by calling unsafe functions.)
29 ///
30 /// Covariance is correct for most safe abstractions, such as `Box`, `Rc`, `Arc`, `Vec`,
31 /// and `LinkedList`. This is the case because they provide a public API that follows the
32 /// normal shared XOR mutable rules of Rust.
33 ///
34 /// If your type cannot safely be covariant, you must ensure it contains some
35 /// additional field to provide invariance. Often this field will be a [`PhantomData`]
36 /// type like `PhantomData<Cell<T>>` or `PhantomData<&'a mut T>`.
37 ///
38 /// Notice that `NonNull<T>` has a `From` instance for `&T`. However, this does
39 /// not change the fact that mutating through a (pointer derived from a) shared
40 /// reference is undefined behavior unless the mutation happens inside an
41 /// [`UnsafeCell<T>`]. The same goes for creating a mutable reference from a shared
42 /// reference. When using this `From` instance without an `UnsafeCell<T>`,
43 /// it is your responsibility to ensure that `as_mut` is never called, and `as_ptr`
44 /// is never used for mutation.
45 ///
46 /// # Representation
47 ///
48 /// Thanks to the [null pointer optimization],
49 /// `NonNull<T>` and `Option<NonNull<T>>`
50 /// are guaranteed to have the same size and alignment:
51 ///
52 /// ```
53 /// # use std::mem::{size_of, align_of};
54 /// use std::ptr::NonNull;
55 ///
56 /// assert_eq!(size_of::<NonNull<i16>>(), size_of::<Option<NonNull<i16>>>());
57 /// assert_eq!(align_of::<NonNull<i16>>(), align_of::<Option<NonNull<i16>>>());
58 ///
59 /// assert_eq!(size_of::<NonNull<str>>(), size_of::<Option<NonNull<str>>>());
60 /// assert_eq!(align_of::<NonNull<str>>(), align_of::<Option<NonNull<str>>>());
61 /// ```
62 ///
63 /// [covariant]: https://doc.rust-lang.org/reference/subtyping.html
64 /// [`PhantomData`]: crate::marker::PhantomData
65 /// [`UnsafeCell<T>`]: crate::cell::UnsafeCell
66 /// [null pointer optimization]: crate::option#representation
67 #[stable(feature = "nonnull", since = "1.25.0")]
68 #[repr(transparent)]
69 #[rustc_layout_scalar_valid_range_start(1)]
70 #[rustc_nonnull_optimization_guaranteed]
71 pub struct NonNull<T: ?Sized> {
72 pointer: *const T,
73 }
74
75 /// `NonNull` pointers are not `Send` because the data they reference may be aliased.
76 // N.B., this impl is unnecessary, but should provide better error messages.
77 #[stable(feature = "nonnull", since = "1.25.0")]
78 impl<T: ?Sized> !Send for NonNull<T> {}
79
80 /// `NonNull` pointers are not `Sync` because the data they reference may be aliased.
81 // N.B., this impl is unnecessary, but should provide better error messages.
82 #[stable(feature = "nonnull", since = "1.25.0")]
83 impl<T: ?Sized> !Sync for NonNull<T> {}
84
85 impl<T: Sized> NonNull<T> {
86 /// Creates a new `NonNull` that is dangling, but well-aligned.
87 ///
88 /// This is useful for initializing types which lazily allocate, like
89 /// `Vec::new` does.
90 ///
91 /// Note that the pointer value may potentially represent a valid pointer to
92 /// a `T`, which means this must not be used as a "not yet initialized"
93 /// sentinel value. Types that lazily allocate must track initialization by
94 /// some other means.
95 ///
96 /// # Examples
97 ///
98 /// ```
99 /// use std::ptr::NonNull;
100 ///
101 /// let ptr = NonNull::<u32>::dangling();
102 /// // Important: don't try to access the value of `ptr` without
103 /// // initializing it first! The pointer is not null but isn't valid either!
104 /// ```
105 #[stable(feature = "nonnull", since = "1.25.0")]
106 #[rustc_const_stable(feature = "const_nonnull_dangling", since = "1.36.0")]
107 #[must_use]
108 #[inline]
109 pub const fn dangling() -> Self {
110 // SAFETY: mem::align_of() returns a non-zero usize which is then casted
111 // to a *mut T. Therefore, `ptr` is not null and the conditions for
112 // calling new_unchecked() are respected.
113 unsafe {
114 let ptr = crate::ptr::invalid_mut::<T>(mem::align_of::<T>());
115 NonNull::new_unchecked(ptr)
116 }
117 }
118
119 /// Returns a shared references to the value. In contrast to [`as_ref`], this does not require
120 /// that the value has to be initialized.
121 ///
122 /// For the mutable counterpart see [`as_uninit_mut`].
123 ///
124 /// [`as_ref`]: NonNull::as_ref
125 /// [`as_uninit_mut`]: NonNull::as_uninit_mut
126 ///
127 /// # Safety
128 ///
129 /// When calling this method, you have to ensure that all of the following is true:
130 ///
131 /// * The pointer must be properly aligned.
132 ///
133 /// * It must be "dereferenceable" in the sense defined in [the module documentation].
134 ///
135 /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
136 /// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
137 /// In particular, while this reference exists, the memory the pointer points to must
138 /// not get mutated (except inside `UnsafeCell`).
139 ///
140 /// This applies even if the result of this method is unused!
141 ///
142 /// [the module documentation]: crate::ptr#safety
143 #[inline]
144 #[must_use]
145 #[unstable(feature = "ptr_as_uninit", issue = "75402")]
146 #[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
147 pub const unsafe fn as_uninit_ref<'a>(self) -> &'a MaybeUninit<T> {
148 // SAFETY: the caller must guarantee that `self` meets all the
149 // requirements for a reference.
150 unsafe { &*self.cast().as_ptr() }
151 }
152
153 /// Returns a unique references to the value. In contrast to [`as_mut`], this does not require
154 /// that the value has to be initialized.
155 ///
156 /// For the shared counterpart see [`as_uninit_ref`].
157 ///
158 /// [`as_mut`]: NonNull::as_mut
159 /// [`as_uninit_ref`]: NonNull::as_uninit_ref
160 ///
161 /// # Safety
162 ///
163 /// When calling this method, you have to ensure that all of the following is true:
164 ///
165 /// * The pointer must be properly aligned.
166 ///
167 /// * It must be "dereferenceable" in the sense defined in [the module documentation].
168 ///
169 /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
170 /// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
171 /// In particular, while this reference exists, the memory the pointer points to must
172 /// not get accessed (read or written) through any other pointer.
173 ///
174 /// This applies even if the result of this method is unused!
175 ///
176 /// [the module documentation]: crate::ptr#safety
177 #[inline]
178 #[must_use]
179 #[unstable(feature = "ptr_as_uninit", issue = "75402")]
180 #[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
181 pub const unsafe fn as_uninit_mut<'a>(self) -> &'a mut MaybeUninit<T> {
182 // SAFETY: the caller must guarantee that `self` meets all the
183 // requirements for a reference.
184 unsafe { &mut *self.cast().as_ptr() }
185 }
186 }
187
188 impl<T: ?Sized> NonNull<T> {
189 /// Creates a new `NonNull`.
190 ///
191 /// # Safety
192 ///
193 /// `ptr` must be non-null.
194 ///
195 /// # Examples
196 ///
197 /// ```
198 /// use std::ptr::NonNull;
199 ///
200 /// let mut x = 0u32;
201 /// let ptr = unsafe { NonNull::new_unchecked(&mut x as *mut _) };
202 /// ```
203 ///
204 /// *Incorrect* usage of this function:
205 ///
206 /// ```rust,no_run
207 /// use std::ptr::NonNull;
208 ///
209 /// // NEVER DO THAT!!! This is undefined behavior. ⚠️
210 /// let ptr = unsafe { NonNull::<u32>::new_unchecked(std::ptr::null_mut()) };
211 /// ```
212 #[stable(feature = "nonnull", since = "1.25.0")]
213 #[rustc_const_stable(feature = "const_nonnull_new_unchecked", since = "1.25.0")]
214 #[inline]
215 pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
216 // SAFETY: the caller must guarantee that `ptr` is non-null.
217 unsafe {
218 assert_unsafe_precondition!("NonNull::new_unchecked requires that the pointer is non-null", [T: ?Sized](ptr: *mut T) => !ptr.is_null());
219 NonNull { pointer: ptr as _ }
220 }
221 }
222
223 /// Creates a new `NonNull` if `ptr` is non-null.
224 ///
225 /// # Examples
226 ///
227 /// ```
228 /// use std::ptr::NonNull;
229 ///
230 /// let mut x = 0u32;
231 /// let ptr = NonNull::<u32>::new(&mut x as *mut _).expect("ptr is null!");
232 ///
233 /// if let Some(ptr) = NonNull::<u32>::new(std::ptr::null_mut()) {
234 /// unreachable!();
235 /// }
236 /// ```
237 #[stable(feature = "nonnull", since = "1.25.0")]
238 #[rustc_const_unstable(feature = "const_nonnull_new", issue = "93235")]
239 #[inline]
240 pub const fn new(ptr: *mut T) -> Option<Self> {
241 if !ptr.is_null() {
242 // SAFETY: The pointer is already checked and is not null
243 Some(unsafe { Self::new_unchecked(ptr) })
244 } else {
245 None
246 }
247 }
248
249 /// Performs the same functionality as [`std::ptr::from_raw_parts`], except that a
250 /// `NonNull` pointer is returned, as opposed to a raw `*const` pointer.
251 ///
252 /// See the documentation of [`std::ptr::from_raw_parts`] for more details.
253 ///
254 /// [`std::ptr::from_raw_parts`]: crate::ptr::from_raw_parts
255 #[unstable(feature = "ptr_metadata", issue = "81513")]
256 #[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
257 #[inline]
258 pub const fn from_raw_parts(
259 data_address: NonNull<()>,
260 metadata: <T as super::Pointee>::Metadata,
261 ) -> NonNull<T> {
262 // SAFETY: The result of `ptr::from::raw_parts_mut` is non-null because `data_address` is.
263 unsafe {
264 NonNull::new_unchecked(super::from_raw_parts_mut(data_address.as_ptr(), metadata))
265 }
266 }
267
268 /// Decompose a (possibly wide) pointer into its address and metadata components.
269 ///
270 /// The pointer can be later reconstructed with [`NonNull::from_raw_parts`].
271 #[unstable(feature = "ptr_metadata", issue = "81513")]
272 #[rustc_const_unstable(feature = "ptr_metadata", issue = "81513")]
273 #[must_use = "this returns the result of the operation, \
274 without modifying the original"]
275 #[inline]
276 pub const fn to_raw_parts(self) -> (NonNull<()>, <T as super::Pointee>::Metadata) {
277 (self.cast(), super::metadata(self.as_ptr()))
278 }
279
280 /// Gets the "address" portion of the pointer.
281 ///
282 /// For more details see the equivalent method on a raw pointer, [`pointer::addr`].
283 ///
284 /// This API and its claimed semantics are part of the Strict Provenance experiment,
285 /// see the [`ptr` module documentation][crate::ptr].
286 #[must_use]
287 #[inline]
288 #[unstable(feature = "strict_provenance", issue = "95228")]
289 pub fn addr(self) -> NonZeroUsize {
290 // SAFETY: The pointer is guaranteed by the type to be non-null,
291 // meaning that the address will be non-zero.
292 unsafe { NonZeroUsize::new_unchecked(self.pointer.addr()) }
293 }
294
295 /// Creates a new pointer with the given address.
296 ///
297 /// For more details see the equivalent method on a raw pointer, [`pointer::with_addr`].
298 ///
299 /// This API and its claimed semantics are part of the Strict Provenance experiment,
300 /// see the [`ptr` module documentation][crate::ptr].
301 #[must_use]
302 #[inline]
303 #[unstable(feature = "strict_provenance", issue = "95228")]
304 pub fn with_addr(self, addr: NonZeroUsize) -> Self {
305 // SAFETY: The result of `ptr::from::with_addr` is non-null because `addr` is guaranteed to be non-zero.
306 unsafe { NonNull::new_unchecked(self.pointer.with_addr(addr.get()) as *mut _) }
307 }
308
309 /// Creates a new pointer by mapping `self`'s address to a new one.
310 ///
311 /// For more details see the equivalent method on a raw pointer, [`pointer::map_addr`].
312 ///
313 /// This API and its claimed semantics are part of the Strict Provenance experiment,
314 /// see the [`ptr` module documentation][crate::ptr].
315 #[must_use]
316 #[inline]
317 #[unstable(feature = "strict_provenance", issue = "95228")]
318 pub fn map_addr(self, f: impl FnOnce(NonZeroUsize) -> NonZeroUsize) -> Self {
319 self.with_addr(f(self.addr()))
320 }
321
322 /// Acquires the underlying `*mut` pointer.
323 ///
324 /// # Examples
325 ///
326 /// ```
327 /// use std::ptr::NonNull;
328 ///
329 /// let mut x = 0u32;
330 /// let ptr = NonNull::new(&mut x).expect("ptr is null!");
331 ///
332 /// let x_value = unsafe { *ptr.as_ptr() };
333 /// assert_eq!(x_value, 0);
334 ///
335 /// unsafe { *ptr.as_ptr() += 2; }
336 /// let x_value = unsafe { *ptr.as_ptr() };
337 /// assert_eq!(x_value, 2);
338 /// ```
339 #[stable(feature = "nonnull", since = "1.25.0")]
340 #[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")]
341 #[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
342 #[must_use]
343 #[inline(always)]
344 pub const fn as_ptr(self) -> *mut T {
345 self.pointer as *mut T
346 }
347
348 /// Returns a shared reference to the value. If the value may be uninitialized, [`as_uninit_ref`]
349 /// must be used instead.
350 ///
351 /// For the mutable counterpart see [`as_mut`].
352 ///
353 /// [`as_uninit_ref`]: NonNull::as_uninit_ref
354 /// [`as_mut`]: NonNull::as_mut
355 ///
356 /// # Safety
357 ///
358 /// When calling this method, you have to ensure that all of the following is true:
359 ///
360 /// * The pointer must be properly aligned.
361 ///
362 /// * It must be "dereferenceable" in the sense defined in [the module documentation].
363 ///
364 /// * The pointer must point to an initialized instance of `T`.
365 ///
366 /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
367 /// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
368 /// In particular, while this reference exists, the memory the pointer points to must
369 /// not get mutated (except inside `UnsafeCell`).
370 ///
371 /// This applies even if the result of this method is unused!
372 /// (The part about being initialized is not yet fully decided, but until
373 /// it is, the only safe approach is to ensure that they are indeed initialized.)
374 ///
375 /// # Examples
376 ///
377 /// ```
378 /// use std::ptr::NonNull;
379 ///
380 /// let mut x = 0u32;
381 /// let ptr = NonNull::new(&mut x as *mut _).expect("ptr is null!");
382 ///
383 /// let ref_x = unsafe { ptr.as_ref() };
384 /// println!("{ref_x}");
385 /// ```
386 ///
387 /// [the module documentation]: crate::ptr#safety
388 #[stable(feature = "nonnull", since = "1.25.0")]
389 #[rustc_const_stable(feature = "const_nonnull_as_ref", since = "1.73.0")]
390 #[must_use]
391 #[inline(always)]
392 pub const unsafe fn as_ref<'a>(&self) -> &'a T {
393 // SAFETY: the caller must guarantee that `self` meets all the
394 // requirements for a reference.
395 // `cast_const` avoids a mutable raw pointer deref.
396 unsafe { &*self.as_ptr().cast_const() }
397 }
398
399 /// Returns a unique reference to the value. If the value may be uninitialized, [`as_uninit_mut`]
400 /// must be used instead.
401 ///
402 /// For the shared counterpart see [`as_ref`].
403 ///
404 /// [`as_uninit_mut`]: NonNull::as_uninit_mut
405 /// [`as_ref`]: NonNull::as_ref
406 ///
407 /// # Safety
408 ///
409 /// When calling this method, you have to ensure that all of the following is true:
410 ///
411 /// * The pointer must be properly aligned.
412 ///
413 /// * It must be "dereferenceable" in the sense defined in [the module documentation].
414 ///
415 /// * The pointer must point to an initialized instance of `T`.
416 ///
417 /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
418 /// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
419 /// In particular, while this reference exists, the memory the pointer points to must
420 /// not get accessed (read or written) through any other pointer.
421 ///
422 /// This applies even if the result of this method is unused!
423 /// (The part about being initialized is not yet fully decided, but until
424 /// it is, the only safe approach is to ensure that they are indeed initialized.)
425 /// # Examples
426 ///
427 /// ```
428 /// use std::ptr::NonNull;
429 ///
430 /// let mut x = 0u32;
431 /// let mut ptr = NonNull::new(&mut x).expect("null pointer");
432 ///
433 /// let x_ref = unsafe { ptr.as_mut() };
434 /// assert_eq!(*x_ref, 0);
435 /// *x_ref += 2;
436 /// assert_eq!(*x_ref, 2);
437 /// ```
438 ///
439 /// [the module documentation]: crate::ptr#safety
440 #[stable(feature = "nonnull", since = "1.25.0")]
441 #[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
442 #[must_use]
443 #[inline(always)]
444 pub const unsafe fn as_mut<'a>(&mut self) -> &'a mut T {
445 // SAFETY: the caller must guarantee that `self` meets all the
446 // requirements for a mutable reference.
447 unsafe { &mut *self.as_ptr() }
448 }
449
450 /// Casts to a pointer of another type.
451 ///
452 /// # Examples
453 ///
454 /// ```
455 /// use std::ptr::NonNull;
456 ///
457 /// let mut x = 0u32;
458 /// let ptr = NonNull::new(&mut x as *mut _).expect("null pointer");
459 ///
460 /// let casted_ptr = ptr.cast::<i8>();
461 /// let raw_ptr: *mut i8 = casted_ptr.as_ptr();
462 /// ```
463 #[stable(feature = "nonnull_cast", since = "1.27.0")]
464 #[rustc_const_stable(feature = "const_nonnull_cast", since = "1.36.0")]
465 #[must_use = "this returns the result of the operation, \
466 without modifying the original"]
467 #[inline]
468 pub const fn cast<U>(self) -> NonNull<U> {
469 // SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
470 unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) }
471 }
472
473 /// See [`pointer::add`] for semantics and safety requirements.
474 #[inline]
475 pub(crate) const unsafe fn add(self, delta: usize) -> Self
476 where
477 T: Sized,
478 {
479 // SAFETY: We require that the delta stays in-bounds of the object, and
480 // thus it cannot become null, as that would require wrapping the
481 // address space, which no legal objects are allowed to do.
482 // And the caller promised the `delta` is sound to add.
483 unsafe { NonNull { pointer: self.pointer.add(delta) } }
484 }
485
486 /// See [`pointer::sub`] for semantics and safety requirements.
487 #[inline]
488 pub(crate) const unsafe fn sub(self, delta: usize) -> Self
489 where
490 T: Sized,
491 {
492 // SAFETY: We require that the delta stays in-bounds of the object, and
493 // thus it cannot become null, as no legal objects can be allocated
494 // in such as way that the null address is part of them.
495 // And the caller promised the `delta` is sound to subtract.
496 unsafe { NonNull { pointer: self.pointer.sub(delta) } }
497 }
498
499 /// See [`pointer::sub_ptr`] for semantics and safety requirements.
500 #[inline]
501 pub(crate) const unsafe fn sub_ptr(self, subtrahend: Self) -> usize
502 where
503 T: Sized,
504 {
505 // SAFETY: The caller promised that this is safe to do, and
506 // the non-nullness is irrelevant to the operation.
507 unsafe { self.pointer.sub_ptr(subtrahend.pointer) }
508 }
509 }
510
511 impl<T> NonNull<[T]> {
512 /// Creates a non-null raw slice from a thin pointer and a length.
513 ///
514 /// The `len` argument is the number of **elements**, not the number of bytes.
515 ///
516 /// This function is safe, but dereferencing the return value is unsafe.
517 /// See the documentation of [`slice::from_raw_parts`] for slice safety requirements.
518 ///
519 /// # Examples
520 ///
521 /// ```rust
522 /// use std::ptr::NonNull;
523 ///
524 /// // create a slice pointer when starting out with a pointer to the first element
525 /// let mut x = [5, 6, 7];
526 /// let nonnull_pointer = NonNull::new(x.as_mut_ptr()).unwrap();
527 /// let slice = NonNull::slice_from_raw_parts(nonnull_pointer, 3);
528 /// assert_eq!(unsafe { slice.as_ref()[2] }, 7);
529 /// ```
530 ///
531 /// (Note that this example artificially demonstrates a use of this method,
532 /// but `let slice = NonNull::from(&x[..]);` would be a better way to write code like this.)
533 #[stable(feature = "nonnull_slice_from_raw_parts", since = "1.70.0")]
534 #[rustc_const_unstable(feature = "const_slice_from_raw_parts_mut", issue = "67456")]
535 #[must_use]
536 #[inline]
537 pub const fn slice_from_raw_parts(data: NonNull<T>, len: usize) -> Self {
538 // SAFETY: `data` is a `NonNull` pointer which is necessarily non-null
539 unsafe { Self::new_unchecked(super::slice_from_raw_parts_mut(data.as_ptr(), len)) }
540 }
541
542 /// Returns the length of a non-null raw slice.
543 ///
544 /// The returned value is the number of **elements**, not the number of bytes.
545 ///
546 /// This function is safe, even when the non-null raw slice cannot be dereferenced to a slice
547 /// because the pointer does not have a valid address.
548 ///
549 /// # Examples
550 ///
551 /// ```rust
552 /// use std::ptr::NonNull;
553 ///
554 /// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3);
555 /// assert_eq!(slice.len(), 3);
556 /// ```
557 #[stable(feature = "slice_ptr_len_nonnull", since = "1.63.0")]
558 #[rustc_const_stable(feature = "const_slice_ptr_len_nonnull", since = "1.63.0")]
559 #[rustc_allow_const_fn_unstable(const_slice_ptr_len)]
560 #[must_use]
561 #[inline]
562 pub const fn len(self) -> usize {
563 self.as_ptr().len()
564 }
565
566 /// Returns a non-null pointer to the slice's buffer.
567 ///
568 /// # Examples
569 ///
570 /// ```rust
571 /// #![feature(slice_ptr_get)]
572 /// use std::ptr::NonNull;
573 ///
574 /// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3);
575 /// assert_eq!(slice.as_non_null_ptr(), NonNull::<i8>::dangling());
576 /// ```
577 #[inline]
578 #[must_use]
579 #[unstable(feature = "slice_ptr_get", issue = "74265")]
580 #[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")]
581 pub const fn as_non_null_ptr(self) -> NonNull<T> {
582 // SAFETY: We know `self` is non-null.
583 unsafe { NonNull::new_unchecked(self.as_ptr().as_mut_ptr()) }
584 }
585
586 /// Returns a raw pointer to the slice's buffer.
587 ///
588 /// # Examples
589 ///
590 /// ```rust
591 /// #![feature(slice_ptr_get)]
592 /// use std::ptr::NonNull;
593 ///
594 /// let slice: NonNull<[i8]> = NonNull::slice_from_raw_parts(NonNull::dangling(), 3);
595 /// assert_eq!(slice.as_mut_ptr(), NonNull::<i8>::dangling().as_ptr());
596 /// ```
597 #[inline]
598 #[must_use]
599 #[unstable(feature = "slice_ptr_get", issue = "74265")]
600 #[rustc_const_unstable(feature = "slice_ptr_get", issue = "74265")]
601 #[cfg_attr(not(bootstrap), rustc_never_returns_null_ptr)]
602 pub const fn as_mut_ptr(self) -> *mut T {
603 self.as_non_null_ptr().as_ptr()
604 }
605
606 /// Returns a shared reference to a slice of possibly uninitialized values. In contrast to
607 /// [`as_ref`], this does not require that the value has to be initialized.
608 ///
609 /// For the mutable counterpart see [`as_uninit_slice_mut`].
610 ///
611 /// [`as_ref`]: NonNull::as_ref
612 /// [`as_uninit_slice_mut`]: NonNull::as_uninit_slice_mut
613 ///
614 /// # Safety
615 ///
616 /// When calling this method, you have to ensure that all of the following is true:
617 ///
618 /// * The pointer must be [valid] for reads for `ptr.len() * mem::size_of::<T>()` many bytes,
619 /// and it must be properly aligned. This means in particular:
620 ///
621 /// * The entire memory range of this slice must be contained within a single allocated object!
622 /// Slices can never span across multiple allocated objects.
623 ///
624 /// * The pointer must be aligned even for zero-length slices. One
625 /// reason for this is that enum layout optimizations may rely on references
626 /// (including slices of any length) being aligned and non-null to distinguish
627 /// them from other data. You can obtain a pointer that is usable as `data`
628 /// for zero-length slices using [`NonNull::dangling()`].
629 ///
630 /// * The total size `ptr.len() * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`.
631 /// See the safety documentation of [`pointer::offset`].
632 ///
633 /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
634 /// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
635 /// In particular, while this reference exists, the memory the pointer points to must
636 /// not get mutated (except inside `UnsafeCell`).
637 ///
638 /// This applies even if the result of this method is unused!
639 ///
640 /// See also [`slice::from_raw_parts`].
641 ///
642 /// [valid]: crate::ptr#safety
643 #[inline]
644 #[must_use]
645 #[unstable(feature = "ptr_as_uninit", issue = "75402")]
646 #[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
647 pub const unsafe fn as_uninit_slice<'a>(self) -> &'a [MaybeUninit<T>] {
648 // SAFETY: the caller must uphold the safety contract for `as_uninit_slice`.
649 unsafe { slice::from_raw_parts(self.cast().as_ptr(), self.len()) }
650 }
651
652 /// Returns a unique reference to a slice of possibly uninitialized values. In contrast to
653 /// [`as_mut`], this does not require that the value has to be initialized.
654 ///
655 /// For the shared counterpart see [`as_uninit_slice`].
656 ///
657 /// [`as_mut`]: NonNull::as_mut
658 /// [`as_uninit_slice`]: NonNull::as_uninit_slice
659 ///
660 /// # Safety
661 ///
662 /// When calling this method, you have to ensure that all of the following is true:
663 ///
664 /// * The pointer must be [valid] for reads and writes for `ptr.len() * mem::size_of::<T>()`
665 /// many bytes, and it must be properly aligned. This means in particular:
666 ///
667 /// * The entire memory range of this slice must be contained within a single allocated object!
668 /// Slices can never span across multiple allocated objects.
669 ///
670 /// * The pointer must be aligned even for zero-length slices. One
671 /// reason for this is that enum layout optimizations may rely on references
672 /// (including slices of any length) being aligned and non-null to distinguish
673 /// them from other data. You can obtain a pointer that is usable as `data`
674 /// for zero-length slices using [`NonNull::dangling()`].
675 ///
676 /// * The total size `ptr.len() * mem::size_of::<T>()` of the slice must be no larger than `isize::MAX`.
677 /// See the safety documentation of [`pointer::offset`].
678 ///
679 /// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
680 /// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
681 /// In particular, while this reference exists, the memory the pointer points to must
682 /// not get accessed (read or written) through any other pointer.
683 ///
684 /// This applies even if the result of this method is unused!
685 ///
686 /// See also [`slice::from_raw_parts_mut`].
687 ///
688 /// [valid]: crate::ptr#safety
689 ///
690 /// # Examples
691 ///
692 /// ```rust
693 /// #![feature(allocator_api, ptr_as_uninit)]
694 ///
695 /// use std::alloc::{Allocator, Layout, Global};
696 /// use std::mem::MaybeUninit;
697 /// use std::ptr::NonNull;
698 ///
699 /// let memory: NonNull<[u8]> = Global.allocate(Layout::new::<[u8; 32]>())?;
700 /// // This is safe as `memory` is valid for reads and writes for `memory.len()` many bytes.
701 /// // Note that calling `memory.as_mut()` is not allowed here as the content may be uninitialized.
702 /// # #[allow(unused_variables)]
703 /// let slice: &mut [MaybeUninit<u8>] = unsafe { memory.as_uninit_slice_mut() };
704 /// # Ok::<_, std::alloc::AllocError>(())
705 /// ```
706 #[inline]
707 #[must_use]
708 #[unstable(feature = "ptr_as_uninit", issue = "75402")]
709 #[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
710 pub const unsafe fn as_uninit_slice_mut<'a>(self) -> &'a mut [MaybeUninit<T>] {
711 // SAFETY: the caller must uphold the safety contract for `as_uninit_slice_mut`.
712 unsafe { slice::from_raw_parts_mut(self.cast().as_ptr(), self.len()) }
713 }
714
715 /// Returns a raw pointer to an element or subslice, without doing bounds
716 /// checking.
717 ///
718 /// Calling this method with an out-of-bounds index or when `self` is not dereferenceable
719 /// is *[undefined behavior]* even if the resulting pointer is not used.
720 ///
721 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
722 ///
723 /// # Examples
724 ///
725 /// ```
726 /// #![feature(slice_ptr_get)]
727 /// use std::ptr::NonNull;
728 ///
729 /// let x = &mut [1, 2, 4];
730 /// let x = NonNull::slice_from_raw_parts(NonNull::new(x.as_mut_ptr()).unwrap(), x.len());
731 ///
732 /// unsafe {
733 /// assert_eq!(x.get_unchecked_mut(1).as_ptr(), x.as_non_null_ptr().as_ptr().add(1));
734 /// }
735 /// ```
736 #[unstable(feature = "slice_ptr_get", issue = "74265")]
737 #[inline]
738 pub unsafe fn get_unchecked_mut<I>(self, index: I) -> NonNull<I::Output>
739 where
740 I: SliceIndex<[T]>,
741 {
742 // SAFETY: the caller ensures that `self` is dereferenceable and `index` in-bounds.
743 // As a consequence, the resulting pointer cannot be null.
744 unsafe { NonNull::new_unchecked(self.as_ptr().get_unchecked_mut(index)) }
745 }
746 }
747
748 #[stable(feature = "nonnull", since = "1.25.0")]
749 impl<T: ?Sized> Clone for NonNull<T> {
750 #[inline(always)]
751 fn clone(&self) -> Self {
752 *self
753 }
754 }
755
756 #[stable(feature = "nonnull", since = "1.25.0")]
757 impl<T: ?Sized> Copy for NonNull<T> {}
758
759 #[unstable(feature = "coerce_unsized", issue = "18598")]
760 impl<T: ?Sized, U: ?Sized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
761
762 #[unstable(feature = "dispatch_from_dyn", issue = "none")]
763 impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
764
765 #[stable(feature = "nonnull", since = "1.25.0")]
766 impl<T: ?Sized> fmt::Debug for NonNull<T> {
767 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
768 fmt::Pointer::fmt(&self.as_ptr(), f)
769 }
770 }
771
772 #[stable(feature = "nonnull", since = "1.25.0")]
773 impl<T: ?Sized> fmt::Pointer for NonNull<T> {
774 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
775 fmt::Pointer::fmt(&self.as_ptr(), f)
776 }
777 }
778
779 #[stable(feature = "nonnull", since = "1.25.0")]
780 impl<T: ?Sized> Eq for NonNull<T> {}
781
782 #[stable(feature = "nonnull", since = "1.25.0")]
783 impl<T: ?Sized> PartialEq for NonNull<T> {
784 #[inline]
785 fn eq(&self, other: &Self) -> bool {
786 self.as_ptr() == other.as_ptr()
787 }
788 }
789
790 #[stable(feature = "nonnull", since = "1.25.0")]
791 impl<T: ?Sized> Ord for NonNull<T> {
792 #[inline]
793 fn cmp(&self, other: &Self) -> Ordering {
794 self.as_ptr().cmp(&other.as_ptr())
795 }
796 }
797
798 #[stable(feature = "nonnull", since = "1.25.0")]
799 impl<T: ?Sized> PartialOrd for NonNull<T> {
800 #[inline]
801 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
802 self.as_ptr().partial_cmp(&other.as_ptr())
803 }
804 }
805
806 #[stable(feature = "nonnull", since = "1.25.0")]
807 impl<T: ?Sized> hash::Hash for NonNull<T> {
808 #[inline]
809 fn hash<H: hash::Hasher>(&self, state: &mut H) {
810 self.as_ptr().hash(state)
811 }
812 }
813
814 #[unstable(feature = "ptr_internals", issue = "none")]
815 impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
816 #[inline]
817 fn from(unique: Unique<T>) -> Self {
818 // SAFETY: A Unique pointer cannot be null, so the conditions for
819 // new_unchecked() are respected.
820 unsafe { NonNull::new_unchecked(unique.as_ptr()) }
821 }
822 }
823
824 #[stable(feature = "nonnull", since = "1.25.0")]
825 impl<T: ?Sized> From<&mut T> for NonNull<T> {
826 /// Converts a `&mut T` to a `NonNull<T>`.
827 ///
828 /// This conversion is safe and infallible since references cannot be null.
829 #[inline]
830 fn from(reference: &mut T) -> Self {
831 // SAFETY: A mutable reference cannot be null.
832 unsafe { NonNull { pointer: reference as *mut T } }
833 }
834 }
835
836 #[stable(feature = "nonnull", since = "1.25.0")]
837 impl<T: ?Sized> From<&T> for NonNull<T> {
838 /// Converts a `&T` to a `NonNull<T>`.
839 ///
840 /// This conversion is safe and infallible since references cannot be null.
841 #[inline]
842 fn from(reference: &T) -> Self {
843 // SAFETY: A reference cannot be null, so the conditions for
844 // new_unchecked() are respected.
845 unsafe { NonNull { pointer: reference as *const T } }
846 }
847 }