use futures_util::future::BoxFuture;
use futures_util::stream::FuturesUnordered;
use futures_util::{ready, Stream};
use pin_project::pin_project;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
#[pin_project]
pub struct ShutdownHooks {
#[pin]
hooks: FuturesUnordered<BoxFuture<'static, ()>>,
}
impl ShutdownHooks {
pub fn new() -> Self {
ShutdownHooks {
hooks: FuturesUnordered::new(),
}
}
#[allow(dead_code)]
pub fn push<F>(&mut self, future: F)
where
F: Future<Output = ()> + 'static + Send,
{
self.hooks.push(Box::pin(future));
}
}
impl Future for ShutdownHooks {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
while let Some(()) = ready!(self.as_mut().project().hooks.poll_next(cx)) {}
Poll::Ready(())
}
}