threadcell 1.0.3

A cell whose value can only be accessed by a owning thread
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#![doc = include_str!("../README.md")]
#![warn(clippy::cargo_common_metadata)]
#![warn(clippy::doc_markdown)]
#![warn(clippy::missing_panics_doc)]
#![warn(clippy::must_use_candidate)]
#![warn(clippy::semicolon_if_nothing_returned)]
#![warn(missing_docs)]
#![warn(rustdoc::missing_crate_level_docs)]
#![cfg_attr(feature = "nightly_thread_id_value", feature(thread_id_value))]

use std::mem::ManuallyDrop;
use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicU64, Ordering};
use std::{cmp, fmt, mem};

/// A cell that can be owned by a single thread or none at all.
pub struct ThreadCell<T> {
    data: ManuallyDrop<T>,
    thread_id: AtomicU64,
}

// We use the highest bit of a thread id to indicate that we hold a guard
const GUARD_BIT: u64 = i64::MAX as u64 + 1;

#[allow(clippy::non_send_fields_in_send_ty)]
unsafe impl<T: Send> Send for ThreadCell<T> {}
unsafe impl<T: Send> Sync for ThreadCell<T> {}

impl<T> ThreadCell<T> {
    /// Creates a `ThreadCell` that is not owned by any thread. This is a const fn which
    /// allows static construction of `ThreadCells`.
    pub const fn new_disowned(data: T) -> Self {
        Self {
            data: ManuallyDrop::new(data),
            thread_id: AtomicU64::new(0),
        }
    }

    /// Creates a `ThreadCell` that is owned by the current thread.
    pub fn new_owned(data: T) -> Self {
        Self {
            data: ManuallyDrop::new(data),
            thread_id: AtomicU64::new(current_thread_id()),
        }
    }

    /// Takes the ownership of a cell.
    ///
    /// # Panics
    ///
    /// When the cell is already owned by this thread or it is owned by another thread.
    pub fn acquire(&self) {
        self.thread_id
            .compare_exchange(0, current_thread_id(), Ordering::Acquire, Ordering::Relaxed)
            .expect("Thread can not acquire ThreadCell");
    }

    /// Tries to take the ownership of a cell. Returns true when the ownership could be
    /// obtained or the cell was already owned by the current thread and false when the cell
    /// is owned by another thread.
    pub fn try_acquire(&self) -> bool {
        if self.is_acquired() {
            true
        } else {
            self.thread_id
                .compare_exchange(0, current_thread_id(), Ordering::Acquire, Ordering::Relaxed)
                .is_ok()
        }
    }

    /// Tries to take the ownership of a cell. Returns true when the ownership could be
    /// obtained and false when the cell is already owned or owned by another thread.
    /// Note that this fails when the cell is already owned (unlike `try_acquire()`).
    pub fn try_acquire_once(&self) -> bool {
        self.thread_id
            .compare_exchange(0, current_thread_id(), Ordering::Acquire, Ordering::Relaxed)
            .is_ok()
    }

    /// Takes the ownership of a cell and returns a reference to its value.
    ///
    /// # Panics
    ///
    /// When the cell is owned by another thread.
    pub fn acquire_get(&self) -> &T {
        if !self.is_owned() {
            self.acquire();
        }
        // Safety: we have it
        unsafe { self.get_unchecked() }
    }

    /// Tries to take the ownership of a cell and returns a reference to its value.
    /// Will return 'None' when the cell is owned by another thread.
    pub fn try_acquire_get(&self) -> Option<&T> {
        if self.try_acquire() {
            // Safety: we have it
            Some(unsafe { self.get_unchecked() })
        } else {
            None
        }
    }

    /// Takes the ownership of a cell and returns a mutable reference to its value.
    ///
    /// # Panics
    ///
    /// When the cell is owned by another thread.
    pub fn acquire_get_mut(&mut self) -> &mut T {
        if !self.is_owned() {
            self.acquire();
        }
        // Safety: we have it
        unsafe { self.get_mut_unchecked() }
    }

    /// Tries to take the ownership of a cell and returns a mutable reference to its value.
    /// Will return 'None' when the cell is owned by another thread.
    pub fn try_acquire_get_mut(&mut self) -> Option<&mut T> {
        if self.try_acquire() {
            // Safety: we have it
            Some(unsafe { self.get_mut_unchecked() })
        } else {
            None
        }
    }

