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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use std::{marker::PhantomData, mem::ManuallyDrop, sync::Arc};

use core_extensions::prelude::*;

use crate::{
    abi_stability::StableAbi,
    pointer_trait::{CallReferentDrop, StableDeref, TransmuteElement},
    std_types::{RResult},
    std_types::utypeid::{UTypeId,new_utypeid},
    return_value_equality::ReturnValueEquality,
};

#[cfg(test)]
mod test;

mod private {
    use super::*;

    /// Ffi-safe version of ::std::sync::Arc<_>
    #[derive(StableAbi)]
    #[repr(C)]
    #[sabi(inside_abi_stable_crate)]
    #[sabi(shared_stable_abi(T))]
    pub struct RArc<T> {
        data: *const T,
        // This is a pointer instead of a static reference only because
        // the compiler complains that T doesn't live for the static lifetime,
        // even though ArcVtable<T> doesn't contain any T.
        vtable: *const ArcVtable<T>,
        _marker: PhantomData<T>,
    }

    impl_from_rust_repr! {
        impl[T] From<Arc<T>> for RArc<T> {
            fn(this){
                let out = RArc {
                    data: Arc::into_raw(this),
                    vtable: VTableGetter::LIB_VTABLE as *const ArcVtable<T>,
                    _marker: Default::default(),
                };
                out
            }
        }
    }

    unsafe impl<T> StableDeref for RArc<T> {}

    unsafe impl<T, O> TransmuteElement<O> for RArc<T> {
        type TransmutedPtr = RArc<O>;
    }

    impl<T> RArc<T> {
        #[inline(always)]
        pub(super) fn data(&self) -> *const T {
            self.data
        }

        #[inline]
        pub(crate) fn into_raw(self) -> *const T {
            let this = ManuallyDrop::new(self);
            this.data
        }

        #[inline(always)]
        pub(crate) fn vtable<'a>(&self) -> &'a ArcVtable<T> {
            unsafe { &*self.vtable }
        }

        #[cfg(test)]
        pub(super) fn set_vtable_for_testing(&mut self) {
            self.vtable = VTableGetter::LIB_VTABLE_FOR_TESTING as *const ArcVtable<T>;
        }
    }
}

pub use self::private::RArc;

impl<T> RArc<T> {
    pub fn new(this: T) -> Self {
        Arc::new(this).into()
    }
}

impl<T> RArc<T> {

    /// Converts this into an `Arc<T>`
    ///
    /// # Allocators
    ///
    /// The reason `RArc<T>` cannot always be converted to an `Arc<T>` is 
    /// because their allocators might be different.
    ///
    /// # When is T cloned
    ///
    /// `T` is cloned if the current dynamic_library/executable is 
    /// not the one that created the `RArc<T>`,
    /// and the strong count is greater than 1.
    pub fn into_arc(this: Self) -> Arc<T>
    where
        T: Clone,
    {
        let this_vtable =this.vtable();
        let other_vtable=VTableGetter::LIB_VTABLE;
        if ::std::ptr::eq(this_vtable,other_vtable)||
            this_vtable.type_id==other_vtable.type_id
        {
            unsafe { Arc::from_raw(this.into_raw()) }
        } else {
            Self::try_unwrap(this)
                .unwrap_or_else(|x| T::clone(&x))
                .piped(Arc::new)
        }
    }

    /// Attempts to unwrap this `RArc<T>` into a `T`,
    /// returns Err(self) if the `RArc<T>`s strong count is greater than 1.
    #[inline]
    pub fn try_unwrap(this: Self) -> Result<T, Self>{
        let vtable = this.vtable();
        (vtable.try_unwrap)(this).into_result()
    }

    /// Attempts to create a mutable reference to `T`,
    /// failing if `RArc<T>`s strong count is greater than 1.
    #[inline]
    pub fn get_mut(this: &mut Self) -> Option<&mut T>{
        let vtable = this.vtable();
        (vtable.get_mut)(this)
    }

    /// Makes a mutable reference to `T`,
    /// if there are other `RArc<T>`s pointing to the same value,
    /// then `T` is cloned to ensure unique ownership of the value.
    ///
    /// # Postconditions
    ///
    /// After this call, the strong count of `this` will be 1,
    /// because either it was 1 before the call,
    /// or because a new `RArc<T>` was created to ensure unique ownership of `T`.
    #[inline]
    pub fn make_mut<'a>(this: &'a mut Self) -> &'a mut T 
    where T:Clone
    {
        // Workaround for non-lexical lifetimes not being smart enough 
        // to figure out that this borrow doesn't continue in the None branch.
        let unbounded_this=unsafe{ &mut *(this as *mut Self) };
        match Self::get_mut(unbounded_this) {
            Some(x)=>x,
            None=>{
                let new_arc=RArc::new((**this).clone());
                *this=new_arc;
                // This is fine,since this is a freshly created arc with a clone of the data.
                unsafe{
                    &mut *(this.data() as *mut T)
                }
            }
        }
    }
    
}

