archery/shared_pointer/kind/arct/
mod.rs

1use crate::shared_pointer::kind::SharedPointerKind;
2use alloc::boxed::Box;
3use core::fmt;
4use core::fmt::Debug;
5use core::fmt::Formatter;
6use core::mem;
7use core::mem::ManuallyDrop;
8use core::ops::Deref;
9use core::ops::DerefMut;
10use core::ptr;
11use triomphe::Arc;
12
13type UntypedArc = Arc<()>;
14
15/// [Type constructors](https://en.wikipedia.org/wiki/Type_constructor) for
16/// [`triomphe::Arc`](triomphe::Arc) pointers.
17pub struct ArcTK {
18    /// We use [`ManuallyDrop`] here, so that we can drop it explicitly as
19    /// [`Arc<T>`](triomphe::Arc).  Not sure if it can be dropped as [`UntypedArc`], but it
20    /// seems to be playing with fire (even more than we already are).
21    inner: ManuallyDrop<UntypedArc>,
22}
23
24impl ArcTK {
25    #[inline(always)]
26    fn new_from_inner<T>(arc: Arc<T>) -> ArcTK {
27        ArcTK { inner: ManuallyDrop::new(unsafe { mem::transmute::<Arc<T>, UntypedArc>(arc) }) }
28    }
29
30    #[inline(always)]
31    unsafe fn take_inner<T>(self) -> Arc<T> {
32        let arc: UntypedArc = ManuallyDrop::into_inner(self.inner);
33
34        unsafe { mem::transmute(arc) }
35    }
36
37    #[inline(always)]
38    unsafe fn as_inner_ref<T>(&self) -> &Arc<T> {
39        let arc_t: *const Arc<T> = ptr::from_ref::<UntypedArc>(self.inner.deref()).cast::<Arc<T>>();
40
41        // Static check to make sure we are not messing up the sizes.
42        // This could happen if we allowed for `T` to be unsized, because it would need to be
43        // represented as a wide pointer inside `Arc`.
44        // TODO Use static_assertion when https://github.com/nvzqz/static-assertions-rs/issues/21
45        //      gets fixed
46        let _ = mem::transmute::<UntypedArc, Arc<T>>;
47
48        unsafe { &*arc_t }
49    }
50
51    #[inline(always)]
52    unsafe fn as_inner_mut<T>(&mut self) -> &mut Arc<T> {
53        let arc_t: *mut Arc<T> =
54            ptr::from_mut::<UntypedArc>(self.inner.deref_mut()).cast::<Arc<T>>();
55
56        unsafe { &mut *arc_t }
57    }
58}
59
60unsafe impl SharedPointerKind for ArcTK {
61    #[inline(always)]
62    fn new<T>(v: T) -> ArcTK {
63        ArcTK::new_from_inner(Arc::new(v))
64    }
65
66    #[inline(always)]
67    fn from_box<T>(v: Box<T>) -> ArcTK {
68        ArcTK::new_from_inner::<T>(Arc::from(v))
69    }
70
71    #[inline(always)]
72    unsafe fn as_ptr<T>(&self) -> *const T {
73        unsafe { Arc::as_ptr(self.as_inner_ref()) }
74    }
75
76    #[inline(always)]
77    unsafe fn deref<T>(&self) -> &T {
78        unsafe { self.as_inner_ref::<T>().as_ref() }
79    }
80
81    #[inline(always)]
82    unsafe fn try_unwrap<T>(self) -> Result<T, ArcTK> {
83        unsafe { Arc::try_unwrap(self.take_inner()).map_err(ArcTK::new_from_inner) }
84    }
85
86    #[inline(always)]
87    unsafe fn get_mut<T>(&mut self) -> Option<&mut T> {
88        unsafe { Arc::get_mut(self.as_inner_mut()) }
89    }
90
91    #[inline(always)]
92    unsafe fn make_mut<T: Clone>(&mut self) -> &mut T {
93        unsafe { Arc::make_mut(self.as_inner_mut()) }
94    }
95
96    #[inline(always)]
97    unsafe fn strong_count<T>(&self) -> usize {
98        unsafe { Arc::count(self.as_inner_ref::<T>()) }
99    }
100
101    #[inline(always)]
102    unsafe fn clone<T>(&self) -> ArcTK {
103        unsafe { ArcTK { inner: ManuallyDrop::new(Arc::clone(self.as_inner_ref())) } }
104    }
105
106    #[inline(always)]
107    unsafe fn drop<T>(&mut self) {
108        unsafe {
109            ptr::drop_in_place::<Arc<T>>(self.as_inner_mut());
110        }
111    }
112}
113
114impl Debug for ArcTK {
115    #[inline(always)]
116    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
117        f.write_str("ArcTK")
118    }
119}
120
121#[cfg(test)]
122mod test;