    /// Acquires a `ThreadCell` returning a `Guard` that releases it when becoming dropped.
    ///
    /// # Panics
    ///
    /// When the cell is owned by another thread.
    #[inline]
    pub fn acquire_guard(&self) -> Guard<T> {
        self.thread_id
            .compare_exchange(
                0,
                current_thread_id() | GUARD_BIT,
                Ordering::Acquire,
                Ordering::Relaxed,
            )
            .expect("Thread can not acquire ThreadCell");
        Guard(self)
    }

    /// Acquires a `ThreadCell` returning a `Option<Guard>` that releases it when becoming
    /// dropped.  Returns `None` when self is owned by another thread.
    #[inline]
    #[mutants::skip]
    pub fn try_acquire_guard(&self) -> Option<Guard<T>> {
        if self
            .thread_id
            .compare_exchange(
                0,
                current_thread_id() | GUARD_BIT,
                Ordering::Acquire,
                Ordering::Relaxed,
            )
            .is_ok()
        {
            Some(Guard(self))
        } else {
            None
        }
    }

    /// Acquires a `ThreadCell` returning a `GuardMut` that releases it when becoming dropped.
    ///
    /// # Panics
    ///
    /// When the cell is owned by another thread.
    #[inline]
    pub fn acquire_guard_mut(&mut self) -> GuardMut<T> {
        self.thread_id
            .compare_exchange(
                0,
                current_thread_id() | GUARD_BIT,
                Ordering::Acquire,
                Ordering::Relaxed,
            )
            .expect("Thread can not acquire ThreadCell");
        GuardMut(self)
    }

    /// Acquires a `ThreadCell` returning a `Option<GuardMut>` that releases it when becoming
    /// dropped.  Returns `None` when self is owned by another thread.
    #[inline]
    pub fn try_acquire_guard_mut(&mut self) -> Option<GuardMut<T>> {
        if self
            .thread_id
            .compare_exchange(
                0,
                current_thread_id() | GUARD_BIT,
                Ordering::Acquire,
                Ordering::Relaxed,
            )
            .is_ok()
        {
            Some(GuardMut(self))
        } else {
            None
        }
    }

    /// Runs a closure on a `ThreadCell` with acquire/release.
    ///
    /// # Panics
    ///
    /// When the cell is already owned by the current thread or is owned by another thread.
    pub fn with<R, F: FnOnce(&T) -> R>(&self, f: F) -> R {
        f(&*self.acquire_guard())
    }

    /// Runs a closure on a mutable `ThreadCell` with acquire/release.
    ///
    /// # Panics
    ///
    /// When the cell is already owned by the current thread or is owned by another thread.
    pub fn with_mut<R, F: FnOnce(&mut T) -> R>(&mut self, f: F) -> R {
        f(&mut *self.acquire_guard_mut())
    }

    /// Tries to run a closure on a `ThreadCell` with acquire/release.  Returns `Some(Result)`
    /// when the cell could be acquired and None when it is owned by another thread.
    pub fn try_with<R, F: FnOnce(&T) -> R>(&self, f: F) -> Option<R> {
        Some(f(&*self.try_acquire_guard()?))
    }

    /// Tries to run a closure on a mutable `ThreadCell` with acquire/release.  Returns
    /// `Some(Result)` when the cell could be acquired and None when it is owned by another
    /// thread.
    pub fn try_with_mut<R, F: FnOnce(&mut T) -> R>(&mut self, f: F) -> Option<R> {
        Some(f(&mut *self.try_acquire_guard_mut()?))
    }

    /// Takes the ownership of a cell unconditionally. This is a no-op when the cell is
    /// already owned by the current thread. Returns 'self' thus it can be chained with
    /// `.release()`.
    ///
    /// # Safety
    ///
    /// This method does not check if the cell is owned by another thread. The owning thread
    /// may operate on the content, thus a data race/UB will happen when the accessed value is
    /// not Sync. The previous owning thread may panic when it expects owning the cell. The
    /// only safe way to use this method is to recover a cell that is owned by a thread that
    /// finished without releasing it (e.g after a panic). Attention should be paid to the
    /// fact that the value protected by the `ThreadCell` might be in a undefined state.
    ///
    /// # Panics
    ///
    /// The `ThreadCell` has a `Guard` on it. `steal()` can only be used with acquire/release
    /// semantics.
    pub unsafe fn steal(&self) -> &Self {
        if !self.is_acquired() {
            assert!(
                self.thread_id.load(Ordering::Acquire) & GUARD_BIT == 0,
                "Can't steal guarded ThreadCell"
            );
            self.thread_id.store(current_thread_id(), Ordering::SeqCst);
        }

        self
    }

