1 use crate::cmp::Ordering;
2 use crate::convert::From;
5 use crate::marker::Unsize;
7 use crate::ops::{CoerceUnsized, DispatchFromDyn};
8 use crate::ptr::Unique;
10 // ignore-tidy-undocumented-unsafe
12 /// `*mut T` but non-zero and covariant.
14 /// This is often the correct thing to use when building data structures using
15 /// raw pointers, but is ultimately more dangerous to use because of its additional
16 /// properties. If you're not sure if you should use `NonNull<T>`, just use `*mut T`!
18 /// Unlike `*mut T`, the pointer must always be non-null, even if the pointer
19 /// is never dereferenced. This is so that enums may use this forbidden value
20 /// as a discriminant -- `Option<NonNull<T>>` has the same size as `*mut T`.
21 /// However the pointer may still dangle if it isn't dereferenced.
23 /// Unlike `*mut T`, `NonNull<T>` is covariant over `T`. If this is incorrect
24 /// for your use case, you should include some [`PhantomData`] in your type to
25 /// provide invariance, such as `PhantomData<Cell<T>>` or `PhantomData<&'a mut T>`.
26 /// Usually this won't be necessary; covariance is correct for most safe abstractions,
27 /// such as `Box`, `Rc`, `Arc`, `Vec`, and `LinkedList`. This is the case because they
28 /// provide a public API that follows the normal shared XOR mutable rules of Rust.
30 /// Notice that `NonNull<T>` has a `From` instance for `&T`. However, this does
31 /// not change the fact that mutating through a (pointer derived from a) shared
32 /// reference is undefined behavior unless the mutation happens inside an
33 /// [`UnsafeCell<T>`]. The same goes for creating a mutable reference from a shared
34 /// reference. When using this `From` instance without an `UnsafeCell<T>`,
35 /// it is your responsibility to ensure that `as_mut` is never called, and `as_ptr`
36 /// is never used for mutation.
38 /// [`PhantomData`]: ../marker/struct.PhantomData.html
39 /// [`UnsafeCell<T>`]: ../cell/struct.UnsafeCell.html
40 #[stable(feature = "nonnull", since = "1.25.0")]
42 #[rustc_layout_scalar_valid_range_start(1)]
43 #[rustc_nonnull_optimization_guaranteed]
44 pub struct NonNull<T: ?Sized> {
48 /// `NonNull` pointers are not `Send` because the data they reference may be aliased.
49 // N.B., this impl is unnecessary, but should provide better error messages.
50 #[stable(feature = "nonnull", since = "1.25.0")]
51 impl<T: ?Sized> !Send for NonNull<T> {}
53 /// `NonNull` pointers are not `Sync` because the data they reference may be aliased.
54 // N.B., this impl is unnecessary, but should provide better error messages.
55 #[stable(feature = "nonnull", since = "1.25.0")]
56 impl<T: ?Sized> !Sync for NonNull<T> {}
58 impl<T: Sized> NonNull<T> {
59 /// Creates a new `NonNull` that is dangling, but well-aligned.
61 /// This is useful for initializing types which lazily allocate, like
64 /// Note that the pointer value may potentially represent a valid pointer to
65 /// a `T`, which means this must not be used as a "not yet initialized"
66 /// sentinel value. Types that lazily allocate must track initialization by
68 #[stable(feature = "nonnull", since = "1.25.0")]
69 #[rustc_const_stable(feature = "const_nonnull_dangling", since = "1.32.0")]
71 pub const fn dangling() -> Self {
73 let ptr = mem::align_of::<T>() as *mut T;
74 NonNull::new_unchecked(ptr)
79 impl<T: ?Sized> NonNull<T> {
80 /// Creates a new `NonNull`.
84 /// `ptr` must be non-null.
85 #[stable(feature = "nonnull", since = "1.25.0")]
86 #[rustc_const_stable(feature = "const_nonnull_new_unchecked", since = "1.32.0")]
88 pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
89 NonNull { pointer: ptr as _ }
92 /// Creates a new `NonNull` if `ptr` is non-null.
93 #[stable(feature = "nonnull", since = "1.25.0")]
95 pub fn new(ptr: *mut T) -> Option<Self> {
96 if !ptr.is_null() { Some(unsafe { Self::new_unchecked(ptr) }) } else { None }
99 /// Acquires the underlying `*mut` pointer.
100 #[stable(feature = "nonnull", since = "1.25.0")]
101 #[rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0")]
103 pub const fn as_ptr(self) -> *mut T {
104 self.pointer as *mut T
107 /// Dereferences the content.
109 /// The resulting lifetime is bound to self so this behaves "as if"
110 /// it were actually an instance of T that is getting borrowed. If a longer
111 /// (unbound) lifetime is needed, use `&*my_ptr.as_ptr()`.
112 #[stable(feature = "nonnull", since = "1.25.0")]
114 pub unsafe fn as_ref(&self) -> &T {
118 /// Mutably dereferences the content.
120 /// The resulting lifetime is bound to self so this behaves "as if"
121 /// it were actually an instance of T that is getting borrowed. If a longer
122 /// (unbound) lifetime is needed, use `&mut *my_ptr.as_ptr()`.
123 #[stable(feature = "nonnull", since = "1.25.0")]
125 pub unsafe fn as_mut(&mut self) -> &mut T {
129 /// Casts to a pointer of another type.
130 #[stable(feature = "nonnull_cast", since = "1.27.0")]
131 #[rustc_const_stable(feature = "const_nonnull_cast", since = "1.32.0")]
133 pub const fn cast<U>(self) -> NonNull<U> {
134 unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) }
138 #[stable(feature = "nonnull", since = "1.25.0")]
139 impl<T: ?Sized> Clone for NonNull<T> {
141 fn clone(&self) -> Self {
146 #[stable(feature = "nonnull", since = "1.25.0")]
147 impl<T: ?Sized> Copy for NonNull<T> {}
149 #[unstable(feature = "coerce_unsized", issue = "27732")]
150 impl<T: ?Sized, U: ?Sized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
152 #[unstable(feature = "dispatch_from_dyn", issue = "none")]
153 impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
155 #[stable(feature = "nonnull", since = "1.25.0")]
156 impl<T: ?Sized> fmt::Debug for NonNull<T> {
157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158 fmt::Pointer::fmt(&self.as_ptr(), f)
162 #[stable(feature = "nonnull", since = "1.25.0")]
163 impl<T: ?Sized> fmt::Pointer for NonNull<T> {
164 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
165 fmt::Pointer::fmt(&self.as_ptr(), f)
169 #[stable(feature = "nonnull", since = "1.25.0")]
170 impl<T: ?Sized> Eq for NonNull<T> {}
172 #[stable(feature = "nonnull", since = "1.25.0")]
173 impl<T: ?Sized> PartialEq for NonNull<T> {
175 fn eq(&self, other: &Self) -> bool {
176 self.as_ptr() == other.as_ptr()
180 #[stable(feature = "nonnull", since = "1.25.0")]
181 impl<T: ?Sized> Ord for NonNull<T> {
183 fn cmp(&self, other: &Self) -> Ordering {
184 self.as_ptr().cmp(&other.as_ptr())
188 #[stable(feature = "nonnull", since = "1.25.0")]
189 impl<T: ?Sized> PartialOrd for NonNull<T> {
191 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
192 self.as_ptr().partial_cmp(&other.as_ptr())
196 #[stable(feature = "nonnull", since = "1.25.0")]
197 impl<T: ?Sized> hash::Hash for NonNull<T> {
199 fn hash<H: hash::Hasher>(&self, state: &mut H) {
200 self.as_ptr().hash(state)
204 #[unstable(feature = "ptr_internals", issue = "none")]
205 impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
207 fn from(unique: Unique<T>) -> Self {
208 unsafe { NonNull::new_unchecked(unique.as_ptr()) }
212 #[stable(feature = "nonnull", since = "1.25.0")]
213 impl<T: ?Sized> From<&mut T> for NonNull<T> {
215 fn from(reference: &mut T) -> Self {
216 unsafe { NonNull { pointer: reference as *mut T } }
220 #[stable(feature = "nonnull", since = "1.25.0")]
221 impl<T: ?Sized> From<&T> for NonNull<T> {
223 fn from(reference: &T) -> Self {
224 unsafe { NonNull { pointer: reference as *const T } }