use usb_pd::{Duration, Instant};
pub struct Timeout {
now: Instant,
expiry: Option<Instant>,
}
impl Timeout {
pub fn new() -> Self {
Self {
now: Instant::from_ticks(0),
expiry: None,
}
}
pub fn update(&mut self, now: Instant) {
self.now = now;
}
pub fn start(&mut self, duration: Duration) {
self.expiry = Some(self.now.checked_add_duration(duration).unwrap());
}
pub fn cancel(&mut self) {
self.expiry = None;
}
pub fn is_expired(&mut self) -> bool {
let Some(timeout) = self.expiry else {
return false;
};
let expired = timeout <= self.now;
if expired {
self.cancel();
}
expired
}
}