    /// Sets a `ThreadCell` which is owned by the current thread into the disowned state.
    ///
    /// # Panics
    ///
    /// The current thread does not own the cell.
    pub fn release(&self) {
        self.thread_id
            .compare_exchange(current_thread_id(), 0, Ordering::Release, Ordering::Relaxed)
            .expect("Thread has no access to ThreadCell");
    }

    /// Unsafe as it doesn't check for ownership.
    #[mutants::skip]
    unsafe fn release_unchecked(&self) {
        debug_assert!(self.is_owned());
        self.thread_id.store(0, Ordering::Release);
    }

    /// Tries to set a `ThreadCell` which is owned by the current thread into the disowned
    /// state. Returns *true* on success and *false* when the current thread does not own the
    /// cell.
    pub fn try_release(&self) -> bool {
        self.thread_id
            .compare_exchange(current_thread_id(), 0, Ordering::Release, Ordering::Relaxed)
            .is_ok()
    }

    /// Returns true when the current thread owns this cell.
    #[inline(always)]
    pub fn is_owned(&self) -> bool {
        // This can be Relaxed because when we already own it (with Acquire), no other thread
        // can change the ownership.  When we do not own it this may return Zero or some other
        // thread id in a racy way, which is ok (to indicate disowned state) either way.
        self.thread_id.load(Ordering::Relaxed) & !GUARD_BIT == current_thread_id()
    }

    /// Returns true when this `ThreadCell` is not owned by any thread. As this can change at
    /// any time by another taking ownership of this `ThreadCell` the result of this function
    /// may be **inexact and racy**. Use this only when only a hint is required or access to the
    /// `ThreadCell` is synchronized by some other means.
    #[inline(always)]
    pub fn is_disowned(&self) -> bool {
        self.thread_id.load(Ordering::Acquire) == 0
    }

    /// Returns true when the current thread owns this cell by acquire.
    #[inline(always)]
    pub fn is_acquired(&self) -> bool {
        // This can be Relaxed because when we already own it (with Acquire), no other thread
        // can change the ownership.  When we do not own it this may return Zero or some other
        // thread id in a racy way, which is ok (to indicate disowned state) either way.
        self.thread_id.load(Ordering::Relaxed) == current_thread_id()
    }

    /// Returns true when the current thread holds a guard on this cell.
    #[inline(always)]
    pub fn is_guarded(&self) -> bool {
        // This can be Relaxed because when we already own it (with Acquire), no other thread
        // can change the ownership.  When we do not own it this may return Zero or some other
        // thread id in a racy way, which is ok (to indicate disowned state) either way.
        self.thread_id.load(Ordering::Relaxed) == current_thread_id() | GUARD_BIT
    }

    #[inline]
    #[track_caller]
    fn assert_owned(&self) {
        assert!(self.is_owned(), "Thread has no access to ThreadCell");
    }

    /// Consumes a owned cell and returns its content.
    ///
    /// # Panics
    ///
    /// The current thread does not own the cell.
    #[inline]
    pub fn into_inner(mut self) -> T {
        self.assert_owned();
        unsafe { ManuallyDrop::take(&mut self.data) }
    }

    /// Gets an immutable reference to the cells content.
    ///
    /// # Panics
    ///
    /// The current thread does not own the cell.
    #[inline]
    pub fn get(&self) -> &T {
        self.assert_owned();
        &self.data
    }

    /// Gets a mutable reference to the cells content.
    ///
    /// # Panics
    ///
    /// The current thread does not own the cell.
    #[inline]
    pub fn get_mut(&mut self) -> &mut T {
        self.assert_owned();
        &mut self.data
    }

    /// Tries to get an immutable reference to the cells content.
    /// Returns 'None' when the thread does not own the cell.
    #[inline]
    pub fn try_get(&self) -> Option<&T> {
        if self.is_owned() {
            Some(&self.data)
        } else {
            None
        }
    }

    /// Tries to get a mutable reference to the cells content.
    /// Returns 'None' when the thread does not own the cell.
    #[inline]
    pub fn try_get_mut(&mut self) -> Option<&mut T> {
        if self.is_owned() {
            Some(&mut self.data)
        } else {
            None
        }
    }

    /// Gets an immutable reference to the cells content without checking for ownership.
    ///
    /// # Safety
    ///
    /// This is always safe when the thread owns the cell, for example after a `acquire()`
    /// call.  When the current thread does not own the cell then it is only safe when T is a
    /// Sync type.
    // PLANNED: When specialization is available: 'fn is_sync<T>() -> bool' and debug_assert!(is_owned() || is_sync::<T>())
    #[inline]
    pub unsafe fn get_unchecked(&self) -> &T {
        debug_assert!(self.is_owned(), "Thread has no access to ThreadCell");
        &self.data
    }

