1use core::borrow::Borrow;
5use core::fmt::Debug;
6use core::mem::ManuallyDrop;
7use core::ops::Deref;
8
9use crate::concurrent::smr;
10use crate::concurrent::smr::Guard as _;
11use crate::sequential;
12pub use crate::sequential::value::Arc;
13
14pub trait Value: sequential::Value + Borrow<Self::Borrowed> {
25 type Borrowed;
37
38 type Guard<G>: smr::Guard<Self> + From<G>
41 where
42 G: smr::Guard<Self>;
43
44 unsafe fn borrow_from_raw_unchecked(raw: &u64) -> &Self::Borrowed;
51}
52
53macro_rules! impl_integer {
54 ($($ty:ty),*) => {
55 $(
56 impl Value for $ty {
57 type Borrowed = Self;
58
59 type Guard<G>
60 = smr::no_op::Guard<G, Self>
61 where
62 G: smr::Guard<Self>;
63
64 #[inline]
65 unsafe fn borrow_from_raw_unchecked(raw: &u64) -> &Self::Borrowed {
66 unsafe { core::mem::transmute::<&u64, &Self>(raw) }
67 }
68 }
69 )*
70 };
71}
72
73impl_integer!(u64, i64);
74
75impl<'v, T: 'v + Sized> Value for &'v T {
79 type Borrowed = Self;
80
81 type Guard<G>
82 = smr::no_op::Guard<G, Self>
83 where
84 G: smr::Guard<Self>;
85
86 #[inline]
87 unsafe fn borrow_from_raw_unchecked(raw: &u64) -> &Self::Borrowed {
88 unsafe { core::mem::transmute::<&u64, &Self>(raw) }
89 }
90}
91
92impl<T: Sized> Value for Box<T> {
93 type Borrowed = T;
94
95 type Guard<G>
96 = G
97 where
98 G: smr::Guard<Self>;
99
100 #[inline]
101 unsafe fn borrow_from_raw_unchecked(raw: &u64) -> &Self::Borrowed {
102 let borrow = unsafe { core::ptr::with_exposed_provenance::<T>((*raw) as usize).as_ref() };
103 if_validate!(borrow.unwrap(), unsafe { borrow.unwrap_unchecked() })
104 }
105}
106
107impl<T: Sized> Value for Arc<T> {
108 type Borrowed = ArcRef<T>;
109
110 type Guard<G>
111 = G
112 where
113 G: smr::Guard<Self>;
114
115 #[inline]
116 unsafe fn borrow_from_raw_unchecked(raw: &u64) -> &Self::Borrowed {
117 let borrow = unsafe {
118 core::ptr::with_exposed_provenance::<T>((*raw) as usize)
119 .cast::<ArcRef<T>>()
120 .as_ref()
121 };
122 if_validate!(borrow.unwrap(), unsafe { borrow.unwrap_unchecked() })
123 }
124}
125
126impl<T> Borrow<ArcRef<T>> for crate::sequential::value::Arc<T> {
127 fn borrow(&self) -> &ArcRef<T> {
128 unsafe { core::mem::transmute::<&T, &ArcRef<T>>(self.0.as_ref()) }
129 }
130}
131
132#[repr(transparent)]
135#[derive(Debug)]
136pub struct ArcRef<T>(T);
137
138impl<T> Deref for ArcRef<T> {
139 type Target = T;
140 #[inline]
141 fn deref(&self) -> &Self::Target {
142 &self.0
143 }
144}
145
146impl<T> ToOwned for ArcRef<T> {
147 type Owned = Arc<T>;
148 fn to_owned(&self) -> Self::Owned {
150 let ptr = unsafe { core::mem::transmute::<&Self, &T>(self) };
152
153 unsafe { crate::sync::Arc::increment_strong_count(ptr) };
156
157 Arc(unsafe { crate::sync::Arc::from_raw(ptr) })
159 }
160}
161
162pub struct Owned<G: smr::Guard<V>, V: Value> {
168 guard: V::Guard<G>,
169 raw: u64,
170}
171
172impl<G, V> Owned<G, V>
173where
174 G: smr::Guard<V>,
175 V: Value,
176{
177 pub(crate) unsafe fn wrap(guard: G, raw: u64) -> Self {
178 Self {
179 guard: V::Guard::<G>::from(guard),
180 raw,
181 }
182 }
183}
184
185impl<G, V> Deref for Owned<G, V>
186where
187 G: smr::Guard<V>,
188 V: Value,
189{
190 type Target = V::Borrowed;
191
192 #[inline]
193 fn deref(&self) -> &Self::Target {
194 unsafe { V::borrow_from_raw_unchecked(&self.raw) }
195 }
196}
197
198impl<G: smr::Guard<V>, V: Value> Drop for Owned<G, V> {
199 fn drop(&mut self) {
200 unsafe { self.guard.retire_value(self.raw) }
201 }
202}
203
204impl<G, V> Debug for Owned<G, V>
205where
206 G: smr::Guard<V>,
207 V: Value,
208 V::Borrowed: Debug,
209{
210 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
211 self.deref().fmt(f)
212 }
213}
214
215pub struct Shared<G: smr::Guard<V>, V: Value> {
218 _guard: V::Guard<G>,
219 raw: u64,
220}
221
222impl<G, V> Shared<G, V>
223where
224 G: smr::Guard<V>,
225 V: Value,
226{
227 pub(crate) unsafe fn wrap(guard: G, raw: u64) -> Self {
228 Self {
229 _guard: V::Guard::<G>::from(guard),
230 raw,
231 }
232 }
233}
234
235impl<G, V> Deref for Shared<G, V>
236where
237 G: smr::Guard<V>,
238 V: Value,
239{
240 type Target = V::Borrowed;
241
242 #[inline]
243 fn deref(&self) -> &Self::Target {
244 unsafe { V::borrow_from_raw_unchecked(&self.raw) }
245 }
246}
247
248impl<G, V> Debug for Shared<G, V>
249where
250 G: smr::Guard<V>,
251 V: Value,
252 V::Borrowed: Debug,
253{
254 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
255 self.deref().fmt(f)
256 }
257}
258
259pub struct Updated<G: smr::Guard<V>, V: Value> {
265 guard: V::Guard<G>,
266 old: u64,
267 new: u64,
268}
269
270impl<G, V> Updated<G, V>
271where
272 G: smr::Guard<V>,
273 V: Value,
274{
275 pub(crate) unsafe fn wrap(guard: G, old: u64, new: u64) -> Self {
276 Self {
277 guard: V::Guard::<G>::from(guard),
278 old,
279 new,
280 }
281 }
282
283 #[inline]
285 pub fn old(&self) -> &V::Borrowed {
286 unsafe { V::borrow_from_raw_unchecked(&self.old) }
287 }
288
289 #[inline]
291 #[expect(clippy::new_ret_no_self)]
292 pub fn new(&self) -> &V::Borrowed {
293 unsafe { V::borrow_from_raw_unchecked(&self.new) }
294 }
295}
296
297impl<G: smr::Guard<V>, V: Value> Drop for Updated<G, V> {
298 fn drop(&mut self) {
299 unsafe { self.guard.retire_value(self.old) }
300 }
301}
302
303impl<G, V> Debug for Updated<G, V>
304where
305 G: smr::Guard<V>,
306 V: Value,
307 V::Borrowed: Debug,
308{
309 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
310 f.debug_struct("Updated")
311 .field("old", self.old())
312 .field("new", self.new())
313 .finish()
314 }
315}
316
317pub struct Upserted<G: smr::Guard<V>, V: Value> {
323 guard: V::Guard<G>,
324 old: Option<u64>,
325 new: u64,
326}
327
328impl<G, V> Upserted<G, V>
329where
330 G: smr::Guard<V>,
331 V: Value,
332{
333 pub(crate) unsafe fn wrap(guard: G, old: Option<u64>, new: u64) -> Self {
334 Self {
335 guard: V::Guard::<G>::from(guard),
336 old,
337 new,
338 }
339 }
340
341 pub(crate) fn try_into_inserted(self) -> Result<Shared<G, V>, Self> {
342 let upserted = ManuallyDrop::new(self);
344
345 match upserted.old {
346 None => Ok(Shared {
347 _guard: unsafe { core::ptr::read(&upserted.guard) },
349 raw: upserted.new,
350 }),
351 Some(_) => Err(ManuallyDrop::into_inner(upserted)),
352 }
353 }
354
355 #[inline]
357 pub fn old(&self) -> Option<&V::Borrowed> {
358 self.old
359 .as_ref()
360 .map(|old| unsafe { V::borrow_from_raw_unchecked(old) })
361 }
362
363 #[inline]
365 #[expect(clippy::new_ret_no_self)]
366 pub fn new(&self) -> &V::Borrowed {
367 unsafe { V::borrow_from_raw_unchecked(&self.new) }
368 }
369}
370
371impl<G: smr::Guard<V>, V: Value> Drop for Upserted<G, V> {
372 fn drop(&mut self) {
373 let Some(old) = self.old else { return };
374 unsafe { self.guard.retire_value(old) }
375 }
376}
377
378impl<G, V> Debug for Upserted<G, V>
379where
380 G: smr::Guard<V>,
381 V: Value,
382 V::Borrowed: Debug,
383{
384 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
385 f.debug_struct("Upserted")
386 .field("old", &self.old())
387 .field("new", self.new())
388 .finish()
389 }
390}