use std::marker::PhantomData;
use std::time::{Duration, Instant};
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub struct Attempt<'a> {
number: u32,
start: Instant,
now: Instant,
_borrow: PhantomData<&'a ()>,
}
impl<'a> Attempt<'a> {
pub(crate) fn new(number: u32, start: Instant, now: Instant) -> Self {
Self {
number,
start,
now,
_borrow: PhantomData,
}
}
pub fn number(&self) -> u32 {
self.number
}
pub fn elapsed(&self) -> Duration {
self.now.duration_since(self.start)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn number_is_one_based() {
let t = Instant::now();
let a = Attempt::new(1, t, t);
assert_eq!(a.number(), 1);
}
#[test]
fn elapsed_reflects_clock() {
let start = Instant::now();
let now = start + Duration::from_millis(250);
let a = Attempt::new(2, start, now);
assert_eq!(a.elapsed(), Duration::from_millis(250));
}
}