1use std::{marker::PhantomData, ptr::NonNull};
2
3#[repr(transparent)]
4pub(crate) struct Own<T>
6where
7 T: ?Sized,
8{
9 pub(crate) ptr: NonNull<T>,
10}
11
12unsafe impl<T> Send for Own<T> where T: ?Sized {}
13unsafe impl<T> Sync for Own<T> where T: ?Sized {}
14
15impl<T> Copy for Own<T> where T: ?Sized {}
16
17impl<T> Clone for Own<T>
18where
19 T: ?Sized,
20{
21 fn clone(&self) -> Self {
22 *self
23 }
24}
25
26impl<T> Own<T>
27where
28 T: ?Sized,
29{
30 pub(crate) fn new(ptr: Box<T>) -> Self {
31 Own {
32 ptr: unsafe { NonNull::new_unchecked(Box::into_raw(ptr)) },
33 }
34 }
35
36 pub(crate) fn cast<U: CastTo>(self) -> Own<U::Target> {
37 Own {
38 ptr: self.ptr.cast(),
39 }
40 }
41
42 pub(crate) unsafe fn boxed(self) -> Box<T> {
43 Box::from_raw(self.ptr.as_ptr())
44 }
45
46 pub(crate) const fn by_ref<'a>(&self) -> Ref<'a, T> {
47 Ref {
48 ptr: self.ptr,
49 lifetime: PhantomData,
50 }
51 }
52
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)]
63pub(crate) struct Ref<'a, T>
65where
66 T: ?Sized,
67{
68 pub(crate) ptr: NonNull<T>,
69 lifetime: PhantomData<&'a T>,
70}
71
72impl<'a, T> Copy for Ref<'a, T> where T: ?Sized {}
73
74impl<'a, T> Clone for Ref<'a, T>
75where
76 T: ?Sized,
77{
78 fn clone(&self) -> Self {
79 *self
80 }
81}
82
83impl<'a, T> Ref<'a, T>
84where
85 T: ?Sized,
86{
87 pub(crate) fn new(ptr: &'a T) -> Self {
88 Ref {
89 ptr: NonNull::from(ptr),
90 lifetime: PhantomData,
91 }
92 }
93
94 pub(crate) const fn from_raw(ptr: NonNull<T>) -> Self {
95 Ref {
96 ptr,
97 lifetime: PhantomData,
98 }
99 }
100
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
108 pub(crate) fn by_mut(self) -> Mut<'a, T> {
109 Mut {
110 ptr: self.ptr,
111 lifetime: PhantomData,
112 }
113 }
114
115 pub(crate) const fn as_ptr(self) -> *const T {
116 self.ptr.as_ptr() as *const T
117 }
118
119 pub(crate) unsafe fn deref(self) -> &'a T {
120 &*self.ptr.as_ptr()
121 }
122}
123
124#[allow(explicit_outlives_requirements)]
125#[repr(transparent)]
126pub(crate) struct Mut<'a, T>
128where
129 T: ?Sized,
130{
131 pub(crate) ptr: NonNull<T>,
132 lifetime: PhantomData<&'a mut T>,
133}
134
135impl<'a, T> Copy for Mut<'a, T> where T: ?Sized {}
136
137impl<'a, T> Clone for Mut<'a, T>
138where
139 T: ?Sized,
140{
141 fn clone(&self) -> Self {
142 *self
143 }
144}
145
146impl<'a, T> Mut<'a, T>
147where
148 T: ?Sized,
149{
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
157 pub(crate) const fn by_ref(self) -> Ref<'a, T> {
158 Ref {
159 ptr: self.ptr,
160 lifetime: PhantomData,
161 }
162 }
163
164 pub(crate) fn extend<'b>(self) -> Mut<'b, T> {
165 Mut {
166 ptr: self.ptr,
167 lifetime: PhantomData,
168 }
169 }
170
171 pub(crate) unsafe fn deref_mut(self) -> &'a mut T {
172 &mut *self.ptr.as_ptr()
173 }
174}
175
176impl<'a, T> Mut<'a, T> {
177 pub(crate) unsafe fn read(self) -> T {
178 self.ptr.as_ptr().read()
179 }
180}
181
182pub(crate) trait CastTo {
183 type Target;
184}
185
186impl<T> CastTo for T {
187 type Target = T;
188}