impl<T> Default for RArc<T>
where
    T: Default,
{
    fn default() -> Self {
        RArc::new(T::default())
    }
}

impl<T> Clone for RArc<T> {
    fn clone(&self) -> Self {
        (self.vtable().clone)(self)
    }
}

impl_into_rust_repr! {
    impl[T] Into<Arc<T>> for RArc<T>
    where[
        T: Clone+StableAbi,
    ]{
        fn(this){
            Self::into_arc(this)
        }
    }
}

impl<T> Drop for RArc<T> {
    fn drop(&mut self) {
        // The layout of the RArc<_> won't change since it doesn't
        // actually support ?Sized types.
        unsafe {
            let vtable = self.vtable();
            (vtable.destructor)((self.data() as *const T).into(), CallReferentDrop::Yes);
        }
    }
}

shared_impls! {pointer
    mod=arc_impls
    new_type=RArc[][T],
    original_type=Arc,
}

unsafe impl<T> Sync for RArc<T> where T: Send + Sync {}

unsafe impl<T> Send for RArc<T> where T: Send + Sync {}

/////////////////////////////////////////////////////////

mod vtable_mod {
    use super::*;

    pub(super) struct VTableGetter<'a, T>(&'a T);

    impl<'a, T: 'a> VTableGetter<'a, T> {
        // The VTABLE for this type in this executable/library
        pub(super) const LIB_VTABLE: &'a ArcVtable<T> = &ArcVtable {
            type_id:ReturnValueEquality{
                function:new_utypeid::<RArc<()>>
            },
            destructor: destructor_arc::<T>,
            clone: clone_arc::<T>,
            get_mut: get_mut_arc::<T>,
            try_unwrap: try_unwrap_arc::<T>,
        };

        #[cfg(test)]
        pub(super) const LIB_VTABLE_FOR_TESTING: &'a ArcVtable<T> = &ArcVtable {
            type_id:ReturnValueEquality{
                function:new_utypeid::<RArc<i32>>
            },
            ..*Self::LIB_VTABLE
        };
    }

    #[derive(StableAbi)]
    #[repr(C)]
    #[sabi(inside_abi_stable_crate)]
    pub struct ArcVtable<T> {
        pub(super) type_id:ReturnValueEquality<UTypeId>,
        pub(super) destructor: unsafe extern "C" fn(*const T, CallReferentDrop),
        pub(super) clone: extern "C" fn(&RArc<T>) -> RArc<T>,
        pub(super) get_mut: extern "C" fn(&mut RArc<T>) -> Option<&mut T>,
        pub(super) try_unwrap:extern "C" fn(RArc<T>) -> RResult<T, RArc<T>>,
    }

}
use self::vtable_mod::{ArcVtable, VTableGetter};

unsafe extern "C" fn destructor_arc<T>(this: *const T, call_drop: CallReferentDrop) {
    extern_fn_panic_handling! {
        if call_drop == CallReferentDrop::Yes {
            drop(Arc::from_raw(this));
        } else {
            drop(Arc::from_raw(this as *const ManuallyDrop<T>));
        }
    }
}

extern "C" fn clone_arc<T>(this: &RArc<T>) -> RArc<T> {
    unsafe {
        this.data()
            .piped(|x| Arc::from_raw(x))
            .piped(ManuallyDrop::new)
            .piped(|x| Arc::clone(&x))
            .into()
    }
}

extern "C" fn get_mut_arc<'a,T>(this: &'a mut RArc<T>) -> Option<&'a mut T> {
    unsafe {
        let arc=Arc::from_raw(this.data());
        let mut arc=ManuallyDrop::new(arc);
        // This is fine,since we are only touching the data afterwards,
        // which is guaranteed to have the 'a lifetime.
        let arc:&'a mut Arc<T>=&mut *(&mut *arc as *mut Arc<T>);
        Arc::get_mut(arc)
    }
}

extern "C" fn try_unwrap_arc<T>(this: RArc<T>) -> RResult<T, RArc<T>> {
    unsafe {
        this.into_raw()
            .piped(|x| Arc::from_raw(x))
            .piped(Arc::try_unwrap)
            .map_err(RArc::from)
            .into()
    }
}