diff --git a/src/modem.rs b/src/modem.rs index a8e82a8..82b55f3 100644 --- a/src/modem.rs +++ b/src/modem.rs @@ -33,12 +33,12 @@ impl std::fmt::Display for ModemError { pub struct IterableRx { inner: Rx, - timeout: Option, + timeout: Duration, } impl IterableRx { fn reset(&mut self, timeout: Duration) -> &mut Self { - self.timeout = Some(timeout); + self.timeout = timeout; self } } @@ -50,19 +50,19 @@ impl Iterator for IterableRx { /// interest, so the retry mechanism is triggered on _any_ error every 200ms until a byte is /// received, or the timeout is reached. fn next(&mut self) -> Option { - let now = Instant::now(); + let start = Instant::now(); loop { - let timeout = self.timeout.unwrap_or(Duration::from_millis(0)); match self.inner.read() { Ok(b) => { + self.timeout = self.timeout.saturating_sub(start.elapsed()); break Some(b) }, - _ => { - if now.elapsed() > timeout { + Err(_) => { + if start.elapsed() > self.timeout { + self.timeout = Duration::from_millis(0); break None } thread::sleep(Duration::from_millis(200)); - self.timeout = Some(timeout.saturating_sub(now.elapsed())); } } } @@ -72,7 +72,7 @@ impl Iterator for IterableRx { impl Modem { pub fn new(tx: Tx, rx: Rx) -> Self { Self { - rx: IterableRx { inner: rx, timeout: None }, + rx: IterableRx { inner: rx, timeout: Duration::from_millis(0) }, tx, } }