simple_git/
cancellation_token.rs1use std::sync::{
2    atomic::{AtomicBool, Ordering::Relaxed},
3    Arc,
4};
5
6use derive_destructure2::destructure;
7
8#[derive(Clone, Debug, Default)]
10pub struct GitCancellationToken(Arc<AtomicBool>);
11
12impl GitCancellationToken {
13    #[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    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#[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    pub fn disarm(self) -> GitCancellationToken {
42        self.destructure().0
43    }
44}