1 #![allow(dead_code)]
2
3 extern crate alloc;
4
5 #[cfg(not(loom))]
6 use alloc::sync::Arc;
7 #[cfg(not(loom))]
8 use core::sync::atomic::{AtomicUsize, Ordering::SeqCst};
9 #[cfg(loom)]
10 use loom::sync::{
11 atomic::{AtomicUsize, Ordering::SeqCst},
12 Arc,
13 };
14
15 #[cfg(loom)]
16 pub mod waker;
17
maybe_loom_model(test: impl Fn() + Sync + Send + 'static)18 pub fn maybe_loom_model(test: impl Fn() + Sync + Send + 'static) {
19 #[cfg(loom)]
20 loom::model(test);
21 #[cfg(not(loom))]
22 test();
23 }
24
25 pub struct DropCounter<T> {
26 drop_count: Arc<AtomicUsize>,
27 value: Option<T>,
28 }
29
30 pub struct DropCounterHandle(Arc<AtomicUsize>);
31
32 impl<T> DropCounter<T> {
new(value: T) -> (Self, DropCounterHandle)33 pub fn new(value: T) -> (Self, DropCounterHandle) {
34 let drop_count = Arc::new(AtomicUsize::new(0));
35 (
36 Self {
37 drop_count: drop_count.clone(),
38 value: Some(value),
39 },
40 DropCounterHandle(drop_count),
41 )
42 }
43
value(&self) -> &T44 pub fn value(&self) -> &T {
45 self.value.as_ref().unwrap()
46 }
47
into_value(mut self) -> T48 pub fn into_value(mut self) -> T {
49 self.value.take().unwrap()
50 }
51 }
52
53 impl DropCounterHandle {
count(&self) -> usize54 pub fn count(&self) -> usize {
55 self.0.load(SeqCst)
56 }
57 }
58
59 impl<T> Drop for DropCounter<T> {
drop(&mut self)60 fn drop(&mut self) {
61 self.drop_count.fetch_add(1, SeqCst);
62 }
63 }
64