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
#![doc(html_root_url = "https://docs.rs/reference-counted-singleton/0.1.1")]
#[cfg(test)]
mod tests;
use std::error::Error;
use std::hash::{Hash, Hasher};
use std::mem::ManuallyDrop;
use std::ops::Deref;
use std::sync::{Arc, Mutex};
#[derive(Debug)]
pub struct RefCountedSingleton<T>(Mutex<Option<Arc<T>>>);
impl<T> Default for RefCountedSingleton<T> {
fn default() -> Self {
Self(Mutex::new(None))
}
}
impl<T> RefCountedSingleton<T> {
pub fn get_or_init<E: Error>(
&'_ self,
creator: impl FnOnce() -> Result<T, E>,
) -> Result<RCSRef<'_, T>, Option<E>> {
if let Ok(mut value) = self.0.lock() {
match *value {
None => match creator() {
Ok(data) => {
let data = Arc::new(data);
let data_ref = Arc::clone(&data);
*value = Some(data);
Ok(RCSRef {
data: ManuallyDrop::new(data_ref),
rcs: &self,
})
}
Err(err) => Err(Some(err)),
},
Some(ref data) => Ok(RCSRef {
data: ManuallyDrop::new(Arc::clone(data)),
rcs: &self,
}),
}
} else {
Err(None)
}
}
pub fn get(&'_ self) -> Option<RCSRef<'_, T>> {
self.0.lock().ok().and_then(|value| {
value.as_ref().map(|data| RCSRef {
data: ManuallyDrop::new(Arc::clone(data)),
rcs: &self,
})
})
}
}
#[derive(Debug)]
pub struct RCSRef<'t, T> {
data: ManuallyDrop<Arc<T>>,
rcs: &'t RefCountedSingleton<T>,
}
impl<'t, T: PartialEq> PartialEq for RCSRef<'t, T> {
fn eq(&self, other: &Self) -> bool {
self.data.as_ref().deref().eq(other.data.as_ref().deref())
}
}
impl<'t, T: Eq> Eq for RCSRef<'t, T> {}
impl<'t, T: PartialOrd> PartialOrd for RCSRef<'t, T> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.data
.as_ref()
.deref()
.partial_cmp(other.data.as_ref().deref())
}
}
impl<'t, T: Ord> Ord for RCSRef<'t, T> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.data.as_ref().deref().cmp(other.data.as_ref().deref())
}
}
impl<'t, T: Hash> Hash for RCSRef<'t, T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.data.as_ref().deref().hash(state)
}
}
impl<'t, T> Deref for RCSRef<'t, T> {
type Target = T;
fn deref(&self) -> &T {
self.data.deref().deref()
}
}
impl<'t, T> Clone for RCSRef<'t, T> {
fn clone(&self) -> Self {
Self {
data: ManuallyDrop::new(Arc::clone(&self.data)),
rcs: self.rcs,
}
}
}
impl<'t, T> Drop for RCSRef<'t, T> {
fn drop(&mut self) {
unsafe { ManuallyDrop::drop(&mut self.data) };
if let Ok(mut value) = self.rcs.0.lock() {
if let Some(data) = value.take() {
match Arc::try_unwrap(data) {
Ok(data) => drop(data),
Err(data) => *value = Some(data),
}
}
}
}
}