Skip to main content

t2t_core/
point.rs

1//! The trait every point on a timeline shares.
2
3use core::ops::{Add, Sub};
4
5/// A point on one timeline, carried as a count of its unit since the timeline's origin, with the
6/// span between two of them.
7///
8/// Every point here implements it, and so may a type of your own that wraps one, to work with code
9/// generic over points.
10///
11/// # Examples
12/// ```
13/// use t2t_core::{TimePoint, Timedelta, Timestamp};
14///
15/// fn age<P: TimePoint>(stamp: P, now: P) -> P::Span {
16///     now - stamp
17/// }
18///
19/// let stamp = Timestamp::from_secs(10);
20/// assert_eq!(age(stamp, stamp + Timedelta::SECOND), Timedelta::SECOND, "any point's age");
21/// assert_eq!(Timestamp::from_count(stamp.count()), stamp, "and its count round trips");
22/// ```
23pub trait TimePoint:
24    Copy + Ord + Add<Self::Span, Output = Self> + Sub<Output = Self::Span>
25{
26    /// The span between two points.
27    type Span: Copy + Ord;
28
29    /// The point `count` units after the origin.
30    #[must_use]
31    fn from_count(count: i64) -> Self;
32
33    /// The point's count of units since the origin.
34    #[must_use]
35    fn count(self) -> i64;
36}