    /// Gets an mutable reference to the cells content without checking for ownership.
    ///
    /// # Safety
    ///
    /// This is always safe when the thread owns the cell, for example after a `acquire()`
    /// call.  When the current thread does not own the cell then it is only safe when T is a
    /// Sync type.
    // PLANNED: When specialization is available: 'fn is_sync<T>() -> bool' and debug_assert!(is_owned() || is_sync::<T>())
    #[inline]
    pub unsafe fn get_mut_unchecked(&mut self) -> &mut T {
        &mut self.data
    }
}

/// Destroys a `ThreadCell`. The cell must be either owned by the current thread or disowned.
///
/// # Panics
///
/// Another thread owns the cell.
#[mutants::skip]
impl<T> Drop for ThreadCell<T> {
    // In debug builds we check first for ownership since dropping cells whose types do not
    // need dropping would still be a violation.
    #[cfg(debug_assertions)]
    fn drop(&mut self) {
        let owner = self.thread_id.load(Ordering::Acquire) & !GUARD_BIT;
        if owner == 0 || owner == current_thread_id() {
            if mem::needs_drop::<T>() {
                unsafe { ManuallyDrop::drop(&mut self.data) };
            }
        } else {
            panic!("Thread has no access to ThreadCell");
        }
    }

    // In release builds we can reverse the check to be slightly more efficient. The side
    // effect that dropping cells which one are not allowed to but don't need a destructor
    // either is safe and harmless anyway.
    #[cfg(not(debug_assertions))]
    fn drop(&mut self) {
        if mem::needs_drop::<T>() {
            let owner = self.thread_id.load(Ordering::Acquire) & !GUARD_BIT;
            if owner == 0 || owner == current_thread_id() {
                unsafe { ManuallyDrop::drop(&mut self.data) };
            } else {
                panic!("Thread has no access to ThreadCell");
            }
        }
    }
}

/// Creates a new owned `ThreadCell` from the given value.
impl<T> From<T> for ThreadCell<T> {
    #[inline]
    fn from(t: T) -> ThreadCell<T> {
        ThreadCell::new_owned(t)
    }
}

/// Clones a owned `ThreadCell`.
///
/// # Panics
///
/// Another thread owns the cell.
impl<T: Clone> Clone for ThreadCell<T> {
    #[inline]
    fn clone(&self) -> ThreadCell<T> {
        ThreadCell::new_owned(self.get().clone())
    }
}

/// Creates a new owned `ThreadCell` with the default constructed target value.
impl<T: Default> Default for ThreadCell<T> {
    #[inline]
    fn default() -> ThreadCell<T> {
        ThreadCell::new_owned(T::default())
    }
}

/// Check two `ThreadCells` for partial equality.
///
/// # Panics
///
/// Either cell is not owned by the current thread.
#[mutants::skip]
impl<T: PartialEq> PartialEq for ThreadCell<T> {
    #[inline]
    fn eq(&self, other: &ThreadCell<T>) -> bool {
        *self.get() == *other.get()
    }
}

impl<T: Eq> Eq for ThreadCell<T> {}

/// Comparison functions between `ThreadCells`.
///
/// # Panics
///
/// Either cell is not owned by the current thread.
#[mutants::skip]
impl<T: PartialOrd> PartialOrd for ThreadCell<T> {
    #[inline]
    fn partial_cmp(&self, other: &ThreadCell<T>) -> Option<cmp::Ordering> {
        self.get().partial_cmp(other.get())
    }

    #[inline]
    fn lt(&self, other: &ThreadCell<T>) -> bool {
        *self.get() < *other.get()
    }

    #[inline]
    fn le(&self, other: &ThreadCell<T>) -> bool {
        *self.get() <= *other.get()
    }

    #[inline]
    fn gt(&self, other: &ThreadCell<T>) -> bool {
        *self.get() > *other.get()
    }

    #[inline]
    fn ge(&self, other: &ThreadCell<T>) -> bool {
        *self.get() >= *other.get()
    }
}

/// Compare two `ThreadCells`.
///
/// # Panics
///
/// Either cell is not owned by the current thread.
#[mutants::skip]
impl<T: Ord> Ord for ThreadCell<T> {
    #[inline]
    fn cmp(&self, other: &ThreadCell<T>) -> cmp::Ordering {
        self.get().cmp(other.get())
    }
}

