castbox 0.1.4

A Runtime-Typed Reference-Counted Smart Pointer and Concurrent Programming tools.
Documentation
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
use crate::anyref::inner::{AnyRefInner, MAX_REFCOUNT};
use crate::anyref::ptr_interface::PtrInterface;
use crate::anyref::weak::WeakAnyRef;
use crate::utils::is_dangling;
use crossync::sync::{WatchGuardMut, WatchGuardRef};
use std::any::{Any, TypeId};
use std::mem::ManuallyDrop;
use std::ops::Deref;
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::process::abort;
use std::sync::atomic;
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use std::{fmt, hint, ptr};

#[repr(transparent)]
pub struct AnyRef {
    ptr: *const AnyRefInner,
}

unsafe impl Send for AnyRef {}
unsafe impl Sync for AnyRef {}

impl UnwindSafe for AnyRef {}
impl RefUnwindSafe for AnyRef {}

impl AnyRef {
    /// Creates a new `AnyRef` containing the given value.
    ///
    /// # Example
    /// ```
    /// use castbox::AnyRef;
    /// let a = AnyRef::new(42);
    /// assert_eq!(a.as_ref::<i32>(), 42);
    /// ```
    pub fn new<T>(value: T) -> Self
    where
        T: Any + Sized,
    {
        unsafe { Self::from_inner(Box::leak(Box::new(AnyRefInner::new(value)))) }
    }

    /// Attempts to extract the inner value if there is exactly one strong reference.
    ///
    /// # Example
    /// ```
    /// use castbox::AnyRef;
    /// let a = AnyRef::new(123i32);
    /// let value = AnyRef::try_unwrap::<i32>(a).unwrap();
    /// assert_eq!(value, 123i32);
    /// ```
    pub fn try_unwrap<T>(this: Self) -> Result<T, Self>
    where
        T: Any + Sized,
    {
        let inner_ptr = this.ptr as *mut AnyRefInner;

        // SAFETY: We access only atomic fields via raw pointer arithmetic
        let strong_ptr = unsafe { ptr::addr_of!((*inner_ptr).strong) };
        let weak_ptr = unsafe { ptr::addr_of!((*inner_ptr).weak) };
        let type_id = unsafe { ptr::addr_of!((*inner_ptr).type_id).read() };

        // Attempt to become sole owner by changing strong from 1 -> 0
        if unsafe { &*strong_ptr }
            .compare_exchange(1, 0, Acquire, Relaxed)
            .is_err()
        {
            return Err(this);
        }

        // Synchronize with the decrement that triggered deletion semantics
        atomic::fence(Acquire);

        // Check weak count. If it's not the implicit weak (1), restore and return Err.
        let weak_count = unsafe { &*weak_ptr }.load(Relaxed);
        if weak_count != 1 {
            // Restore strong to 1 so the object remains live and return the original AnyRef.
            unsafe { &*strong_ptr }.store(1, Release);
            return Err(this);
        }

        // We will consume `this` on success; keep it alive for now
        let this = ManuallyDrop::new(this);

        // If the type doesn't match, restore strong count and return Err
        if type_id != TypeId::of::<T>() {
            // Restore strong count back to 1 so the AnyRef remains valid
            unsafe { &*strong_ptr }.store(1, Release);
            return Err(ManuallyDrop::into_inner(this));
        }

        // SAFETY: We've verified the type and have exclusive access.
        // We need to extract the value without triggering a double-drop.
        let data_ptr = unsafe { ptr::addr_of_mut!((*inner_ptr).data) };

        // Extract the value from the Box<dyn Any>
        let elem: T = unsafe {
            let mut guard = (*data_ptr).lock_exclusive();

            // Take the Box<dyn Any> out of the guard, replacing with a dummy
            let boxed_any: Box<dyn Any> = std::mem::replace(&mut *guard, Box::new(()));
            drop(guard);

            // Downcast to Box<T> and unbox
            match boxed_any.downcast::<T>() {
                Ok(boxed_t) => *boxed_t,
                Err(_) => unreachable!("Type mismatch after type_id check"),
            }
        };

        // Make a weak pointer to clean up the implicit strong-weak reference
        // and deallocate the AnyRefInner when it drops
        let _weak = WeakAnyRef { ptr: this.ptr };

        // Drop the data field - it now contains a dummy Box<()> which is safe to drop
        unsafe {
            let data_field_ptr = ptr::addr_of_mut!((*inner_ptr).data);
            ptr::drop_in_place(data_field_ptr);
        }

        Ok(elem)
    }

