use futures::{
channel::oneshot,
future::{FusedFuture, Shared},
FutureExt,
};
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
#[derive(Clone)]
pub struct Shutdown(Shared<oneshot::Receiver<()>>);
impl Future for Shutdown {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let pin = self.get_mut();
if pin.0.is_terminated() || pin.0.poll_unpin(cx).is_ready() {
Poll::Ready(())
} else {
Poll::Pending
}
}
}
pub struct Signal(oneshot::Sender<()>);
impl Signal {
pub fn fire(self) -> Result<(), ()> {
self.0.send(())
}
}
pub fn signal() -> (Signal, Shutdown) {
let (sender, receiver) = oneshot::channel();
(Signal(sender), Shutdown(receiver.shared()))
}