use std::future::Future;
use std::pin::Pin;
use std::task::Context;
use tokio_util::sync::{CancellationToken, WaitForCancellationFutureOwned};
#[derive(Debug, Clone, Default)]
pub struct StreamDrain {
token: CancellationToken,
}
impl StreamDrain {
pub fn end_streams(&self) {
self.token.cancel();
}
#[must_use]
pub fn is_ending(&self) -> bool {
self.token.is_cancelled()
}
#[must_use]
pub fn watch(&self) -> StreamDrainWatch {
StreamDrainWatch {
ended: Box::pin(self.token.clone().cancelled_owned()),
}
}
}
#[derive(Debug)]
pub struct StreamDrainWatch {
ended: Pin<Box<WaitForCancellationFutureOwned>>,
}
impl StreamDrainWatch {
pub fn poll_ended(&mut self, cx: &mut Context<'_>) -> bool {
self.ended.as_mut().poll(cx).is_ready()
}
}