1use std::{marker::PhantomData, ptr::NonNull};
2
3pub(crate) struct OwnedPtr<T: ?Sized> {
7 ptr: NonNull<T>,
8}
9
10impl<T: ?Sized> Copy for OwnedPtr<T> {}
11
12impl<T: ?Sized> Clone for OwnedPtr<T> {
13 fn clone(&self) -> Self {
14 *self
15 }
16}
17
18unsafe impl<T> Send for OwnedPtr<T> where T: Send {}
19unsafe impl<T> Sync for OwnedPtr<T> where T: Send {}
20
21impl<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 Self {
29 ptr: unsafe { NonNull::new_unchecked(Box::into_raw(boxed)) },
30 }
31 }
32
33 pub(crate) fn cast<U>(self) -> OwnedPtr<U> {
35 OwnedPtr {
36 ptr: self.ptr.cast(),
37 }
38 }
39
40 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
66pub(crate) struct RefPtr<'a, T: ?Sized> {
69 pub(crate) ptr: NonNull<T>,
70 _marker: PhantomData<&'a T>,
71}
72
73unsafe impl<'a, T: ?Sized> Send for RefPtr<'a, T> where &'a T: Send {}
76unsafe impl<'a, T: ?Sized> Sync for RefPtr<'a, T> where &'a T: Sync {}
77
78impl<'a, T: ?Sized> Copy for RefPtr<'a, T> {}
79impl<'a, T: ?Sized> Clone for RefPtr<'a, T> {
80 fn clone(&self) -> Self {
81 *self
82 }
83}
84
85impl<'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 pub(crate) fn cast<U>(self) -> RefPtr<'a, U> {
95 RefPtr {
96 ptr: self.ptr.cast(),
97 _marker: PhantomData,
98 }
99 }
100
101 #[inline]
107 pub(crate) unsafe fn as_ref(&self) -> &'a T {
108 unsafe { self.ptr.as_ref() }
109 }
110}
111
112pub(crate) struct MutPtr<'a, T: ?Sized> {
115 pub(crate) ptr: NonNull<T>,
116 _marker: PhantomData<&'a mut T>,
117}
118
119unsafe impl<'a, T: ?Sized> Send for MutPtr<'a, T> where &'a mut T: Send {}
122unsafe impl<'a, T: ?Sized> Sync for MutPtr<'a, T> where &'a mut T: Sync {}
123
124impl<'a, T: ?Sized> Copy for MutPtr<'a, T> {}
125impl<'a, T: ?Sized> Clone for MutPtr<'a, T> {
126 fn clone(&self) -> Self {
127 *self
128 }
129}
130
131impl<'a, T: ?Sized> MutPtr<'a, T> {
132 pub(crate) fn cast<U>(self) -> MutPtr<'a, U> {
134 MutPtr {
135 ptr: self.ptr.cast(),
136 _marker: PhantomData,
137 }
138 }
139
140 #[inline]
146 pub(crate) unsafe fn into_mut(mut self) -> &'a mut T {
147 unsafe { self.ptr.as_mut() }
148 }
149}