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
#![cfg(feature = "tbox")]
use std::borrow::Borrow;
use std::borrow::BorrowMut;
use std::cmp::Ordering;
use std::fmt;
use std::hash::Hash;
use std::hash::Hasher;
use std::marker::PhantomData;
use std::ops::Deref;
use std::ops::DerefMut;

use crate::*;

/// The pool type backing the `TRc`.
#[cfg(feature = "st_tbox")]
#[doc(hidden)]
pub type TRcPool<T> = STPool<RcInner<T>>;

#[cfg(not(feature = "st_tbox"))]
#[doc(hidden)]
pub type TRcPool<T> = TPool<RcInner<T>>;

/// For each type that shall be allocated with `TRcs` there must be an associated global
/// memory pool. This is defined with this macro.
///
/// ```rust,ignore
/// use onsen::*;
///
/// // ZST tag
/// struct MyTag;
///
/// // define a pool holding u8 values
/// define_trc_pool!(MyTag: u8);
///
/// /// allocated a trc from the u8 pool tagged by 'MyTag'
/// let trc = TRc::new(123u8, MyTag);
/// ```
#[macro_export]
macro_rules! define_trc_pool {
    ($TAG:ty:$T:ty) => {
        $crate::assoc_static!($TAG: $T, $crate::TRcPool<$T> = $crate::TRcPool::new());
    };
}

/// A reference counted smart pointer for Pool allocated objects. This wraps Slots in a safe
/// way. A `TRc` need a Pool holding `RcInner<T>`, not `T`.
pub struct TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    slot: Slot<RcInner<T>, Mutable>,
    tag: PhantomData<TAG>,
}

impl<T, TAG> TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    /// Associated function that returns the number of strong counters of this `TRc`.
    #[must_use]
    pub fn strong_count(this: &Self) -> usize {
        this.slot.get().get_strong()
    }

    /// Associated function that returns the number of weak counters of this `TRc`.
    #[must_use]
    pub fn weak_count(this: &Self) -> usize {
        this.slot.get().get_weak()
    }
}

impl<T, TAG> TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    /// Allocate a `TRc` from a Pool.
    #[inline]
    pub fn new(t: T, _tag: TAG) -> Self {
        Self {
            slot: T::get_static().alloc(RcInner::new(t)).for_mutation(),
            tag: PhantomData,
        }
    }

    /// Allocate a `TRc` from a Pool with inferred or turbofish tag.
    #[inline]
    pub fn new_notag(t: T) -> Self {
        Self {
            slot: T::get_static().alloc(RcInner::new(t)).for_mutation(),
            tag: PhantomData,
        }
    }

    /// Creates a `TWeak` reference from a `TRc`.
    #[must_use]
    pub fn downgrade(this: &Self) -> TWeak<T, TAG> {
        this.slot.get().inc_weak();
        unsafe {
            TWeak::<T, TAG> {
                slot: this.slot.copy(),
                tag: PhantomData,
            }
        }
    }
}

impl<T, TAG> Default for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static + Default,
    TAG: 'static,
{
    /// Allocate a default initialized `TRc`
    #[inline]
    fn default() -> Self {
        TRc::new_notag(T::default())
    }
}

impl<T, TAG> Clone for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    #[must_use]
    fn clone(&self) -> Self {
        self.slot.get().inc_strong();
        unsafe {
            Self {
                slot: self.slot.copy(),
                tag: PhantomData,
            }
        }
    }
}

impl<T, TAG> Drop for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    #[inline]
    fn drop(&mut self) {
        let mslot = self.slot.get_mut();

        mslot.dec_strong();

        if mslot.get_strong() == 0 {
            if mslot.get_weak() == 0 {
                // no references exist, can be freed completely
                unsafe {
                    T::get_static().free_by_ref(&mut self.slot);
                }
            } else {
                // only weak references exist, drop in place
                unsafe {
                    mslot.data.assume_init_drop();
                }
            }
        }
    }
}

impl<T, TAG> Deref for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    type Target = T;

    #[inline]
    fn deref(&self) -> &<Self as Deref>::Target {
        unsafe { self.slot.get().data.assume_init_ref() }
    }
}

impl<T, TAG> DerefMut for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    #[inline]
    fn deref_mut(&mut self) -> &mut <Self as Deref>::Target {
        unsafe { self.slot.get_mut().data.assume_init_mut() }
    }
}

impl<T, TAG> Borrow<T> for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    #[inline]
    fn borrow(&self) -> &T {
        unsafe { self.slot.get().data.assume_init_ref() }
    }
}

impl<T, TAG> BorrowMut<T> for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    #[inline]
    fn borrow_mut(&mut self) -> &mut T {
        unsafe { self.slot.get_mut().data.assume_init_mut() }
    }
}

impl<T, TAG> AsRef<T> for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    #[inline]
    fn as_ref(&self) -> &T {
        unsafe { self.slot.get().data.assume_init_ref() }
    }
}

impl<T, TAG> AsMut<T> for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    #[inline]
    fn as_mut(&mut self) -> &mut T {
        unsafe { self.slot.get_mut().data.assume_init_mut() }
    }
}

