1 use crate::convert::From;
3 use crate::marker::{PhantomData, Unsize};
5 use crate::ops::{CoerceUnsized, DispatchFromDyn};
6 use crate::ptr::NonNull;
8 // ignore-tidy-undocumented-unsafe
10 /// A wrapper around a raw non-null `*mut T` that indicates that the possessor
11 /// of this wrapper owns the referent. Useful for building abstractions like
12 /// `Box<T>`, `Vec<T>`, `String`, and `HashMap<K, V>`.
14 /// Unlike `*mut T`, `Unique<T>` behaves "as if" it were an instance of `T`.
15 /// It implements `Send`/`Sync` if `T` is `Send`/`Sync`. It also implies
16 /// the kind of strong aliasing guarantees an instance of `T` can expect:
17 /// the referent of the pointer should not be modified without a unique path to
18 /// its owning Unique.
20 /// If you're uncertain of whether it's correct to use `Unique` for your purposes,
21 /// consider using `NonNull`, which has weaker semantics.
23 /// Unlike `*mut T`, the pointer must always be non-null, even if the pointer
24 /// is never dereferenced. This is so that enums may use this forbidden value
25 /// as a discriminant -- `Option<Unique<T>>` has the same size as `Unique<T>`.
26 /// However the pointer may still dangle if it isn't dereferenced.
28 /// Unlike `*mut T`, `Unique<T>` is covariant over `T`. This should always be correct
29 /// for any type which upholds Unique's aliasing requirements.
31 feature = "ptr_internals",
33 reason = "use `NonNull` instead and consider `PhantomData<T>` \
34 (if you also use `#[may_dangle]`), `Send`, and/or `Sync`"
38 #[rustc_layout_scalar_valid_range_start(1)]
39 pub struct Unique<T: ?Sized> {
41 // NOTE: this marker has no consequences for variance, but is necessary
42 // for dropck to understand that we logically own a `T`.
45 // https://github.com/rust-lang/rfcs/blob/master/text/0769-sound-generic-drop.md#phantom-data
46 _marker: PhantomData<T>,
49 /// `Unique` pointers are `Send` if `T` is `Send` because the data they
50 /// reference is unaliased. Note that this aliasing invariant is
51 /// unenforced by the type system; the abstraction using the
52 /// `Unique` must enforce it.
53 #[unstable(feature = "ptr_internals", issue = "none")]
54 unsafe impl<T: Send + ?Sized> Send for Unique<T> {}
56 /// `Unique` pointers are `Sync` if `T` is `Sync` because the data they
57 /// reference is unaliased. Note that this aliasing invariant is
58 /// unenforced by the type system; the abstraction using the
59 /// `Unique` must enforce it.
60 #[unstable(feature = "ptr_internals", issue = "none")]
61 unsafe impl<T: Sync + ?Sized> Sync for Unique<T> {}
63 #[unstable(feature = "ptr_internals", issue = "none")]
64 impl<T: Sized> Unique<T> {
65 /// Creates a new `Unique` that is dangling, but well-aligned.
67 /// This is useful for initializing types which lazily allocate, like
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
74 // FIXME: rename to dangling() to match NonNull?
76 pub const fn empty() -> Self {
77 unsafe { Unique::new_unchecked(mem::align_of::<T>() as *mut T) }
81 #[unstable(feature = "ptr_internals", issue = "none")]
82 impl<T: ?Sized> Unique<T> {
83 /// Creates a new `Unique`.
87 /// `ptr` must be non-null.
89 pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
90 Unique { pointer: ptr as _, _marker: PhantomData }
93 /// Creates a new `Unique` if `ptr` is non-null.
95 pub fn new(ptr: *mut T) -> Option<Self> {
97 Some(unsafe { Unique { pointer: ptr as _, _marker: PhantomData } })
103 /// Acquires the underlying `*mut` pointer.
105 pub const fn as_ptr(self) -> *mut T {
106 self.pointer as *mut T
109 /// Dereferences the content.
111 /// The resulting lifetime is bound to self so this behaves "as if"
112 /// it were actually an instance of T that is getting borrowed. If a longer
113 /// (unbound) lifetime is needed, use `&*my_ptr.as_ptr()`.
115 pub unsafe fn as_ref(&self) -> &T {
119 /// Mutably dereferences the content.
121 /// The resulting lifetime is bound to self so this behaves "as if"
122 /// it were actually an instance of T that is getting borrowed. If a longer
123 /// (unbound) lifetime is needed, use `&mut *my_ptr.as_ptr()`.
125 pub unsafe fn as_mut(&mut self) -> &mut T {
129 /// Casts to a pointer of another type.
131 pub const fn cast<U>(self) -> Unique<U> {
132 unsafe { Unique::new_unchecked(self.as_ptr() as *mut U) }
136 #[unstable(feature = "ptr_internals", issue = "none")]
137 impl<T: ?Sized> Clone for Unique<T> {
139 fn clone(&self) -> Self {
144 #[unstable(feature = "ptr_internals", issue = "none")]
145 impl<T: ?Sized> Copy for Unique<T> {}
147 #[unstable(feature = "ptr_internals", issue = "none")]
148 impl<T: ?Sized, U: ?Sized> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> {}
150 #[unstable(feature = "ptr_internals", issue = "none")]
151 impl<T: ?Sized, U: ?Sized> DispatchFromDyn<Unique<U>> for Unique<T> where T: Unsize<U> {}
153 #[unstable(feature = "ptr_internals", issue = "none")]
154 impl<T: ?Sized> fmt::Debug for Unique<T> {
155 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156 fmt::Pointer::fmt(&self.as_ptr(), f)
160 #[unstable(feature = "ptr_internals", issue = "none")]
161 impl<T: ?Sized> fmt::Pointer for Unique<T> {
162 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163 fmt::Pointer::fmt(&self.as_ptr(), f)
167 #[unstable(feature = "ptr_internals", issue = "none")]
168 impl<T: ?Sized> From<&mut T> for Unique<T> {
170 fn from(reference: &mut T) -> Self {
171 unsafe { Unique { pointer: reference as *mut T, _marker: PhantomData } }
175 #[unstable(feature = "ptr_internals", issue = "none")]
176 impl<T: ?Sized> From<&T> for Unique<T> {
178 fn from(reference: &T) -> Self {
179 unsafe { Unique { pointer: reference as *const T, _marker: PhantomData } }
183 #[unstable(feature = "ptr_internals", issue = "none")]
184 impl<T: ?Sized> From<NonNull<T>> for Unique<T> {
186 fn from(p: NonNull<T>) -> Self {
187 unsafe { Unique::new_unchecked(p.as_ptr()) }