1 | use std::{marker::PhantomData, ptr::NonNull}; |
2 | |
3 | /// An owned pointer |
4 | /// |
5 | /// **NOTE**: Does not deallocate when dropped |
6 | pub(crate) struct OwnedPtr<T: ?Sized> { |
7 | ptr: NonNull<T>, |
8 | } |
9 | |
10 | impl<T: ?Sized> Copy for OwnedPtr<T> {} |
11 | |
12 | impl<T: ?Sized> Clone for OwnedPtr<T> { |
13 | fn clone(&self) -> Self { |
14 | *self |
15 | } |
16 | } |
17 | |
18 | unsafe impl<T> Send for OwnedPtr<T> where T: Send {} |
19 | unsafe impl<T> Sync for OwnedPtr<T> where T: Send {} |
20 | |
21 | impl<T> OwnedPtr<T> { |
22 | pub(crate) fn new(value: T) -> Self { |
23 | Self::from_boxed(Box::new(value)) |
24 | } |
25 | |
26 | pub(crate) fn from_boxed(boxed: Box<T>) -> Self { |
27 | // Safety: `Box::into_raw` is guaranteed to be non-null |
28 | Self { |
29 | ptr: unsafe { NonNull::new_unchecked(Box::into_raw(boxed)) }, |
30 | } |
31 | } |
32 | |
33 | /// Convert the pointer to another type |
34 | pub(crate) fn cast<U>(self) -> OwnedPtr<U> { |
35 | OwnedPtr { |
36 | ptr: self.ptr.cast(), |
37 | } |
38 | } |
39 | |
40 | /// Context the pointer into a Box |
41 | /// |
42 | /// # Safety |
43 | /// |
44 | /// Dropping the Box will deallocate a layout of `T` and run the destructor of `T`. |
45 | /// |
46 | /// A cast pointer must therefore be cast back to the original type before calling this method. |
47 | pub(crate) unsafe fn into_box(self) -> Box<T> { |
48 | unsafe { Box::from_raw(self.ptr.as_ptr()) } |
49 | } |
50 | |
51 | pub(crate) const fn as_ref(&self) -> RefPtr<'_, T> { |
52 | RefPtr { |
53 | ptr: self.ptr, |
54 | _marker: PhantomData, |
55 | } |
56 | } |
57 | |
58 | pub(crate) fn as_mut(&mut self) -> MutPtr<'_, T> { |
59 | MutPtr { |
60 | ptr: self.ptr, |
61 | _marker: PhantomData, |
62 | } |
63 | } |
64 | } |
65 | |
66 | /// Convenience lifetime annotated mutable pointer which facilitates returning an inferred lifetime |
67 | /// in a `fn` pointer. |
68 | pub(crate) struct RefPtr<'a, T: ?Sized> { |
69 | pub(crate) ptr: NonNull<T>, |
70 | _marker: PhantomData<&'a T>, |
71 | } |
72 | |
73 | /// Safety: RefPtr indicates a shared reference to a value and as such exhibits the same Send + |
74 | /// Sync behavior of &'a T |
75 | unsafe impl<'a, T: ?Sized> Send for RefPtr<'a, T> where &'a T: Send {} |
76 | unsafe impl<'a, T: ?Sized> Sync for RefPtr<'a, T> where &'a T: Sync {} |
77 | |
78 | impl<'a, T: ?Sized> Copy for RefPtr<'a, T> {} |
79 | impl<'a, T: ?Sized> Clone for RefPtr<'a, T> { |
80 | fn clone(&self) -> Self { |
81 | *self |
82 | } |
83 | } |
84 | |
85 | impl<'a, T: ?Sized> RefPtr<'a, T> { |
86 | pub(crate) fn new(ptr: &'a T) -> Self { |
87 | Self { |
88 | ptr: NonNull::from(ptr), |
89 | _marker: PhantomData, |
90 | } |
91 | } |
92 | |
93 | /// Convert the pointer to another type |
94 | pub(crate) fn cast<U>(self) -> RefPtr<'a, U> { |
95 | RefPtr { |
96 | ptr: self.ptr.cast(), |
97 | _marker: PhantomData, |
98 | } |
99 | } |
100 | |
101 | /// Returns a shared reference to the owned value |
102 | /// |
103 | /// # Safety |
104 | /// |
105 | /// See: [`NonNull::as_ref`] |
106 | #[inline ] |
107 | pub(crate) unsafe fn as_ref(&self) -> &'a T { |
108 | unsafe { self.ptr.as_ref() } |
109 | } |
110 | } |
111 | |
112 | /// Convenience lifetime annotated mutable pointer which facilitates returning an inferred lifetime |
113 | /// in a `fn` pointer. |
114 | pub(crate) struct MutPtr<'a, T: ?Sized> { |
115 | pub(crate) ptr: NonNull<T>, |
116 | _marker: PhantomData<&'a mut T>, |
117 | } |
118 | |
119 | /// Safety: RefPtr indicates an exclusive reference to a value and as such exhibits the same Send + |
120 | /// Sync behavior of &'a mut T |
121 | unsafe impl<'a, T: ?Sized> Send for MutPtr<'a, T> where &'a mut T: Send {} |
122 | unsafe impl<'a, T: ?Sized> Sync for MutPtr<'a, T> where &'a mut T: Sync {} |
123 | |
124 | impl<'a, T: ?Sized> Copy for MutPtr<'a, T> {} |
125 | impl<'a, T: ?Sized> Clone for MutPtr<'a, T> { |
126 | fn clone(&self) -> Self { |
127 | *self |
128 | } |
129 | } |
130 | |
131 | impl<'a, T: ?Sized> MutPtr<'a, T> { |
132 | /// Convert the pointer to another type |
133 | pub(crate) fn cast<U>(self) -> MutPtr<'a, U> { |
134 | MutPtr { |
135 | ptr: self.ptr.cast(), |
136 | _marker: PhantomData, |
137 | } |
138 | } |
139 | |
140 | /// Returns a mutable reference to the owned value with the lifetime decoupled from self |
141 | /// |
142 | /// # Safety |
143 | /// |
144 | /// See: [`NonNull::as_mut`] |
145 | #[inline ] |
146 | pub(crate) unsafe fn into_mut(mut self) -> &'a mut T { |
147 | unsafe { self.ptr.as_mut() } |
148 | } |
149 | } |
150 | |