wiwi/
rc.rs

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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use crate::prelude_std::*;
use crate::num::*;

pub use self::inner::{ Counter, ThreadCounter, AtomicCounter };
pub use self::str::{
	RcStr,
	RcStrWeak,
	RcStrThread,
	RcStrThreadWeak,
	RcStrAtomic,
	RcStrAtomicWeak
};

/// This module encapsulates and provides low level manipulation APIs on
/// a raw and opaque inner reference counting pointer
mod inner;
mod str;

/// Reference counted thin pointer, that can hold one sized
/// value and one (dynamically sized) slice
#[repr(transparent)]
pub struct Rc<C, V, S>
where
	C: Counter
{
	/// Opaque reference to inner data
	inner: inner::RcInner<C, V, S>
}

/// Weak pointer to a reference counted thin pointer [`Rc`]
#[repr(transparent)]
pub struct RcWeak<C, V, S>
where
	C: Counter
{
	/// Opaque reference to inner data
	inner: inner::RcInner<C, V, S>
}

/// Single threaded reference counting thin pointer
pub type RcThread<V, S = ()> = Rc<ThreadCounter, V, S>;

/// Weak pointer to a single threaded reference counted thin pointer [`RcThread`]
pub type RcThreadWeak<V, S = ()> = RcWeak<ThreadCounter, V, S>;

/// Atomically counted reference counting thin pointer
pub type RcAtomic<V, S = ()> = Rc<AtomicCounter, V, S>;

/// Weak pointer to an atomically counted reference counted thin pointer [`RcAtomic`]
pub type RcAtomicWeak<V, S = ()> = RcWeak<AtomicCounter, V, S>;

impl<C, V> Rc<C, V, ()>
where
	C: Counter
{
	/// Creates a reference counter from a (sized) value, storing it in the `value`
	/// field
	#[inline]
	pub fn from_value(value: V) -> Self {
		Self { inner: inner::new_from_value(value) }
	}
}

impl<C, S> Rc<C, (), S>
where
	C: Counter
{
	/// Creates a reference counter from an array, storing it in the `slice` field
	/// and erasing the array length from the type
	///
	/// If you want to keep the const length of the array, you should use
	/// [`from_value`](Rc::from_value) instead. The memory usage of the RC
	/// allocation actually does not differ between these two.
	#[inline]
	pub fn from_array_into_slice<const N: usize>(array: [S; N]) -> Self {
		Self { inner: inner::new_from_array_into_slice(array) }
	}
}

impl<C, S> Rc<C, (), S>
where
	C: Counter,
	S: Clone
{
	/// Creates a reference counter from a slice, cloning all elements into the
	/// `slice` field
	///
	/// If your slice contains elements that implement copy, you should use
	/// [`from_slice_copy`](Rc::from_slice_copy) instead, which can be more
	/// efficient. (Rust, specialisation when?)
	#[inline]
	pub fn from_slice_clone(slice: &[S]) -> Self {
		Self { inner: inner::new_from_slice_clone(slice) }
	}
}

impl<C, S> Rc<C, (), S>
where
	C: Counter,
	S: Copy
{
	/// Creates a reference counter from a slice, copying all elements into
	/// the `slice` field
	#[inline]
	pub fn from_slice_copy(slice: &[S]) -> Self {
		Self { inner: inner::new_from_slice_copy(slice) }
	}
}

impl<C, V, S> Rc<C, V, S>
where
	C: Counter
{
	/// Creates a reference counter from a value and an array, with the array
	/// being stored in the `slice` field
	#[inline]
	pub fn from_value_and_array_into_slice<const N: usize>(value: V, array: [S; N]) -> Self {
		Self { inner: inner::new_from_value_and_array_into_slice(value, array) }
	}
}

impl<C, V, S> Rc<C, V, S>
where
	C: Counter,
	S: Clone
{
	/// Creates a reference counter from a value and a slice, cloning all
	/// elements of the slice into the `slice` field
	///
	/// If your slice contains elements that implement copy, you should use
	/// [`from_value_and_slice_copy`](Rc::from_value_and_slice_copy) instead,
	/// which can be more efficient. (Rust, specialisation when?)
	#[inline]
	pub fn from_value_and_slice_clone(value: V, slice: &[S]) -> Self {
		Self { inner: inner::new_from_value_and_slice_clone(value, slice) }
	}
}

impl<C, V, S> Rc<C, V, S>
where
	C: Counter,
	S: Copy
{
	/// Creates a reference counter from a value and a slice, copying all
	/// elements of the slice into the `slice` field
	#[inline]
	pub fn from_value_and_slice_copy(value: V, slice: &[S]) -> Self {
		Self { inner: inner::new_from_value_and_slice_copy(value, slice) }
	}
}

