1 use std::{marker::PhantomData, ptr::NonNull};
2 
3 #[repr(transparent)]
4 /// A raw pointer that owns its pointee
5 pub(crate) struct Own<T>
6 where
7     T: ?Sized,
8 {
9     pub(crate) ptr: NonNull<T>,
10 }
11 
12 unsafe impl<T> Send for Own<T> where T: ?Sized {}
13 unsafe impl<T> Sync for Own<T> where T: ?Sized {}
14 
15 impl<T> Copy for Own<T> where T: ?Sized {}
16 
17 impl<T> Clone for Own<T>
18 where
19     T: ?Sized,
20 {
clone(&self) -> Self21     fn clone(&self) -> Self {
22         *self
23     }
24 }
25 
26 impl<T> Own<T>
27 where
28     T: ?Sized,
29 {
new(ptr: Box<T>) -> Self30     pub(crate) fn new(ptr: Box<T>) -> Self {
31         Own {
32             ptr: unsafe { NonNull::new_unchecked(Box::into_raw(ptr)) },
33         }
34     }
35 
cast<U: CastTo>(self) -> Own<U::Target>36     pub(crate) fn cast<U: CastTo>(self) -> Own<U::Target> {
37         Own {
38             ptr: self.ptr.cast(),
39         }
40     }
41 
boxed(self) -> Box<T>42     pub(crate) unsafe fn boxed(self) -> Box<T> {
43         Box::from_raw(self.ptr.as_ptr())
44     }
45 
by_ref<'a>(&self) -> Ref<'a, T>46     pub(crate) const fn by_ref<'a>(&self) -> Ref<'a, T> {
47         Ref {
48             ptr: self.ptr,
49             lifetime: PhantomData,
50         }
51     }
52 
by_mut<'a>(self) -> Mut<'a, T>53     pub(crate) fn by_mut<'a>(self) -> Mut<'a, T> {
54         Mut {
55             ptr: self.ptr,
56             lifetime: PhantomData,
57         }
58     }
59 }
60 
61 #[allow(explicit_outlives_requirements)]
62 #[repr(transparent)]
63 /// A raw pointer that represents a shared borrow of its pointee
64 pub(crate) struct Ref<'a, T>
65 where
66     T: ?Sized,
67 {
68     pub(crate) ptr: NonNull<T>,
69     lifetime: PhantomData<&'a T>,
70 }
71 
72 impl<'a, T> Copy for Ref<'a, T> where T: ?Sized {}
73 
74 impl<'a, T> Clone for Ref<'a, T>
75 where
76     T: ?Sized,
77 {
clone(&self) -> Self78     fn clone(&self) -> Self {
79         *self
80     }
81 }
82 
83 impl<'a, T> Ref<'a, T>
84 where
85     T: ?Sized,
86 {
new(ptr: &'a T) -> Self87     pub(crate) fn new(ptr: &'a T) -> Self {
88         Ref {
89             ptr: NonNull::from(ptr),
90             lifetime: PhantomData,
91         }
92     }
93 
from_raw(ptr: NonNull<T>) -> Self94     pub(crate) const fn from_raw(ptr: NonNull<T>) -> Self {
95         Ref {
96             ptr,
97             lifetime: PhantomData,
98         }
99     }
100 
cast<U: CastTo>(self) -> Ref<'a, U::Target>101     pub(crate) fn cast<U: CastTo>(self) -> Ref<'a, U::Target> {
102         Ref {
103             ptr: self.ptr.cast(),
104             lifetime: PhantomData,
105         }
106     }
107 
by_mut(self) -> Mut<'a, T>108     pub(crate) fn by_mut(self) -> Mut<'a, T> {
109         Mut {
110             ptr: self.ptr,
111             lifetime: PhantomData,
112         }
113     }
114 
as_ptr(self) -> *const T115     pub(crate) const fn as_ptr(self) -> *const T {
116         self.ptr.as_ptr() as *const T
117     }
118 
deref(self) -> &'a T119     pub(crate) unsafe fn deref(self) -> &'a T {
120         &*self.ptr.as_ptr()
121     }
122 }
123 
124 #[allow(explicit_outlives_requirements)]
125 #[repr(transparent)]
126 /// A raw pointer that represents a unique borrow of its pointee
127 pub(crate) struct Mut<'a, T>
128 where
129     T: ?Sized,
130 {
131     pub(crate) ptr: NonNull<T>,
132     lifetime: PhantomData<&'a mut T>,
133 }
134 
135 impl<'a, T> Copy for Mut<'a, T> where T: ?Sized {}
136 
137 impl<'a, T> Clone for Mut<'a, T>
138 where
139     T: ?Sized,
140 {
clone(&self) -> Self141     fn clone(&self) -> Self {
142         *self
143     }
144 }
145 
146 impl<'a, T> Mut<'a, T>
147 where
148     T: ?Sized,
149 {
cast<U: CastTo>(self) -> Mut<'a, U::Target>150     pub(crate) fn cast<U: CastTo>(self) -> Mut<'a, U::Target> {
151         Mut {
152             ptr: self.ptr.cast(),
153             lifetime: PhantomData,
154         }
155     }
156 
by_ref(self) -> Ref<'a, T>157     pub(crate) const fn by_ref(self) -> Ref<'a, T> {
158         Ref {
159             ptr: self.ptr,
160             lifetime: PhantomData,
161         }
162     }
163 
extend<'b>(self) -> Mut<'b, T>164     pub(crate) fn extend<'b>(self) -> Mut<'b, T> {
165         Mut {
166             ptr: self.ptr,
167             lifetime: PhantomData,
168         }
169     }
170 
deref_mut(self) -> &'a mut T171     pub(crate) unsafe fn deref_mut(self) -> &'a mut T {
172         &mut *self.ptr.as_ptr()
173     }
174 }
175 
176 impl<'a, T> Mut<'a, T> {
read(self) -> T177     pub(crate) unsafe fn read(self) -> T {
178         self.ptr.as_ptr().read()
179     }
180 }
181 
182 pub(crate) trait CastTo {
183     type Target;
184 }
185 
186 impl<T> CastTo for T {
187     type Target = T;
188 }
189