thedes_tui/
cancellability.rs1use std::ops::Not;
2
3pub trait Cancellation<I> {
4 type Output;
5
6 fn is_cancellable(&self) -> bool;
7
8 fn is_cancelling(&self) -> bool;
9
10 fn set_cancelling(&mut self, is_it: bool);
11
12 fn make_output(&self, item: I) -> Self::Output;
13}
14
15#[derive(Debug, Clone, Copy, PartialEq, Default)]
16pub struct NonCancellable;
17
18impl<I> Cancellation<I> for NonCancellable {
19 type Output = I;
20
21 fn is_cancellable(&self) -> bool {
22 false
23 }
24
25 fn is_cancelling(&self) -> bool {
26 false
27 }
28
29 fn set_cancelling(&mut self, _is_it: bool) {}
30
31 fn make_output(&self, item: I) -> Self::Output {
32 item
33 }
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Default)]
37pub struct Cancellable {
38 is_cancelling: bool,
39}
40
41impl Cancellable {
42 pub fn new(initial_cancelling: bool) -> Self {
43 Self { is_cancelling: initial_cancelling }
44 }
45}
46
47impl<I> Cancellation<I> for Cancellable {
48 type Output = Option<I>;
49
50 fn is_cancellable(&self) -> bool {
51 true
52 }
53
54 fn is_cancelling(&self) -> bool {
55 self.is_cancelling
56 }
57
58 fn set_cancelling(&mut self, is_it: bool) {
59 self.is_cancelling = is_it;
60 }
61
62 fn make_output(&self, item: I) -> Self::Output {
63 self.is_cancelling.not().then_some(item)
64 }
65}