    /// Returns a reference to the inner `AnyRefInner`.
    ///
    /// # Safety
    /// This is safe to call as long as there is at least one strong reference,
    /// which is guaranteed by holding an `AnyRef`.
    #[inline]
    pub(crate) fn inner(&self) -> &AnyRefInner {
        // SAFETY: While this AnyRef is alive we're guaranteed
        // that the inner pointer is valid and the data hasn't been dropped.
        unsafe { &*self.ptr }
    }

    #[inline]
    fn inner_mut(&mut self) -> &mut AnyRefInner {
        let ptr = self.get_mut_inner_ptr();
        unsafe { &mut *ptr }
    }

    #[inline]
    pub fn map<T, U: 'static, F>(self, func: F) -> AnyRef
    where
        T: Any,
        F: FnOnce(WatchGuardRef<'_, T>) -> U,
    {
        let ptr = self.as_ref::<T>();
        AnyRef::new(func(ptr))
    }

    /// Returns a raw pointer to the contained type, if possible.
    ///
    /// # Example
    /// ```
    /// use castbox::AnyRef;
    /// let a = AnyRef::new(50);
    /// let ptr = unsafe { a.as_cast_ptr::<i32>() };
    /// unsafe { assert_eq!(*ptr, 50); }
    /// ```
    pub unsafe fn as_cast_ptr<T: Any>(&self) -> *const T {
        if self.inner().type_id != TypeId::of::<T>() {
            panic!(
                "AnyRef: wrong cast in as_ref::<{}>()",
                std::any::type_name::<T>()
            );
        }
        let ptr = self.as_ptr();
        ptr as *const T
    }

    /// Returns `true` if the `AnyRef` is the only strong reference to the value.
    ///
    /// # Example
    /// ```
    /// use castbox::AnyRef;
    /// let a = AnyRef::new("unique");
    /// assert!(AnyRef::is_unique(&a));
    /// let b = a.clone();
    /// assert!(!AnyRef::is_unique(&a));
    /// ```
    pub fn is_unique(this: &Self) -> bool {
        let inner_ptr = this.ptr as *mut AnyRefInner;

        // SAFETY: Access atomic fields via raw pointer to avoid creating reference
        let weak_ptr = unsafe { ptr::addr_of!((*inner_ptr).weak) };
        let strong_ptr = unsafe { ptr::addr_of!((*inner_ptr).strong) };

        // Lock the weak pointer count if we appear to be the sole weak pointer holder.
        if unsafe { &*weak_ptr }
            .compare_exchange(1, usize::MAX, Acquire, Relaxed)
            .is_ok()
        {
            let unique = unsafe { &*strong_ptr }.load(Acquire) == 1;
            unsafe { &*weak_ptr }.store(1, Release);
            unique
        } else {
            false
        }
    }

    /// Convert into a weak reference
    /// # Example
    ///
    /// ```
    /// use castbox::AnyRef;
    /// let five = AnyRef::new(5);
    /// let weak_five = AnyRef::downgrade(&five);
    /// ```
    pub fn downgrade(&self) -> WeakAnyRef {
        let inner_ptr = self.ptr as *mut AnyRefInner;

        // SAFETY: Access atomic field via raw pointer
        let weak_ptr = unsafe { ptr::addr_of!((*inner_ptr).weak) };
        let weak_atomic = unsafe { &*weak_ptr };

        let mut cur = weak_atomic.load(Relaxed);

        loop {
            // Check if the weak counter is currently "locked"; if so, spin.
            if cur == usize::MAX {
                hint::spin_loop();
                cur = weak_atomic.load(Relaxed);
                continue;
            }

            assert!(cur <= MAX_REFCOUNT, "INTERNAL OVERFLOW ERROR");

            match weak_atomic.compare_exchange_weak(cur, cur + 1, Acquire, Relaxed) {
                Ok(_) => {
                    debug_assert!(!is_dangling(self.ptr));
                    return WeakAnyRef { ptr: self.ptr };
                }
                Err(old) => cur = old,
            }
        }
    }

    /// Returns the number of weak references (excluding the implicit one).
    ///
    /// # Example
    /// ```
    /// use castbox::AnyRef;
    /// let a = AnyRef::new(10);
    /// let w = a.downgrade();
    /// assert_eq!(AnyRef::weak_count(&a), 1);
    /// ```
    #[inline]
    pub fn weak_count(this: &Self) -> usize {
        let cnt = this.inner().weak.load(Relaxed);
        if cnt == usize::MAX { 0 } else { cnt - 1 }
    }

    /// Returns the number of strong references.
    ///
    /// # Example
    /// ```
    /// use castbox::AnyRef;
    /// let a = AnyRef::new("count");
    /// let b = a.clone();
    /// assert_eq!(AnyRef::strong_count(&a), 2);
    /// ```
    #[inline]
    pub fn strong_count(this: &Self) -> usize {
        this.inner().strong.load(Relaxed)
    }

    #[inline]
    pub fn ptr_eq(this: &Self, other: &Self) -> bool {
        ptr::addr_eq(this.get_mut_inner_ptr(), other.get_mut_inner_ptr())
    }

    pub fn into_raw(self) -> *const Box<dyn Any> {
        let this = ManuallyDrop::new(self);
        let inner_ptr = this.ptr as *mut AnyRefInner;

        // SAFETY: Get pointer to data field without creating reference to AnyRefInner
        let cell_ptr = unsafe { ptr::addr_of!((*inner_ptr).data) };
        cell_ptr as *const Box<dyn Any>
    }

    pub unsafe fn from_raw<T: ?Sized>(ptr: *const T) -> Self {
        unsafe { Self::from_raw_in(ptr) }
    }

    pub fn type_name(&self) -> &'static str {
        self.inner().type_name
    }
}

