use core::{any::Provider, future::Future};
use std::time::{Duration, Instant};
use crate::{with_ref, ProviderFut, ProviderFutExt};
pub use shutdown::{ShutdownReceiver, ShutdownSender};
#[cfg(feature = "time")]
pub use shutdown::time::{run_until_signal, SignalOrComplete};
mod shutdown;
pub trait WellKnownProviderExt: Future + Sized {
fn with_timeout(self, duration: Duration) -> ProviderFut<Self, Deadline> {
self.with_deadline(Instant::now() + duration)
}
fn with_deadline(self, deadline: Instant) -> ProviderFut<Self, Deadline> {
self.provide(Deadline(deadline))
}
fn with_shutdown_handler(
self,
handler: ShutdownReceiver,
) -> ProviderFut<Self, ShutdownReceiver> {
self.provide(handler)
}
}
impl<F: Future> WellKnownProviderExt for F {}
#[derive(Debug, Clone, Copy)]
pub struct Deadline(pub Instant);
impl Provider for Deadline {
fn provide<'a>(&'a self, demand: &mut core::any::Demand<'a>) {
demand.provide_ref(self);
}
}
impl Deadline {
pub async fn get() -> Option<Instant> {
with_ref(|Deadline(deadline)| *deadline).await
}
}