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