quota 0.3.2

Fastest Lane-parallel Rate-limiter for Rust
Documentation
use num_traits::AsPrimitive;

/// A refill rate normalized to tokens per nanosecond.
///
/// Constructors are named by the time unit callers usually think in. For example,
/// `RefillRate::per_sec(10)` means ten tokens per second.
#[derive(Copy, Clone, Debug)]
pub struct RefillRate(pub(crate) f64);

impl RefillRate {
    /// Returns a refill rate large enough to saturate practical quota capacities immediately.
    #[inline]
    pub fn max() -> Self {
        Self(f64::MAX)
    }

    #[inline]
    /// Creates a rate in tokens per nanosecond.
    pub fn per_nano<T>(refill_rate_per_second: T) -> Self
    where
        T: AsPrimitive<f64>,
    {
        Self(refill_rate_per_second.as_())
    }
    #[inline]
    /// Creates a rate in tokens per microsecond.
    pub fn per_micro<T>(refill_rate_per_second: T) -> Self
    where
        T: AsPrimitive<f64>,
    {
        Self(refill_rate_per_second.as_() / 1_000.0)
    }
    #[inline]
    /// Creates a rate in tokens per millisecond.
    pub fn per_milli<T>(refill_rate_per_second: T) -> Self
    where
        T: AsPrimitive<f64>,
    {
        Self(refill_rate_per_second.as_() / 1_000_000.0)
    }
    #[inline]
    /// Creates a rate in tokens per second.
    pub fn per_sec<T>(refill_rate_per_second: T) -> Self
    where
        T: AsPrimitive<f64>,
    {
        Self(refill_rate_per_second.as_() / 1_000_000_000.0)
    }

    #[inline]
    /// Creates a rate in tokens per minute.
    pub fn per_minute<T>(refill_rate_per_second: T) -> Self
    where
        T: AsPrimitive<f64>,
    {
        Self(refill_rate_per_second.as_() / 60_000_000_000.0)
    }

    #[inline]
    /// Creates a rate in tokens per hour.
    pub fn per_hour<T>(refill_rate_per_second: T) -> Self
    where
        T: AsPrimitive<f64>,
    {
        Self(refill_rate_per_second.as_() / 3600_000_000_000.0)
    }

    #[inline]
    /// Creates a rate in tokens per day.
    pub fn per_day<T>(refill_rate_per_second: T) -> Self
    where
        T: AsPrimitive<f64>,
    {
        Self(refill_rate_per_second.as_() / 86400_000_000_000.0)
    }

    #[inline]
    /// Creates a rate in tokens per week.
    pub fn per_week<T>(refill_rate_per_second: T) -> Self
    where
        T: AsPrimitive<f64>,
    {
        Self(refill_rate_per_second.as_() / 604800_000_000_000.0)
    }
}