impl<T: PartialEq, TAG> PartialEq for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        PartialEq::eq(&**self, &**other)
    }
}

impl<T: PartialOrd, TAG> PartialOrd for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        PartialOrd::partial_cmp(&**self, &**other)
    }
    #[inline]
    fn lt(&self, other: &Self) -> bool {
        PartialOrd::lt(&**self, &**other)
    }
    #[inline]
    fn le(&self, other: &Self) -> bool {
        PartialOrd::le(&**self, &**other)
    }
    #[inline]
    fn ge(&self, other: &Self) -> bool {
        PartialOrd::ge(&**self, &**other)
    }
    #[inline]
    fn gt(&self, other: &Self) -> bool {
        PartialOrd::gt(&**self, &**other)
    }
}

impl<T: Ord, TAG> Ord for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        Ord::cmp(&**self, &**other)
    }
}

impl<T: Eq, TAG> Eq for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
}

impl<T: Hash, TAG> Hash for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        (**self).hash(state);
    }
}

impl<T: Hasher, TAG> Hasher for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    fn finish(&self) -> u64 {
        (**self).finish()
    }
    fn write(&mut self, bytes: &[u8]) {
        (**self).write(bytes);
    }
    fn write_u8(&mut self, i: u8) {
        (**self).write_u8(i);
    }
    fn write_u16(&mut self, i: u16) {
        (**self).write_u16(i);
    }
    fn write_u32(&mut self, i: u32) {
        (**self).write_u32(i);
    }
    fn write_u64(&mut self, i: u64) {
        (**self).write_u64(i);
    }
    fn write_u128(&mut self, i: u128) {
        (**self).write_u128(i);
    }
    fn write_usize(&mut self, i: usize) {
        (**self).write_usize(i);
    }
    fn write_i8(&mut self, i: i8) {
        (**self).write_i8(i);
    }
    fn write_i16(&mut self, i: i16) {
        (**self).write_i16(i);
    }
    fn write_i32(&mut self, i: i32) {
        (**self).write_i32(i);
    }
    fn write_i64(&mut self, i: i64) {
        (**self).write_i64(i);
    }
    fn write_i128(&mut self, i: i128) {
        (**self).write_i128(i);
    }
    fn write_isize(&mut self, i: isize) {
        (**self).write_isize(i);
    }
    // fn write_length_prefix(&mut self, len: usize) {
    //     (**self).write_length_prefix(len)
    // }
    // fn write_str(&mut self, s: &str) {
    //     (**self).write_str(s)
    // }
}

impl<T: fmt::Display, TAG> fmt::Display for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&**self, f)
    }
}

impl<T: fmt::Debug, TAG> fmt::Debug for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&**self, f)
    }
}

impl<T, TAG> fmt::Pointer for TRc<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let ptr: *const T = &**self;
        fmt::Pointer::fmt(&ptr, f)
    }
}

/// `TWeak` references do not keep the object alive.
pub struct TWeak<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    slot: Slot<RcInner<T>, Mutable>,
    tag: PhantomData<TAG>,
}

impl<T, TAG> TWeak<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    /// Associated function that returns the number of strong counters of this `TWeak`.
    #[must_use]
    pub fn strong_count(&self) -> usize {
        self.slot.get().get_strong()
    }

    /// Associated function that returns the number of weak counters of this `TWeak`.
    #[must_use]
    pub fn weak_count(&self) -> usize {
        self.slot.get().get_weak()
    }
}

impl<T, TAG> TWeak<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    /// Tries to create a `TRc` from a `TWeak` reference. Fails when the strong count was zero.
    #[must_use]
    pub fn upgrade(&self) -> Option<TRc<T, TAG>> {
        if self.strong_count() > 0 {
            self.slot.get().inc_strong();
            unsafe {
                Some(TRc::<T, TAG> {
                    slot: self.slot.copy(),
                    tag: PhantomData,
                })
            }
        } else {
            None
        }
    }
}

impl<T, TAG> Clone for TWeak<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    fn clone(&self) -> Self {
        self.slot.get().inc_weak();
        unsafe {
            Self {
                slot: self.slot.copy(),
                tag: PhantomData,
            }
        }
    }
}

impl<T, TAG> Drop for TWeak<T, TAG>
where
    T: AssocStatic<TRcPool<T>, TAG> + 'static,
    TAG: 'static,
{
    #[inline]
    fn drop(&mut self) {
        let mslot = self.slot.get_mut();
        mslot.dec_weak();

        if mslot.get_strong() == 0 {
            if mslot.get_weak() == 0 {
                // no references exist, can be freed completely
                unsafe {
                    T::get_static().free_by_ref(&mut self.slot);
                }
            } else {
                // only weak references exist, drop in place
                unsafe {
                    mslot.data.assume_init_drop();
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::*;
    use serial_test::serial;

    define_trc_pool!((): &'static str);
    define_trc_pool!((): u64);

    #[test]
    #[ignore]
    #[serial]
    fn smoke() {
        TBox::<&'static str, ()>::pool()
            .acquire()
            .expect("some other thread owns the pool");

        let _mybox = TRc::new("TBoxed", ());

        TBox::<&'static str, ()>::pool()
            .release()
            .expect("thread does not own the pool");
    }
}