#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Time(u64);
impl Time {
pub const ZERO: Self = Self(0);
pub const fn from_ticks(ticks: u64) -> Self {
Self(ticks)
}
pub const fn ticks(self) -> u64 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimeSpan {
first: Time,
last: Time,
}
impl TimeSpan {
pub(crate) const fn new(first: Time, last: Time) -> Self {
Self { first, last }
}
pub const fn first(self) -> Time {
self.first
}
pub const fn last(self) -> Time {
self.last
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimeRange {
start: Time,
end: Option<Time>,
}
impl TimeRange {
pub const fn all() -> Self {
Self::from(Time::ZERO)
}
pub const fn from(start: Time) -> Self {
Self { start, end: None }
}
pub const fn closed(start: Time, end: Time) -> Self {
Self {
start,
end: Some(end),
}
}
pub const fn point(time: Time) -> Self {
Self::closed(time, time)
}
pub const fn start(self) -> Time {
self.start
}
pub const fn end(self) -> Option<Time> {
self.end
}
pub const fn is_empty(self) -> bool {
match self.end {
Some(end) => self.start.ticks() > end.ticks(),
None => false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Timescale {
factor: u32,
unit: TimeUnit,
}
impl Timescale {
pub(crate) const fn new(factor: u32, unit: TimeUnit) -> Self {
Self { factor, unit }
}
pub const fn factor(self) -> u32 {
self.factor
}
pub const fn unit(self) -> TimeUnit {
self.unit
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TimeUnit {
Second,
Millisecond,
Microsecond,
Nanosecond,
Picosecond,
Femtosecond,
Attosecond,
Zeptosecond,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn inclusive_ranges_preserve_bounds() {
let start = Time::from_ticks(7);
let end = Time::from_ticks(11);
let all = TimeRange::all();
assert_eq!(all.start(), Time::ZERO);
assert_eq!(all.end(), None);
assert!(!all.is_empty());
let unbounded = TimeRange::from(start);
assert_eq!(unbounded.start(), start);
assert_eq!(unbounded.end(), None);
assert!(!unbounded.is_empty());
let closed = TimeRange::closed(start, end);
assert_eq!(closed.start(), start);
assert_eq!(closed.end(), Some(end));
assert!(!closed.is_empty());
let reversed = TimeRange::closed(end, start);
assert_eq!(reversed.start(), end);
assert_eq!(reversed.end(), Some(start));
assert!(reversed.is_empty());
for time in [Time::ZERO, start, Time::from_ticks(u64::MAX)] {
let point = TimeRange::point(time);
assert_eq!(point.start(), time);
assert_eq!(point.end(), Some(time));
assert!(!point.is_empty());
assert!(!TimeRange::from(time).is_empty());
}
assert!(TimeRange::closed(Time::from_ticks(u64::MAX), Time::ZERO).is_empty());
}
#[test]
fn exact_time_metadata() {
const FIRST: Time = Time::from_ticks(3);
const SPAN: TimeSpan = TimeSpan::new(FIRST, Time::from_ticks(u64::MAX));
const SCALE: Timescale = Timescale::new(10, TimeUnit::Nanosecond);
const _: () = {
assert!(SPAN.first().ticks() == 3);
assert!(SPAN.last().ticks() == u64::MAX);
assert!(SCALE.factor() == 10);
assert!(matches!(SCALE.unit(), TimeUnit::Nanosecond));
assert!(TimeRange::all().start().ticks() == 0);
assert!(TimeRange::from(FIRST).end().is_none());
assert!(TimeRange::closed(FIRST, Time::ZERO).is_empty());
assert!(!TimeRange::point(FIRST).is_empty());
};
let first = FIRST;
let last = Time::from_ticks(u64::MAX);
let span = SPAN;
assert_eq!(SCALE, Timescale::new(10, TimeUnit::Nanosecond));
assert_eq!(span.first(), first);
assert_eq!(span.last(), last);
assert_eq!(last.ticks(), u64::MAX);
assert!(Time::ZERO < first && first < last);
let units = [
TimeUnit::Second,
TimeUnit::Millisecond,
TimeUnit::Microsecond,
TimeUnit::Nanosecond,
TimeUnit::Picosecond,
TimeUnit::Femtosecond,
TimeUnit::Attosecond,
TimeUnit::Zeptosecond,
];
for unit in units {
let scale = Timescale::new(100, unit);
assert_eq!(scale.factor(), 100);
assert_eq!(scale.unit(), unit);
}
}
}