im_lists/
shared.rs

1use std::{ops::Deref, rc::Rc, sync::Arc};
2
3pub trait PointerFamily: 'static {
4    type Pointer<T: 'static>: Deref<Target = T>;
5
6    fn new<T>(value: T) -> Self::Pointer<T>;
7    fn strong_count<T>(this: &Self::Pointer<T>) -> usize;
8    fn try_unwrap<T>(this: Self::Pointer<T>) -> Option<T>;
9    fn get_mut<T>(this: &mut Self::Pointer<T>) -> Option<&mut T>;
10    fn ptr_eq<T>(this: &Self::Pointer<T>, other: &Self::Pointer<T>) -> bool;
11    fn make_mut<T: Clone>(ptr: &mut Self::Pointer<T>) -> &mut T;
12    fn clone<T>(ptr: &Self::Pointer<T>) -> Self::Pointer<T>;
13    fn as_ptr<T>(this: &Self::Pointer<T>) -> *const T;
14    fn into_raw<T>(this: Self::Pointer<T>) -> *const T;
15    /// # Safety
16    /// This must be called from a pointer as returned from into_raw
17    unsafe fn from_raw<T>(this: *const T) -> Self::Pointer<T>;
18}
19
20pub struct RcPointer;
21
22impl PointerFamily for RcPointer {
23    type Pointer<T: 'static> = Rc<T>;
24
25    fn new<T: 'static>(value: T) -> Self::Pointer<T> {
26        Rc::new(value)
27    }
28
29    fn strong_count<T: 'static>(this: &Self::Pointer<T>) -> usize {
30        Rc::strong_count(this)
31    }
32
33    fn try_unwrap<T: 'static>(this: Self::Pointer<T>) -> Option<T> {
34        Rc::try_unwrap(this).ok()
35    }
36
37    fn get_mut<T: 'static>(this: &mut Self::Pointer<T>) -> Option<&mut T> {
38        Rc::get_mut(this)
39    }
40
41    fn ptr_eq<T: 'static>(this: &Self::Pointer<T>, other: &Self::Pointer<T>) -> bool {
42        Rc::ptr_eq(this, other)
43    }
44
45    fn make_mut<T: Clone + 'static>(ptr: &mut Self::Pointer<T>) -> &mut T {
46        Rc::make_mut(ptr)
47    }
48
49    fn clone<T: 'static>(ptr: &Self::Pointer<T>) -> Self::Pointer<T> {
50        Rc::clone(ptr)
51    }
52
53    fn as_ptr<T: 'static>(this: &Self::Pointer<T>) -> *const T {
54        Rc::as_ptr(this)
55    }
56
57    fn into_raw<T: 'static>(this: Self::Pointer<T>) -> *const T {
58        Rc::into_raw(this)
59    }
60
61    unsafe fn from_raw<T: 'static>(this: *const T) -> Self::Pointer<T> {
62        Rc::from_raw(this)
63    }
64}
65
66pub struct ArcPointer;
67
68impl PointerFamily for ArcPointer {
69    type Pointer<T: 'static> = Arc<T>;
70
71    fn new<T: 'static>(value: T) -> Self::Pointer<T> {
72        Arc::new(value)
73    }
74
75    fn strong_count<T: 'static>(this: &Self::Pointer<T>) -> usize {
76        Arc::strong_count(this)
77    }
78
79    fn try_unwrap<T: 'static>(this: Self::Pointer<T>) -> Option<T> {
80        Arc::try_unwrap(this).ok()
81    }
82
83    fn get_mut<T: 'static>(this: &mut Self::Pointer<T>) -> Option<&mut T> {
84        Arc::get_mut(this)
85    }
86
87    fn ptr_eq<T: 'static>(this: &Self::Pointer<T>, other: &Self::Pointer<T>) -> bool {
88        Arc::ptr_eq(this, other)
89    }
90
91    fn make_mut<T: Clone + 'static>(ptr: &mut Self::Pointer<T>) -> &mut T {
92        Arc::make_mut(ptr)
93    }
94
95    fn clone<T: 'static>(ptr: &Self::Pointer<T>) -> Self::Pointer<T> {
96        Arc::clone(ptr)
97    }
98
99    fn as_ptr<T: 'static>(this: &Self::Pointer<T>) -> *const T {
100        Arc::as_ptr(this)
101    }
102
103    fn into_raw<T: 'static>(this: Self::Pointer<T>) -> *const T {
104        Arc::into_raw(this)
105    }
106
107    unsafe fn from_raw<T: 'static>(this: *const T) -> Self::Pointer<T> {
108        Arc::from_raw(this)
109    }
110}