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::*;

/// A "borrowed `Arc`". This is a pointer to
/// a T that is known to have been allocated within an
/// `Arc`.
///
/// This is equivalent in guarantees to `&ArcHandle<T>`, however it is
/// a bit more flexible. To obtain an `&ArcHandle<T>` you must have
/// an `ArcHandle<T>` instance somewhere pinned down until we're done with it.
/// It's also a direct pointer to `T`, so using this involves less pointer-chasing
///
/// However, C++ code may hand us refcounted things as pointers to T directly,
/// so we have to conjure up a temporary `Arc` on the stack each time. The
/// same happens for when the object is managed by a `Arc`.
///
/// `ArcBorrow` lets us deal with borrows of known-refcounted objects
/// without needing to worry about where the `Arc<T>` is.
#[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> {
    /// Clone this as an `Arc<T>`. This bumps the refcount.
    #[inline]
    pub fn clone_arc(&self) -> Arc<T> {
        self.as_arc().clone()
    }

    /// Borrow this as an `Arc<T>`. This does *not* bump the refcount.
    #[inline]
    pub fn as_arc(&self) -> &Arc<T> {
        unsafe { &*(self as *const ArcBorrow<T> as *const Arc<T>) }
    }

    /// For constructing from a pointer known to be Arc-backed,
    /// e.g. if we obtain such a pointer over FFI
    ///
    /// # Safety
    /// This pointer shouild come from `Arc::into_raw`: this, however, will *not* consume it!
    #[inline]
    pub unsafe fn from_ref(ptr: &'a T) -> Self {
        ArcBorrow::from_raw(ptr)
    }

    /// For constructing from a pointer known to be Arc-backed,
    /// e.g. if we obtain such a pointer over FFI
    ///
    /// # Safety
    /// This pointer should come from `Arc::into_raw`: this, however, will *not* consume it!
    #[inline]
    pub unsafe fn from_raw(ptr: *const T) -> Self {
        ArcBorrow {
            ptr: ptr::NonNull::new_unchecked(ptr as *mut T),
            phantom: PhantomData,
        }
    }

    /// Get the internal pointer of an `ArcBorrow`
    #[inline]
    pub fn into_raw(this: Self) -> *const T {
        this.ptr.as_ptr()
    }

    /// Compare two `ArcBorrow`s via pointer equality. Will only return
    /// true if they come from the same allocation
    #[inline]
    pub fn ptr_eq(this: Self, other: Self) -> bool {
        this.ptr == other.ptr
    }

    /// Similar to deref, but uses the lifetime |a| rather than the lifetime of
    /// self, which is incompatible with the signature of the Deref trait.
    #[inline]
    pub fn get(&self) -> &'a T {
        unsafe { &*self.ptr.as_ptr() }
    }

    /// Get the reference count of this `Arc` with a given memory ordering
    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,
        }
    }
}