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::*;

/// Formats `CircuitBreakerError` variants as human-readable descriptions.
impl fmt::Display for CircuitBreakerError {
    /// Writes the error description to the formatter.
    ///
    /// # Arguments
    ///
    /// - `&Self` - The error variant.
    /// - `&mut fmt::Formatter<'_>` - The target formatter.
    ///
    /// # Returns
    ///
    /// - `fmt::Result` - Result of formatting.
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NoTripCondition => f.write_str(ERR_NO_TRIP_CONDITION),
            Self::InvalidThreshold => f.write_str(ERR_INVALID_THRESHOLD),
            Self::InvalidFailureRate => f.write_str(ERR_INVALID_FAILURE_RATE),
            Self::ZeroWindow => f.write_str(ERR_ZERO_WINDOW),
            Self::ZeroCooldown => f.write_str(ERR_ZERO_COOLDOWN),
            Self::InvalidHalfOpenConfig => f.write_str(ERR_INVALID_HALF_OPEN),
            Self::DurationOverflow => f.write_str(ERR_DURATION_OVERFLOW),
        }
    }
}

/// Marks `CircuitBreakerError` as a standard error type.
impl Error for CircuitBreakerError {}

/// Convenience predicates for `CallError`.
impl<E> CallError<E> {
    /// Returns `true` when the call was rejected by the circuit breaker without running.
    ///
    /// # Arguments
    ///
    /// - `&Self` - The call error.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` for `CallError::Rejected`.
    #[inline(always)]
    pub fn is_rejected(&self) -> bool {
        matches!(self, Self::Rejected { .. })
    }

    /// Consumes the error and returns the inner error if the operation ran and failed.
    ///
    /// # Arguments
    ///
    /// - `Self` - The call error.
    ///
    /// # Returns
    ///
    /// - `Option<E>` - `Some(inner)` for `CallError::Inner`, `None` for `Rejected`.
    #[inline(always)]
    pub fn into_inner(self) -> Option<E> {
        match self {
            Self::Inner(err) => Some(err),
            Self::Rejected { .. } => None,
        }
    }
}

/// Formats `CallError` for diagnostics.
impl<E> fmt::Display for CallError<E>
where
    E: fmt::Display,
{
    /// Writes a human-readable summary of the call error.
    ///
    /// # Arguments
    ///
    /// - `&Self` - The call error.
    /// - `&mut fmt::Formatter<'_>` - The target formatter.
    ///
    /// # Returns
    ///
    /// - `fmt::Result` - Result of formatting.
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Rejected {
                state,
                retry_after_ns,
            } => write!(
                f,
                "{ERR_CALL_REJECTED} (state={state}, retry_after_ns={retry_after_ns})"
            ),
            Self::Inner(err) => fmt::Display::fmt(err, f),
        }
    }
}

/// Marks `CallError<E>` as a standard error when `E` implements `Error`.
impl<E> Error for CallError<E> where E: Error + 'static {}