/// Formatted output of the value inside a `ThreadCell`.
///
/// # Panics
///
/// The cell is not owned by the current thread.
#[mutants::skip]
impl<T: fmt::Display> fmt::Display for ThreadCell<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        fmt::Display::fmt(self.get(), f)
    }
}

#[allow(clippy::doc_markdown)]
#[mutants::skip]
/// Debug information of a `ThreadCell`.
/// Prints "\<ThreadCell\>" when the current thread does not own the cell.
impl<T: fmt::Debug> fmt::Debug for ThreadCell<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match self.try_get() {
            Some(data) => f.debug_struct("ThreadCell").field("data", data).finish(),
            None => f.write_str("<ThreadCell>"),
        }
    }
}

#[cfg(not(feature = "nightly_thread_id_value"))]
use std::num::NonZeroU64;

/// A unique identifier for every thread.
#[cfg(not(feature = "nightly_thread_id_value"))]
struct ThreadId(NonZeroU64);

#[cfg(not(feature = "nightly_thread_id_value"))]
impl ThreadId {
    #[inline]
    #[must_use]
    #[mutants::skip]
    fn current() -> ThreadId {
        thread_local!(static THREAD_ID: NonZeroU64 = {
            static COUNTER: AtomicU64 = AtomicU64::new(1);
            {
                let id = NonZeroU64::new(COUNTER.fetch_add(1, Ordering::Relaxed)).unwrap();
                assert!(id.get() <= i64::MAX as u64, "more than i64::MAX threads");
                id
            }
        });
        THREAD_ID.with(|&x| ThreadId(x))
    }

    #[inline(always)]
    #[must_use]
    #[mutants::skip]
    fn as_u64(&self) -> NonZeroU64 {
        self.0
    }
}

#[test]
#[cfg(not(feature = "nightly_thread_id_value"))]
fn threadid() {
    let main = ThreadId::current().as_u64().get();
    let child = std::thread::spawn(|| ThreadId::current().as_u64().get())
        .join()
        .unwrap();

    // just info, actual values are unspecified
    println!("{main}, {child}");

    assert_ne!(main, 0);
    assert_ne!(main, child);
}

#[cfg(not(feature = "nightly_thread_id_value"))]
#[mutants::skip]
#[inline]
fn current_thread_id() -> u64 {
    ThreadId::current().as_u64().get()
}

#[cfg(feature = "nightly_thread_id_value")]
#[mutants::skip]
#[inline]
fn current_thread_id() -> u64 {
    std::thread::current().id().as_u64().get()
}

/// Guards that a referenced `ThreadCell` becomes properly released when its guard becomes
/// dropped. This covers releasing threadcells on panic.  Guards do not prevent the explicit
/// release of a `ThreadCell`. Deref a `Guard` referencing a released `ThreadCell` will panic!
#[repr(transparent)]
pub struct Guard<'a, T>(&'a ThreadCell<T>);

/// Releases the referenced `ThreadCell` when it is owned by the current thread.
impl<T> Drop for Guard<'_, T> {
    #[mutants::skip]
    fn drop(&mut self) {
        unsafe {
            // SAFETY: a guard is guaranteed to own the cell
            self.0.release_unchecked();
        }
    }
}

/// One can deref a `Guard` as long the `ThreadCell` is owned by the current thread this
/// should be the case as long the guarded `ThreadCell` got not explicitly released or stolen.
///
/// # Panics
///
/// When the underlying `ThreadCell` is not owned by the current thread.
impl<T> Deref for Guard<'_, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.0.get()
    }
}

/// Mutable Guard that ensures that a referenced `ThreadCell` becomes properly released when
/// it becomes dropped.  Guards do not prevent the explicit release of a `ThreadCell`. Deref a
/// `GuardMut` referencing a released `ThreadCell` will panic!
#[repr(transparent)]
pub struct GuardMut<'a, T>(&'a mut ThreadCell<T>);

/// Releases the referenced `ThreadCell` when it is owned by the current thread.
impl<T> Drop for GuardMut<'_, T> {
    fn drop(&mut self) {
        unsafe {
            // SAFETY: a guard is guaranteed to own the cell
            self.0.release_unchecked();
        }
    }
}

/// One can deref a `GuardMut` as long the `ThreadCell` is owned by the current thread this
/// should be the case as long the guarded `ThreadCell` got not explicitly released or stolen.
///
/// # Panics
///
/// When the underlying `ThreadCell` is not owned by the current thread.
impl<T> Deref for GuardMut<'_, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.0.get()
    }
}

impl<T> DerefMut for GuardMut<'_, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.0.get_mut()
    }
}