impl<C, V, S> Rc<C, V, S>
where
	C: Counter
{
	/// Gets an immurable reference to the value stored in the `value` field
	#[inline]
	pub fn as_value_ref(&self) -> &V {
		// SAFETY: `self.inner` is a valid instance
		unsafe { inner::value_ref(self.inner) }
	}

	/// Gets an immurable reference to the slice stored in the `slice` field
	#[inline]
	pub fn as_slice_ref(&self) -> &[S] {
		// SAFETY: `self.inner` is a valid instance
		unsafe { inner::slice_ref(self.inner) }
	}

	/// Gets the strong pointer count
	#[inline]
	pub fn strong_count(&self) -> usize {
		// SAFETY: `self.inner` is a valid instance
		unsafe { inner::counter_ref(self.inner).strong_count() }
	}

	/// Gets the weak pointer count
	#[inline]
	pub fn weak_count(&self) -> usize {
		// SAFETY: `self.inner` is a valid instance
		unsafe { inner::counter_ref(self.inner).weak_count() - 1 }
	}

	/// "Downgrades" this pointer, returning a weak pointer [`RcWeak`] to the data
	#[inline]
	pub fn downgrade(&self) -> RcWeak<C, V, S> {
		// SAFETY: `self.inner` is a valid instance
		unsafe { inner::counter_ref(self.inner).inc_weak_for_new_ref() }

		RcWeak { inner: self.inner }
	}
}

impl<C, V, S> Clone for Rc<C, V, S>
where
	C: Counter
{
	/// Creates a new strong pointer to the same allocation,
	/// incrementing the strong count
	#[inline]
	fn clone(&self) -> Self {
		// SAFETY: `self.inner` is a valid instance
		unsafe { inner::counter_ref(self.inner).inc_strong_for_new_ref() }

		Self { inner: self.inner }
	}
}

impl<C, V, S> Drop for Rc<C, V, S>
where
	C: Counter
{
	#[inline]
	fn drop(&mut self) {
		// SAFETY: `self.inner` is a valid instance
		let should_drop = unsafe { inner::counter_ref(self.inner).dec_strong_for_drop() };

		if !should_drop { return }

		// SAFETY: we checked we should drop, and early exited if we shouldn't
		unsafe { inner::drop_instance(self.inner) }

		// take care of the "fake" weak ptr collectively held by strong ptrs
		drop(RcWeak { inner: self.inner });
	}
}

impl<C, V, S> RcWeak<C, V, S>
where
	C: Counter
{
	/// Gets the strong pointer count
	#[inline]
	pub fn strong_count(&self) -> usize {
		// SAFETY: `self.inner` is a valid instance
		unsafe { inner::counter_ref(self.inner).strong_count() }
	}

	/// Gets the weak pointer count
	#[inline]
	pub fn weak_count(&self) -> usize {
		// SAFETY: `self.inner` is a valid instance
		let weak = unsafe { inner::counter_ref(self.inner).weak_count() };

		// SAFETY: same as above
		let strong = unsafe { inner::counter_ref(self.inner).strong_count() };

		weak - (strong > 0).into_usize()
	}

	/// "Upgrades" this pointer, returning a strong pointer [`Rc`] to the data
	/// if there are still other strong pointers to it
	#[inline]
	pub fn upgrade(&self) -> Option<Rc<C, V, S>> {
		// SAFETY: `self.inner` is a valid instance
		let should_upgrade = unsafe { inner::counter_ref(self.inner).try_inc_strong_for_upgrade() };

		should_upgrade.then(|| Rc { inner: self.inner })
	}
}

impl<C, V, S> Clone for RcWeak<C, V, S>
where
	C: Counter
{
	/// Creates a new weak pointer to the same allocation,
	/// incrementing the weak count
	#[inline]
	fn clone(&self) -> Self {
		// SAFETY: `self.inner` is a valid instance
		unsafe { inner::counter_ref(self.inner).inc_weak_for_new_ref() }

		Self { inner: self.inner }
	}
}

impl<C, V, S> Drop for RcWeak<C, V, S>
where
	C: Counter
{
	#[inline]
	fn drop(&mut self) {
		// SAFETY: `self.inner` is a valid instance
		let should_dealloc = unsafe { inner::counter_ref(self.inner).dec_weak_for_drop() };

		if !should_dealloc { return }

		// SAFETY: we checked we should dealloc, and early exit if we shouldn't
		unsafe { inner::dealloc_instance(self.inner) }
	}
}

// SAFETY: we are `Send` if the counter/value/slice are all `Send`
unsafe impl<C, V, S> Send for Rc<C, V, S>
where
	C: Counter + Send,
	V: Send,
	S: Send
{}

// SAFETY: same as above
unsafe impl<C, V, S> Send for RcWeak<C, V, S>
where
	C: Counter + Send,
	V: Send,
	S: Send
{}

// SAFETY: we are `Sync` if the counter/value/slice are all `Sync`
unsafe impl<C, V, S> Sync for Rc<C, V, S>
where
	C: Counter + Sync,
	V: Sync,
	S: Sync
{}

// SAFETY: same as above
unsafe impl<C, V, S> Sync for RcWeak<C, V, S>
where
	C: Counter + Sync,
	V: Sync,
	S: Sync
{}