1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use crate::*;
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct ArcBorrow<'a, T: ?Sized + 'a> {
ptr: ptr::NonNull<T>,
phantom: PhantomData<&'a T>,
}
impl<'a, T: ?Sized> Copy for ArcBorrow<'a, T> {}
impl<'a, T: ?Sized> Clone for ArcBorrow<'a, T> {
#[inline]
fn clone(&self) -> Self {
*self
}
}
impl<'a, T: ?Sized> ArcBorrow<'a, T> {
#[inline]
pub fn clone_arc(&self) -> Arc<T> {
self.as_arc().clone()
}
#[inline]
pub fn as_arc(&self) -> &Arc<T> {
unsafe { &*(self as *const ArcBorrow<T> as *const Arc<T>) }
}
#[inline]
pub unsafe fn from_ref(ptr: &'a T) -> Self {
ArcBorrow::from_raw(ptr)
}
#[inline]
pub unsafe fn from_raw(ptr: *const T) -> Self {
ArcBorrow {
ptr: ptr::NonNull::new_unchecked(ptr as *mut T),
phantom: PhantomData,
}
}
#[inline]
pub fn into_raw(this: Self) -> *const T {
this.ptr.as_ptr()
}
#[inline]
pub fn ptr_eq(this: Self, other: Self) -> bool {
this.ptr == other.ptr
}
#[inline]
pub fn get(&self) -> &'a T {
unsafe { &*self.ptr.as_ptr() }
}
pub fn count(this: ArcBorrow<'a, T>, ordering: LoadOrdering) -> usize {
Arc::count(this.as_arc(), ordering)
}
}
impl<'a, T: ?Sized> Deref for ArcBorrow<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
self.get()
}
}
impl<'a, T: ?Sized> Borrow<Arc<T>> for ArcBorrow<'a, T> {
fn borrow(&self) -> &Arc<T> {
self.as_arc()
}
}
impl<'a, T: ?Sized> Borrow<&'a T> for ArcBorrow<'a, T> {
fn borrow(&self) -> &&'a T {
unsafe { &*(self as *const ArcBorrow<T> as *const &T) }
}
}
impl<'a, T: ?Sized> Borrow<T> for ArcBorrow<'a, T> {
fn borrow(&self) -> &T {
self.deref()
}
}
impl<'a, T: ?Sized> AsRef<Arc<T>> for ArcBorrow<'a, T> {
fn as_ref(&self) -> &Arc<T> {
self.as_arc()
}
}
impl<'a, T: ?Sized> AsRef<&'a T> for ArcBorrow<'a, T> {
fn as_ref(&self) -> &&'a T {
unsafe { &*(self as *const ArcBorrow<T> as *const &T) }
}
}
impl<'a, T: ?Sized> AsRef<T> for ArcBorrow<'a, T> {
fn as_ref(&self) -> &T {
self.deref()
}
}
impl<'a, T: ?Sized> AsRef<*const T> for ArcBorrow<'a, T> {
#[inline]
fn as_ref(&self) -> &*const T {
unsafe { &*(self as *const ArcBorrow<T> as *const *const T) }
}
}
impl<'a, T: ?Sized> AsRef<*mut T> for ArcBorrow<'a, T> {
#[inline]
fn as_ref(&self) -> &*mut T {
unsafe { &*(self as *const ArcBorrow<T> as *const *mut T) }
}
}
impl<'a, T: ?Sized> AsRef<ptr::NonNull<T>> for ArcBorrow<'a, T> {
#[inline]
fn as_ref(&self) -> &ptr::NonNull<T> {
unsafe { &*(self as *const ArcBorrow<T> as *const ptr::NonNull<T>) }
}
}
#[cfg(feature = "stable_deref_trait")]
unsafe impl<'a, T: ?Sized> StableDeref for ArcBorrow<'a, T> {}
#[cfg(feature = "stable_deref_trait")]
unsafe impl<'a, T: ?Sized> CloneStableDeref for ArcBorrow<'a, T> {}
#[cfg(feature = "serde")]
impl<'a, T: ?Sized + Serialize> Serialize for ArcBorrow<'a, T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::ser::Serializer,
{
(**self).serialize(serializer)
}
}
#[cfg(feature = "erasable")]
unsafe impl<'a, T: ?Sized + Erasable> ErasablePtr for ArcBorrow<'a, T> {
fn erase(this: Self) -> ErasedPtr {
T::erase(this.ptr)
}
unsafe fn unerase(this: ErasedPtr) -> Self {
ArcBorrow {
ptr: T::unerase(this),
phantom: PhantomData,
}
}
}