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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use Duration;
/// An instant in time, used for measuring elapsed durations.
///
/// This trait abstracts over the time source so that users can provide their
/// own clock implementations (e.g., for simulated time in tests, or for
/// integration with async runtimes that provide their own `Instant` type).
///
/// The crate never calls "now" internally — callers always pass a timestamp
/// into every method. This trait only needs to support computing the elapsed
/// time between two instants and advancing an instant by a duration.
///
/// # Built-in implementation
///
/// [`std::time::Instant`] implements this trait, so the crate works out of
/// the box with the standard library clock. A type alias
/// [`LatencyTracker`](crate::LatencyTracker) defaults the clock parameter to
/// `Instant` for ergonomic use.
///
/// # Example: custom clock
///
/// ```rust
/// use std::time::Duration;
/// use adaptive_timeout::Instant;
///
/// #[derive(Clone, Copy)]
/// struct FakeInstant(u64); // nanoseconds
///
/// impl Instant for FakeInstant {
/// fn duration_since(&self, earlier: Self) -> Duration {
/// Duration::from_nanos(self.0.saturating_sub(earlier.0))
/// }
/// fn add_duration(&self, duration: Duration) -> Self {
/// FakeInstant(self.0 + duration.as_nanos() as u64)
/// }
/// }
/// ```