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
use super::ShutdownReceiver;
use futures::future::Either;
use std::future::Future;
#[derive(Debug)]
pub struct AttachedShutdown<F> {
pub(super) receiver: ShutdownReceiver,
pub(super) future: F,
}
impl<F, Out> AttachedShutdown<F>
where
F: Future<Output = Out>,
{
pub async fn on_shutdown<S>(self, shutdown: S) -> Out
where
S: Future<Output = Out>,
{
match self.wait().await {
Either::Left(_) => shutdown.await,
Either::Right(out) => out,
}
}
pub async fn wait(self) -> Either<(), Out> {
let Self { receiver, future } = self;
let cancel = receiver.wait();
futures::pin_mut!(cancel);
futures::pin_mut!(future);
match futures::future::select(cancel, future).await {
Either::Left(_) => Either::Left(()),
Either::Right((out, _)) => Either::Right(out),
}
}
pub async fn drop_on_shutdown(self) {
let _ = self.wait().await;
}
}