1use tokio::sync::watch;
4
5pub struct Shutdown {
7 rx: watch::Receiver<bool>,
8}
9
10pub struct ShutdownHandle {
12 tx: watch::Sender<bool>,
13}
14
15impl Shutdown {
16 pub fn new() -> (ShutdownHandle, Shutdown) {
18 let (tx, rx) = watch::channel(false);
19 (ShutdownHandle { tx }, Shutdown { rx })
20 }
21
22 pub async fn cancelled(&mut self) {
24 if self.is_cancelled() {
25 return;
26 }
27 let _ = self.rx.changed().await;
29 }
30
31 pub fn is_cancelled(&self) -> bool {
32 *self.rx.borrow()
33 }
34}
35
36impl ShutdownHandle {
37 pub fn shutdown(&self) {
39 self.tx.send_replace(true);
40 }
41}
42
43#[cfg(test)]
44mod tests;