anvil/
shutdown.rs
1use futures::{
4 channel::oneshot,
5 future::{FusedFuture, Shared},
6 FutureExt,
7};
8use std::{
9 future::Future,
10 pin::Pin,
11 task::{Context, Poll},
12};
13
14#[derive(Clone)]
16pub struct Shutdown(Shared<oneshot::Receiver<()>>);
17
18impl Future for Shutdown {
19 type Output = ();
20
21 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
22 let pin = self.get_mut();
23 if pin.0.is_terminated() || pin.0.poll_unpin(cx).is_ready() {
24 Poll::Ready(())
25 } else {
26 Poll::Pending
27 }
28 }
29}
30
31pub struct Signal(oneshot::Sender<()>);
33
34impl Signal {
35 pub fn fire(self) -> Result<(), ()> {
37 self.0.send(())
38 }
39}
40
41pub fn signal() -> (Signal, Shutdown) {
43 let (sender, receiver) = oneshot::channel();
44 (Signal(sender), Shutdown(receiver.shared()))
45}