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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use std::cell::RefCell;
use std::future::Future;
use std::rc::Rc;
use futures::channel::oneshot;
use futures::future::FutureExt;
use futures::future::LocalBoxFuture;
use futures::stream::FuturesUnordered;
use futures::stream::StreamExt;
use crate::applicable::Identity;
use crate::applicable::Projection;
use crate::error::ShutDown;
use crate::state::OutcomeOf;
use crate::state::State;
#[derive(Clone)]
pub struct Commits {
waker: std::task::Waker,
inner: Rc<CommitsInner>,
}
struct CommitsInner {
submitted: RefCell<Vec<LocalBoxFuture<'static, ()>>>,
active: RefCell<FuturesUnordered<LocalBoxFuture<'static, ()>>>,
}
impl Commits {
pub fn new() -> Self {
Self {
waker: futures::task::noop_waker(),
inner: Rc::new(CommitsInner {
submitted: RefCell::new(Vec::new()),
active: RefCell::new(FuturesUnordered::new()),
}),
}
}
pub fn submit<F: 'static + Future<Output = ()>>(&self, commit: F) {
self.inner.submitted.borrow_mut().push(commit.boxed_local());
self.waker.wake_by_ref();
}
pub fn poll_next(&mut self, cx: &mut std::task::Context<'_>) -> std::task::Poll<Option<()>> {
if !cx.waker().will_wake(&self.waker) {
self.waker = cx.waker().clone();
}
loop {
self.inner
.active
.borrow_mut()
.extend(self.inner.submitted.borrow_mut().drain(..));
let result = self.inner.active.borrow_mut().poll_next_unpin(cx);
if self.inner.submitted.borrow().is_empty() {
return result;
}
}
}
}
impl std::fmt::Debug for Commits {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Commits {{ submitted: {}, active: {} }}",
self.inner.submitted.borrow().len(),
self.inner.active.borrow().len(),
)
}
}
#[derive(Debug)]
pub struct Commit<S: State, R, P = Identity> {
round_num: R,
receiver: oneshot::Receiver<(R, OutcomeOf<S>)>,
_projection: std::marker::PhantomData<P>,
}
impl<S: State, R: Copy> Commit<S, R> {
pub(crate) fn new(round_num: R, receiver: oneshot::Receiver<(R, OutcomeOf<S>)>) -> Self {
Self {
round_num,
receiver,
_projection: std::marker::PhantomData,
}
}
pub(crate) fn ready(round_num: R, outcome: OutcomeOf<S>) -> Self {
let (send, recv) = futures::channel::oneshot::channel();
let _ = send.send((round_num, outcome));
Self {
round_num,
receiver: recv,
_projection: std::marker::PhantomData,
}
}
pub fn projected<P: Projection<OutcomeOf<S>>>(self) -> Commit<S, R, P> {
Commit {
round_num: self.round_num,
receiver: self.receiver,
_projection: std::marker::PhantomData,
}
}
}
impl<S: State, R: Copy, P> Commit<S, R, P> {
pub fn round_num(&self) -> R {
self.round_num
}
}
impl<S, R, P> Future for Commit<S, R, P>
where
S: State,
R: crate::RoundNum,
P: Projection<OutcomeOf<S>>,
{
type Output = Result<<P as Projection<OutcomeOf<S>>>::Projected, ShutDown>;
fn poll(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
self.receiver.poll_unpin(cx).map(|r| {
r.map(|(_, outcome)| P::project(outcome))
.map_err(|oneshot::Canceled| ShutDown)
})
}
}