1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#![cfg(not(feature = "no_std"))] use crate::{Waiter, WaiterError}; use std::time::{Duration, Instant}; #[derive(Clone)] pub struct TimeoutWaiter { timeout: Duration, start: Option<Instant>, } impl TimeoutWaiter { pub fn new(timeout: Duration) -> Self { Self { timeout, start: None, } } } impl Waiter for TimeoutWaiter { fn restart(&mut self) -> Result<(), WaiterError> { let _ = self.start.ok_or(WaiterError::NotStarted)?; self.start = Some(Instant::now()); Ok(()) } fn start(&mut self) { self.start = Some(Instant::now()); } fn wait(&mut self) -> Result<(), WaiterError> { let start = self.start.ok_or(WaiterError::NotStarted)?; if start.elapsed() > self.timeout { Err(WaiterError::Timeout) } else { Ok(()) } } }