1use alloc::boxed::Box;
2use core::marker::PhantomData;
3use core::ptr::NonNull;
4
5#[repr(transparent)]
6pub struct Own<T>
7where
8 T: ?Sized,
9{
10 pub ptr: NonNull<T>,
11}
12
13unsafe impl<T> Send for Own<T> where T: ?Sized {}
14
15unsafe impl<T> Sync for Own<T> where T: ?Sized {}
16
17impl<T> Copy for Own<T> where T: ?Sized {}
18
19impl<T> Clone for Own<T>
20where
21 T: ?Sized,
22{
23 fn clone(&self) -> Self {
24 *self
25 }
26}
27
28impl<T> Own<T>
29where
30 T: ?Sized,
31{
32 pub fn new(ptr: Box<T>) -> Self {
33 Own {
34 ptr: unsafe { NonNull::new_unchecked(Box::into_raw(ptr)) },
35 }
36 }
37
38 pub fn cast<U: CastTo>(self) -> Own<U::Target> {
39 Own {
40 ptr: self.ptr.cast(),
41 }
42 }
43
44 pub unsafe fn boxed(self) -> Box<T> {
45 unsafe { Box::from_raw(self.ptr.as_ptr()) }
46 }
47
48 pub fn by_ref(&self) -> Ref<T> {
49 Ref {
50 ptr: self.ptr,
51 lifetime: PhantomData,
52 }
53 }
54
55 pub fn by_mut(&mut self) -> Mut<T> {
56 Mut {
57 ptr: self.ptr,
58 lifetime: PhantomData,
59 }
60 }
61}
62
63#[repr(transparent)]
64pub struct Ref<'a, T>
65where
66 T: ?Sized,
67{
68 pub 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 fn new(ptr: &'a T) -> Self {
88 Ref {
89 ptr: NonNull::from(ptr),
90 lifetime: PhantomData,
91 }
92 }
93
94 pub fn from_raw(ptr: NonNull<T>) -> Self {
95 Ref {
96 ptr,
97 lifetime: PhantomData,
98 }
99 }
100
101 pub fn cast<U: CastTo>(self) -> Ref<'a, U::Target> {
102 Ref {
103 ptr: self.ptr.cast(),
104 lifetime: PhantomData,
105 }
106 }
107
108 pub fn by_mut(self) -> Mut<'a, T> {
109 Mut {
110 ptr: self.ptr,
111 lifetime: PhantomData,
112 }
113 }
114
115 pub fn as_ptr(self) -> *const T {
116 self.ptr.as_ptr().cast_const()
117 }
118
119 pub unsafe fn deref(self) -> &'a T {
120 unsafe { &*self.ptr.as_ptr() }
121 }
122}
123
124#[repr(transparent)]
125pub struct Mut<'a, T>
126where
127 T: ?Sized,
128{
129 pub ptr: NonNull<T>,
130 lifetime: PhantomData<&'a mut T>,
131}
132
133impl<'a, T> Copy for Mut<'a, T> where T: ?Sized {}
134
135impl<'a, T> Clone for Mut<'a, T>
136where
137 T: ?Sized,
138{
139 fn clone(&self) -> Self {
140 *self
141 }
142}
143
144impl<'a, T> Mut<'a, T>
145where
146 T: ?Sized,
147{
148 pub fn cast<U: CastTo>(self) -> Mut<'a, U::Target> {
149 Mut {
150 ptr: self.ptr.cast(),
151 lifetime: PhantomData,
152 }
153 }
154
155 pub fn by_ref(self) -> Ref<'a, T> {
156 Ref {
157 ptr: self.ptr,
158 lifetime: PhantomData,
159 }
160 }
161
162 pub fn extend<'b>(self) -> Mut<'b, T> {
163 Mut {
164 ptr: self.ptr,
165 lifetime: PhantomData,
166 }
167 }
168
169 pub unsafe fn deref_mut(self) -> &'a mut T {
170 unsafe { &mut *self.ptr.as_ptr() }
171 }
172}
173
174impl<'a, T> Mut<'a, T> {
175 pub unsafe fn read(self) -> T {
176 unsafe { self.ptr.as_ptr().read() }
177 }
178}
179
180pub trait CastTo {
182 type Target;
183}
184
185impl<T> CastTo for T {
186 type Target = T;
187}