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
mod point;

use std::{
    fmt,
    ops::{Add, Deref, Sub},
};

pub use self::point::*;

/// A struct representing a location in time as milliseconds (i32)
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Millis(pub i32);

impl Millis {
    /// Converts from seconds to Milliseconds
    pub fn from_seconds(secs: f64) -> Millis {
        Millis((secs * 1000.0) as i32)
    }

    /// Converts this Milliseconds to seconds
    pub fn as_seconds(&self) -> f64 {
        self.0 as f64 / 1000.0
    }
}

impl fmt::Display for Millis {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_fmt(format_args!("{}ms", self.0))
    }
}

impl From<i32> for Millis {
    fn from(v: i32) -> Self {
        Self(v)
    }
}

impl Deref for Millis {
    type Target = i32;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Add<Millis> for Millis {
    type Output = Millis;

    fn add(self, rhs: Millis) -> Self::Output {
        Millis(self.0 + rhs.0)
    }
}

impl Sub<Millis> for Millis {
    type Output = i32;

    fn sub(self, rhs: Millis) -> Self::Output {
        self.0 - rhs.0
    }
}