summaryrefslogtreecommitdiffstats
path: root/third_party/rust/oneshot-uniffi/tests/helpers/mod.rs
blob: 1b145396e7bd796ca998a4868ace97a3ecdfb824 (plain)
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
#![allow(dead_code)]

extern crate alloc;

#[cfg(not(loom))]
use alloc::sync::Arc;
#[cfg(not(loom))]
use core::sync::atomic::{AtomicUsize, Ordering::SeqCst};
#[cfg(loom)]
use loom::sync::{
    atomic::{AtomicUsize, Ordering::SeqCst},
    Arc,
};

#[cfg(loom)]
pub mod waker;

pub fn maybe_loom_model(test: impl Fn() + Sync + Send + 'static) {
    #[cfg(loom)]
    loom::model(test);
    #[cfg(not(loom))]
    test();
}

pub struct DropCounter<T> {
    drop_count: Arc<AtomicUsize>,
    value: Option<T>,
}

pub struct DropCounterHandle(Arc<AtomicUsize>);

impl<T> DropCounter<T> {
    pub fn new(value: T) -> (Self, DropCounterHandle) {
        let drop_count = Arc::new(AtomicUsize::new(0));
        (
            Self {
                drop_count: drop_count.clone(),
                value: Some(value),
            },
            DropCounterHandle(drop_count),
        )
    }

    pub fn value(&self) -> &T {
        self.value.as_ref().unwrap()
    }

    pub fn into_value(mut self) -> T {
        self.value.take().unwrap()
    }
}

impl DropCounterHandle {
    pub fn count(&self) -> usize {
        self.0.load(SeqCst)
    }
}

impl<T> Drop for DropCounter<T> {
    fn drop(&mut self) {
        self.drop_count.fetch_add(1, SeqCst);
    }
}