simple_git/
cancellation_token.rs

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
use std::sync::{
    atomic::{AtomicBool, Ordering::Relaxed},
    Arc,
};

use derive_destructure2::destructure;

/// Token that can be used to cancel git operation.
#[derive(Clone, Debug, Default)]
pub struct GitCancellationToken(Arc<AtomicBool>);

impl GitCancellationToken {
    /// Create a guard that cancel the git operation on drop.
    #[must_use = "You must assign the guard to a variable, \
otherwise it is equivalent to `GitCancellationToken::cancel()`"]
    pub fn cancel_on_drop(self) -> GitCancelOnDrop {
        GitCancelOnDrop(self)
    }

    /// Cancel the git operation.
    pub fn cancel(&self) {
        self.0.store(true, Relaxed)
    }

    pub(super) fn get_atomic(&self) -> &AtomicBool {
        &self.0
    }
}

/// Guard used to cancel git operation on drop
#[derive(Debug, destructure)]
pub struct GitCancelOnDrop(GitCancellationToken);

impl Drop for GitCancelOnDrop {
    fn drop(&mut self) {
        self.0.cancel()
    }
}
impl GitCancelOnDrop {
    /// Disarm the guard, return the token.
    pub fn disarm(self) -> GitCancellationToken {
        self.destructure().0
    }
}