impl PtrInterface for AnyRef {
    #[inline]
    fn get_mut_inner_ptr(&self) -> *mut AnyRefInner {
        self.ptr as *mut AnyRefInner
    }

    #[inline]
    unsafe fn from_inner_in(ptr: *mut AnyRefInner) -> Self {
        debug_assert!(!ptr.is_null());
        Self { ptr }
    }
}

impl AnyRef {
    pub fn try_downcast_ref<U: Any>(&self) -> Option<WatchGuardRef<'_, U>> {
        if self.inner().type_id == TypeId::of::<U>() {
            let guard = self.inner().data.lock_shared();
            match unsafe { WatchGuardRef::downcast::<U>(guard) } {
                Ok(guard) => Some(guard),
                Err(_) => None,
            }
        } else {
            None
        }
    }

    pub fn try_downcast_mut<U: Any>(&self) -> Option<WatchGuardMut<'_, U>> {
        if self.inner().type_id == TypeId::of::<U>() {
            let guard = self.inner().data.lock_exclusive();
            match unsafe { WatchGuardMut::downcast::<U>(guard) } {
                Ok(guard) => Some(guard),
                Err(_) => None,
            }
        } else {
            None
        }
    }

    pub fn as_ref<U: Any>(&self) -> WatchGuardRef<'_, U> {
        match self.try_downcast_ref::<U>() {
            Some(data) => data,
            None => panic!("Downcast failed"),
        }
    }

    pub fn as_mut<U: Any>(&self) -> WatchGuardMut<'_, U> {
        match self.try_downcast_mut::<U>() {
            Some(data) => data,
            None => panic!("Downcast mut failed"),
        }
    }
}

impl Clone for AnyRef {
    /// Makes a clone of the `AnyRef` pointer.
    ///
    /// This creates another pointer to the same allocation, increasing the
    /// strong reference count.
    #[inline]
    fn clone(&self) -> AnyRef {
        // Using a relaxed ordering is alright here, as knowledge of the
        // original reference prevents other threads from erroneously deleting
        // the object.
        if self.inner().strong.fetch_add(1, Relaxed) > MAX_REFCOUNT {
            abort();
        }

        unsafe { Self::from_inner_in(self.get_mut_inner_ptr()) }
    }
}

impl Default for AnyRef {
    fn default() -> AnyRef {
        unsafe {
            Self::from_inner(Box::leak(Box::write(
                Box::new_uninit(),
                AnyRefInner::default(),
            )))
        }
    }
}

