atomic-circuit-breaker 11.2.0

Zero-dependency, lock-free atomic sliding-window circuit breaker with CAS probe gating, monotonic clock support, and RAII guards.
Documentation
use super::*;

/// Default construction capturing the current monotonic instant.
impl Default for CircuitClock {
    /// Creates a `CircuitClock` anchored at `Instant::now()`.
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}

/// Core monotonic timing operations for `CircuitClock`.
impl CircuitClock {
    /// Creates a new monotonic clock anchored at the current instant.
    #[inline(always)]
    pub fn new() -> Self {
        Self {
            origin: Instant::now(),
        }
    }

    /// Creates a monotonic clock anchored at a caller-supplied instant.
    ///
    /// # Arguments
    ///
    /// - `Instant` - The monotonic origin instant.
    #[inline(always)]
    pub fn from_instant(origin: Instant) -> Self {
        Self { origin }
    }

    /// Returns the monotonic origin instant.
    ///
    /// # Arguments
    ///
    /// - `&Self` - The monotonic clock.
    ///
    /// # Returns
    ///
    /// - `Instant` - The origin timestamp.
    #[inline(always)]
    pub fn get_origin(&self) -> Instant {
        self.origin
    }

    /// Returns elapsed nanoseconds since clock creation, saturating at `u64::MAX`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - The monotonic clock.
    ///
    /// # Returns
    ///
    /// - `u64` - Elapsed monotonic nanoseconds.
    #[inline(always)]
    pub fn now_ns(&self) -> u64 {
        let elapsed: Duration = Instant::now().saturating_duration_since(self.get_origin());
        elapsed
            .as_secs()
            .saturating_mul(NANOS_PER_SEC)
            .saturating_add(u64::from(elapsed.subsec_nanos()))
    }
}