deadpool/managed/
dropguard.rs

1/// This structure calls a function/closure when it is dropped.
2/// The [`DropGuard::disarm`] method stops this from happening.
3pub(crate) struct DropGuard<F: Fn()>(pub(crate) F);
4
5impl<F: Fn()> DropGuard<F> {
6    pub(crate) fn disarm(self) {
7        std::mem::forget(self)
8    }
9}
10
11impl<F> Drop for DropGuard<F>
12where
13    F: Fn(),
14{
15    fn drop(&mut self) {
16        (self.0)()
17    }
18}
19
20#[test]
21fn test_dropguard_drop() {
22    use std::sync::atomic::{AtomicUsize, Ordering};
23    let count = AtomicUsize::new(0);
24    assert_eq!(count.load(Ordering::Relaxed), 0);
25    {
26        let _ = count.fetch_add(1, Ordering::Relaxed);
27        let _ = DropGuard(|| {
28            let _ = count.fetch_sub(1, Ordering::Relaxed);
29        });
30    }
31    assert_eq!(count.load(Ordering::Relaxed), 0);
32}
33
34#[test]
35fn test_dropguard_disarm() {
36    use std::sync::atomic::{AtomicUsize, Ordering};
37    let count = AtomicUsize::new(0);
38    assert_eq!(count.load(Ordering::Relaxed), 0);
39    {
40        let _ = count.fetch_add(1, Ordering::Relaxed);
41        let guard = DropGuard(|| {
42            let _ = count.fetch_sub(1, Ordering::Relaxed);
43        });
44        guard.disarm();
45    }
46    assert_eq!(count.load(Ordering::Relaxed), 1);
47}