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
use std::ops::Sub;
use time_calc::Ticks;

/// A period of time in ticks.
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Period<T = Ticks> {
    pub start: T,
    pub end: T,
}

impl<T> Period<T> {
    /// The duration of the period.
    pub fn duration(&self) -> T
    where
        T: Clone + Sub<T, Output = T>,
    {
        self.end.clone() - self.start.clone()
    }

    /// Does the given ticks fall within the period.
    #[inline]
    pub fn contains(&self, t: T) -> bool
    where
        T: PartialOrd + PartialEq,
    {
        t >= self.start && t < self.end
    }

    /// Whether or not self intersects with the other period.
    #[inline]
    pub fn intersects(&self, other: &Self) -> bool
    where
        T: PartialOrd,
    {
        !(other.start > self.end || self.start > other.end)
    }
}