pub struct Rate { /* private fields */ }Expand description
A rate: units per per period.
Units are dimensionless: use it for requests, connections, messages
or bytes alike. For byte rates Rate::per_sec is bytes/sec;
compose with crate::octets helpers for larger units.
Implementations§
Source§impl Rate
impl Rate
Sourcepub const fn new(units: u64, per: Duration) -> Self
pub const fn new(units: u64, per: Duration) -> Self
Create a new Rate of units per per period.
§Panics
Panics if units is zero, per is zero, or per exceeds
u64::MAX nanoseconds (~584 years). In const contexts this is
a compile-time error. Use Rate::try_new for fallible
construction from dynamic input.
§Examples
use std::time::Duration;
use rama_utils::rate::Rate;
const RATE: Rate = Rate::new(100, Duration::from_millis(250));
assert_eq!(RATE.units(), 100);
assert_eq!(RATE.per(), Duration::from_millis(250));Sourcepub const fn per_sec(units: u64) -> Self
pub const fn per_sec(units: u64) -> Self
Create a new Rate of units per second.
For byte rates this is bytes per second; combine with
crate::octets::kib_u64, crate::octets::mib_u64 and
crate::octets::gib_u64 for KiB/MiB/GiB rates, and with
crate::octets::from_bits_u64 for link rates expressed in bits.
§Panics
Panics if units is zero (compile-time error in const contexts).
§Examples
use rama_utils::{octets, rate::Rate};
// 100 requests per second
const REQ_RATE: Rate = Rate::per_sec(100);
// 10 MiB per second
const BYTE_RATE: Rate = Rate::per_sec(octets::mib_u64(10));
// a 100 Mbit/s link rate
const LINK_RATE: Rate = Rate::per_sec(octets::from_bits_u64(100_000_000));Sourcepub const fn per(&self) -> Duration
pub const fn per(&self) -> Duration
The period over which Rate::units units are allowed.