Sha256: 117b2504449177056447251da3d50791f291f32d91a027697f5e7504ef7e11f5
Contents?: true
Size: 1015 Bytes
Versions: 3
Compression:
Stored size: 1015 Bytes
Contents
use crate::time::{Clock, Duration, Instant}; /// A structure which handles conversion from Instants to u64 timestamps. #[derive(Debug)] pub(crate) struct TimeSource { start_time: Instant, } impl TimeSource { pub(crate) fn new(clock: &Clock) -> Self { Self { start_time: clock.now(), } } pub(crate) fn deadline_to_tick(&self, t: Instant) -> u64 { // Round up to the end of a ms self.instant_to_tick(t + Duration::from_nanos(999_999)) } pub(crate) fn instant_to_tick(&self, t: Instant) -> u64 { // round up let dur: Duration = t .checked_duration_since(self.start_time) .unwrap_or_else(|| Duration::from_secs(0)); let ms = dur.as_millis(); ms.try_into().unwrap_or(u64::MAX) } pub(crate) fn tick_to_duration(&self, t: u64) -> Duration { Duration::from_millis(t) } pub(crate) fn now(&self, clock: &Clock) -> u64 { self.instant_to_tick(clock.now()) } }
Version data entries
3 entries across 3 versions & 1 rubygems