impl AnyRef {
    /// Creates a new `AnyRef` using the default value of `T`.
    ///
    /// # Example
    /// ```
    /// use castbox::AnyRef;
    /// let a: AnyRef = AnyRef::default_with::<String>();
    /// assert_eq!(a.as_ref::<String>(), "");
    /// ```
    pub fn default_with<T: 'static + Default>() -> Self {
        Self::from(Box::new(T::default()))
    }

    /// Replaces the inner value with a new value of type `T`.
    ///
    /// # Example
    /// ```
    /// use castbox::AnyRef;
    /// let a = AnyRef::new(0);
    /// let a = AnyRef::fill(a, 123);
    /// assert_eq!(a.as_ref::<i32>(), 123);
    /// ```
    pub fn fill<T: 'static>(mut this: Self, value: T) -> Self {
        let ref_inner = &mut *this.inner_mut();
        let mut wd = ref_inner.data.lock_exclusive();
        *wd = Box::new(value);
        ref_inner.type_id = TypeId::of::<T>();
        drop(wd);
        this
    }
}

impl Drop for AnyRef {
    fn drop(&mut self) {
        let inner_ptr = self.ptr as *mut AnyRefInner;

        // SAFETY: Access the strong count atomically via raw pointer.
        // This avoids creating a reference to the entire AnyRefInner struct.
        let strong_ptr = unsafe { ptr::addr_of!((*inner_ptr).strong) };

        // Because `fetch_sub` is already atomic, we do not need to synchronize
        // with other threads unless we are going to delete the object.
        if unsafe { &*strong_ptr }.fetch_sub(1, Release) != 1 {
            return;
        }

        // This fence synchronizes with the Release ordering in the fetch_sub above.
        // It ensures that all previous writes to the data are visible before we drop it.
        atomic::fence(Acquire);

        // Create a weak reference that will handle deallocation of AnyRefInner
        // when all weak references are gone.
        let _weak = WeakAnyRef { ptr: self.ptr };

        // SAFETY: We're the last strong reference, so we have exclusive access to the data.
        // We use ptr::addr_of_mut! to get a raw pointer to the `data` field directly,
        // without creating a reference to the entire AnyRefInner struct.
        // This is crucial because WeakAnyRef::upgrade() may concurrently read the atomic
        // fields (strong, weak) of AnyRefInner, and creating a mutable reference to the
        // entire struct would conflict with those reads.
        unsafe {
            let data_ptr = ptr::addr_of_mut!((*inner_ptr).data);
            ptr::drop_in_place(data_ptr);
        }
    }
}

impl<T: 'static> From<*mut T> for AnyRef {
    /// Creates a new `AnyRef` taking possession over the pointed value `*mut T`.
    ///
    /// # Safety
    /// - `ptr` must be valid and pointing to a dynamically allocated instance of T
    ///   (ex. `Box::into_raw`).
    /// - After AnyRef will own `ptr` and no other reference to ptr should be used.
    ///
    /// # Example
    /// ```
    /// use castbox::utils::{create_raw_pointer, dealloc_layout};
    /// use castbox::AnyRef;
    /// let raw = create_raw_pointer(String::from("hello"));
    /// let a = AnyRef::from(raw);
    /// a.as_mut::<String>().push_str(":1");
    /// dealloc_layout(raw);
    /// assert_eq!(a.as_ref::<String>(), String::from("hello:1"));
    /// ```
    #[inline]
    fn from(ptr: *mut T) -> Self {
        let value = unsafe { ptr::read(ptr) };
        AnyRef::new(value)
    }
}

impl From<&str> for AnyRef {
    /// Creates a new `AnyRef` from a `&str`.
    ///
    /// # Example
    /// ```
    /// use castbox::AnyRef;
    /// let a = AnyRef::from("hello");
    /// assert_eq!(*a.as_ref::<String>(), "hello");
    /// ```
    #[inline]
    fn from(s: &str) -> Self {
        AnyRef::new(s.to_string())
    }
}

impl<T: 'static> From<Box<T>> for AnyRef {
    /// Creates a new `AnyRef` from a `Box<T>`.
    ///
    /// # Example
    /// ```
    /// use castbox::AnyRef;
    /// let boxed = Box::new("hello");
    /// let a = AnyRef::from(boxed);
    ///  assert_eq!(a.as_ref::<&str>(), "hello");
    /// ```
    #[inline]
    fn from(b: Box<T>) -> Self
    where
        T: Sized,
    {
        unsafe { Self::from_inner(Box::leak(Box::new(AnyRefInner::from_box(b)))) }
    }
}

impl fmt::Debug for AnyRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let inner = self.inner();
        f.debug_struct("AnyRef")
            .field("type", &inner.type_name)
            .field("S", &inner.strong)
            .field("W", &inner.weak)
            .finish()
    }
}

impl fmt::Pointer for AnyRef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Pointer::fmt(&self.inner().data.lock_shared().deref(), f)
    }
}