simple_git/
cancellation_token.rs

1use std::sync::{
2    atomic::{AtomicBool, Ordering::Relaxed},
3    Arc,
4};
5
6use derive_destructure2::destructure;
7
8/// Token that can be used to cancel git operation.
9#[derive(Clone, Debug, Default)]
10pub struct GitCancellationToken(Arc<AtomicBool>);
11
12impl GitCancellationToken {
13    /// Create a guard that cancel the git operation on drop.
14    #[must_use = "You must assign the guard to a variable, \
15otherwise it is equivalent to `GitCancellationToken::cancel()`"]
16    pub fn cancel_on_drop(self) -> GitCancelOnDrop {
17        GitCancelOnDrop(self)
18    }
19
20    /// Cancel the git operation.
21    pub fn cancel(&self) {
22        self.0.store(true, Relaxed)
23    }
24
25    pub(super) fn get_atomic(&self) -> &AtomicBool {
26        &self.0
27    }
28}
29
30/// Guard used to cancel git operation on drop
31#[derive(Debug, destructure)]
32pub struct GitCancelOnDrop(GitCancellationToken);
33
34impl Drop for GitCancelOnDrop {
35    fn drop(&mut self) {
36        self.0.cancel()
37    }
38}
39impl GitCancelOnDrop {
40    /// Disarm the guard, return the token.
41    pub fn disarm(self) -> GitCancellationToken {
42        self.destructure().0
43    }
44}