use std::time::{Duration, Instant};
use futures::future::BoxFuture;
use crate::addr_of_boxed_future;
#[derive(Debug, Clone)]
pub struct ScheduledTimeoutIdentifier {
run_at: Instant,
boxed_future_addr: usize,
}
impl ScheduledTimeoutIdentifier {
pub fn new(run_at: Instant, boxed_future: &BoxFuture<'static, ()>) -> Self {
Self {
run_at,
boxed_future_addr: addr_of_boxed_future(boxed_future),
}
}
pub fn get_delay(&self, min_sleep_duration: Duration) -> Option<Duration> {
let now = Instant::now();
if self.run_at < now + min_sleep_duration {
None
} else {
Some(self.run_at - now)
}
}
}
impl PartialEq for ScheduledTimeoutIdentifier {
fn eq(&self, other: &Self) -> bool {
self.run_at.eq(&other.run_at) && self.boxed_future_addr.eq(&other.boxed_future_addr)
}
}
impl Eq for ScheduledTimeoutIdentifier {}
impl PartialOrd for ScheduledTimeoutIdentifier {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(
self.run_at
.cmp(&other.run_at)
.then(self.boxed_future_addr.cmp(&other.boxed_future_addr)),
)
}
}
impl Ord for ScheduledTimeoutIdentifier {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.run_at
.cmp(&other.run_at)
.then(self.boxed_future_addr.cmp(&other.boxed_future_addr))
}
}