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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Blocking {
Void,
Pending,
Once,
Always,
}
pub trait BlockingArbiter {
fn can_block(&mut self) -> bool;
fn state(&self) -> Blocking;
}
#[derive(Debug, Default)]
pub struct LenientArbiter;
impl BlockingArbiter for LenientArbiter {
fn can_block(&mut self) -> bool {
true
}
fn state(&self) -> Blocking {
Blocking::Always
}
}
#[derive(Debug)]
pub struct StatefulArbiter {
state: Blocking
}
impl Default for StatefulArbiter {
fn default() -> Self {
StatefulArbiter { state: Blocking::Void }
}
}
impl StatefulArbiter {
pub(crate) fn set(&mut self, state: Blocking) {
self.state = state;
}
}
impl BlockingArbiter for StatefulArbiter {
fn can_block(&mut self) -> bool {
match self.state {
Blocking::Void => {
self.state = Blocking::Pending;
false
}
Blocking::Pending => {
false
}
Blocking::Once => {
self.state = Blocking::Void;
true
}
Blocking::Always => {
true
}
}
}
fn state(&self) -> Blocking {
self.state
}
}