use crate::TaskSet;
use tokio_util::{sync::WaitForCancellationFuture, task::task_tracker::TaskTrackerWaitFuture};
#[derive(Debug)]
pub struct ServerShutdown {
pub(crate) task_set: TaskSet,
}
impl From<TaskSet> for ServerShutdown {
fn from(task_set: TaskSet) -> Self {
Self::new(task_set)
}
}
impl ServerShutdown {
pub(crate) const fn new(task_set: TaskSet) -> Self {
Self { task_set }
}
pub fn wait(&self) -> TaskTrackerWaitFuture<'_> {
self.task_set.wait()
}
pub fn close(&self) {
self.task_set.close();
}
pub fn is_closed(&self) -> bool {
self.task_set.is_closed()
}
pub fn cancel(&self) {
self.task_set.cancel();
}
pub fn is_cancelled(&self) -> bool {
self.task_set.is_cancelled()
}
pub fn cancelled(&self) -> WaitForCancellationFuture<'_> {
self.task_set.cancelled()
}
pub async fn shutdown(self) {
self.task_set.cancel();
self.close();
self.wait().await;
}
}
impl Drop for ServerShutdown {
fn drop(&mut self) {
self.task_set.cancel();
}
}