File size: 6,817 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
use std::{
fmt::{Debug, Display},
time::Duration,
};
/// Stores a [`Duration`] in a given precision (in nanoseconds) in 4 bytes.
///
/// For instance, for `P = 10_000` (10 microseconds), this allows a for a total
/// duration of 11.9 hours. Values smaller than 10 microseconds are stored as 10
/// microseconds.
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
pub struct SmallDuration<const P: u64>(u32);
impl<const P: u64> SmallDuration<P> {
pub const ZERO: SmallDuration<P> = SmallDuration(0);
// TODO(alexkirsz) Figure out if MIN should be 0 or 1.
pub const MIN: SmallDuration<P> = SmallDuration(1);
pub const MAX: SmallDuration<P> = SmallDuration(u32::MAX);
pub const fn from_nanos(nanos: u64) -> Self {
if nanos == 0 {
return SmallDuration::ZERO;
}
if nanos <= P {
return SmallDuration::MIN;
}
let value = nanos / P;
if value > u32::MAX as u64 {
return SmallDuration::MAX;
}
SmallDuration(value as u32)
}
pub const fn from_micros(micros: u64) -> Self {
if micros == 0 {
return SmallDuration::ZERO;
}
let micros_precision = P / 1_000;
if micros <= micros_precision {
return SmallDuration::MIN;
}
let value = micros * 1_000 / P;
if value > u32::MAX as u64 {
return SmallDuration::MAX;
}
SmallDuration(value as u32)
}
pub const fn from_millis(millis: u64) -> Self {
if millis == 0 {
return SmallDuration::ZERO;
}
let millis_precision = P / 1_000_000;
if millis <= millis_precision {
return SmallDuration::MIN;
}
let value = millis * 1_000_000 / P;
if value > u32::MAX as u64 {
return SmallDuration::MAX;
}
SmallDuration(value as u32)
}
pub const fn from_secs(secs: u64) -> Self {
if secs == 0 {
return SmallDuration::ZERO;
}
let secs_precision = P / 1_000_000_000;
if secs <= secs_precision {
return SmallDuration::MIN;
}
let value = secs * 1_000_000_000 / P;
if value > u32::MAX as u64 {
return SmallDuration::MAX;
}
SmallDuration(value as u32)
}
pub(self) fn to_duration(self) -> Duration {
Duration::from_nanos(self.0 as u64 * P)
}
}
impl<const P: u64> From<Duration> for SmallDuration<P> {
fn from(duration: Duration) -> Self {
if duration.is_zero() {
return SmallDuration::ZERO;
}
let nanos = duration.as_nanos();
if nanos <= P as u128 {
return SmallDuration::MIN;
}
(nanos / P as u128)
.try_into()
.map_or(SmallDuration::MAX, SmallDuration)
}
}
impl<const P: u64> From<SmallDuration<P>> for Duration {
fn from(duration: SmallDuration<P>) -> Self {
duration.to_duration()
}
}
impl<const P: u64> Display for SmallDuration<P> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let duration = Duration::from(*self);
duration.fmt(f)
}
}
impl<const P: u64> Debug for SmallDuration<P> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let duration = Duration::from(*self);
duration.fmt(f)
}
}
impl<const P: u64> PartialEq<Duration> for SmallDuration<P> {
fn eq(&self, other: &Duration) -> bool {
self.to_duration() == *other
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::SmallDuration;
#[test]
fn test_1_nano() {
type Sd = SmallDuration<1>;
assert_eq!(Sd::from_nanos(1), Duration::from_nanos(1));
assert_eq!(Sd::from_nanos(42), Duration::from_nanos(42));
assert_eq!(Sd::from_micros(1), Duration::from_micros(1));
assert_eq!(Sd::from_micros(42), Duration::from_micros(42));
assert_eq!(Sd::from_millis(1), Duration::from_millis(1));
assert_eq!(Sd::from_millis(42), Duration::from_millis(42));
assert_eq!(Sd::from_secs(1), Duration::from_secs(1));
// 1ns precision can only store up to ~4.29s.
assert_eq!(Sd::from_secs(4), Duration::from_secs(4));
assert_eq!(Sd::from_secs(5), Sd::MAX);
}
#[test]
fn test_1_micro() {
type Sd = SmallDuration<1_000>;
// 1µs precision can't store ns-level variations.
assert_eq!(Sd::from_nanos(1), Sd::MIN);
assert_eq!(Sd::from_nanos(42), Sd::MIN);
assert_eq!(Sd::from_micros(1), Duration::from_micros(1));
assert_eq!(Sd::from_micros(42), Duration::from_micros(42));
assert_eq!(Sd::from_millis(1), Duration::from_millis(1));
assert_eq!(Sd::from_millis(42), Duration::from_millis(42));
assert_eq!(Sd::from_secs(1), Duration::from_secs(1));
assert_eq!(Sd::from_secs(42), Duration::from_secs(42));
// 1µs precision can only store up to ~4,294s.
assert_eq!(Sd::from_secs(4_000), Duration::from_secs(4_000));
assert_eq!(Sd::from_secs(5_000), Sd::MAX);
}
#[test]
fn test_1_milli() {
type Sd = SmallDuration<1_000_000>;
// 1ms precision can't store ns-or-µs-level variations.
assert_eq!(Sd::from_nanos(1), Sd::MIN);
assert_eq!(Sd::from_nanos(42), Sd::MIN);
assert_eq!(Sd::from_micros(1), Sd::MIN);
assert_eq!(Sd::from_micros(42), Sd::MIN);
assert_eq!(Sd::from_millis(1), Duration::from_millis(1));
assert_eq!(Sd::from_millis(42), Duration::from_millis(42));
assert_eq!(Sd::from_secs(1), Duration::from_secs(1));
assert_eq!(Sd::from_secs(42), Duration::from_secs(42));
// 1ms precision can only store up to ~4,294,000s.
assert_eq!(Sd::from_secs(4_000_000), Duration::from_secs(4_000_000));
assert_eq!(Sd::from_secs(5_000_000), Sd::MAX);
}
#[test]
fn test_1_sec() {
type Sd = SmallDuration<1_000_000_000>;
// 1ms precision can't store ns/µs/ms-level variations.
assert_eq!(Sd::from_nanos(1), Sd::MIN);
assert_eq!(Sd::from_nanos(42), Sd::MIN);
assert_eq!(Sd::from_micros(1), Sd::MIN);
assert_eq!(Sd::from_micros(42), Sd::MIN);
assert_eq!(Sd::from_millis(1), Sd::MIN);
assert_eq!(Sd::from_millis(42), Sd::MIN);
assert_eq!(Sd::from_secs(1), Duration::from_secs(1));
assert_eq!(Sd::from_secs(42), Duration::from_secs(42));
// 1s precision can only store up to ~4,294,000,000s.
assert_eq!(
Sd::from_secs(4_000_000_000),
Duration::from_secs(4_000_000_000)
);
assert_eq!(Sd::from_secs(5_000_000_000), Sd::MAX);
}
}
|