Skip to main content

cloud_sdk/retry/
time.rs

1//! Monotonic retry-budget values, distinct from wall-clock observations.
2
3/// Caller-observed monotonic duration in implementation-defined ticks.
4#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
5pub struct MonotonicDuration(u64);
6
7impl MonotonicDuration {
8    /// Creates a duration in the caller's consistent monotonic tick unit.
9    #[must_use]
10    pub const fn new(ticks: u64) -> Self {
11        Self(ticks)
12    }
13
14    /// Returns the caller-defined monotonic ticks.
15    #[must_use]
16    pub const fn get(self) -> u64 {
17        self.0
18    }
19}
20
21/// Caller-observed monotonic instant in implementation-defined ticks.
22#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
23pub struct MonotonicInstant(u64);
24
25impl MonotonicInstant {
26    /// Creates an instant in the same tick domain used by retry durations.
27    #[must_use]
28    pub const fn new(ticks: u64) -> Self {
29        Self(ticks)
30    }
31
32    /// Returns the caller-defined monotonic ticks.
33    #[must_use]
34    pub const fn get(self) -> u64 {
35        self.0
36    }
37
38    pub(crate) const fn checked_duration_since(self, earlier: Self) -> Option<MonotonicDuration> {
39        match self.0.checked_sub(earlier.0) {
40            Some(value) => Some(MonotonicDuration::new(value)),
41            None => None,
42        }
43    }
44
45    pub(crate) const fn checked_add(self, duration: MonotonicDuration) -> Option<Self> {
46        match self.0.checked_add(duration.get()) {
47            Some(value) => Some(Self(value)),
48            None => None,
49        }
50    }
51}