archery/shared_pointer/kind/arc/
mod.rs

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