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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use crate::time::{Duration, Time};

/// A 64-bit number describing either nanoseconds OR sequence numbers.
///
/// Must be matched with a [`crate::TimeType`] to know what.
///
/// Used both for time points and durations.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct TimeInt(pub(crate) i64);

impl nohash_hasher::IsEnabled for TimeInt {}

impl re_types_core::SizeBytes for TimeInt {
    #[inline]
    fn heap_size_bytes(&self) -> u64 {
        0
    }
}

impl TimeInt {
    /// The beginning of time.
    ///
    /// Special value used for timeless data.
    ///
    /// NOTE: this is not necessarily [`i64::MIN`].
    // The reason we don't use i64::MIN is because in the time panel we need
    // to be able to pan to before the `TimeInt::BEGINNING`, and so we need
    // a bit of leeway.
    pub const BEGINNING: Self = Self(i64::MIN / 2);

    // TODO(#4832): `TimeInt::BEGINNING` vs. `TimeInt::MIN` vs. `Option<TimeInt>`…
    pub const MIN: Self = Self(i64::MIN);
    pub const MAX: Self = Self(i64::MAX);

    /// For time timelines.
    #[inline]
    pub fn from_nanos(nanos: i64) -> Self {
        Self(nanos)
    }

    /// For time timelines.
    #[inline]
    pub fn from_milliseconds(millis: i64) -> Self {
        Self::from_nanos(millis * 1_000_000)
    }

    /// For time timelines.
    #[inline]
    pub fn from_seconds(seconds: i64) -> Self {
        Self::from_nanos(seconds.saturating_mul(1_000_000_000))
    }

    /// For sequence timelines.
    #[inline]
    pub fn from_sequence(sequence: i64) -> Self {
        Self(sequence)
    }

    #[inline]
    pub fn as_i64(&self) -> i64 {
        self.0
    }

    #[inline]
    pub fn as_f32(&self) -> f32 {
        self.0 as _
    }

    #[inline]
    pub fn as_f64(&self) -> f64 {
        self.0 as _
    }

    #[inline]
    pub fn abs(&self) -> Self {
        Self(self.0.saturating_abs())
    }
}

impl From<i64> for TimeInt {
    #[inline]
    fn from(seq: i64) -> Self {
        Self(seq)
    }
}

impl From<Duration> for TimeInt {
    #[inline]
    fn from(duration: Duration) -> Self {
        Self(duration.as_nanos())
    }
}

impl From<Time> for TimeInt {
    #[inline]
    fn from(time: Time) -> Self {
        Self(time.nanos_since_epoch())
    }
}

impl From<TimeInt> for Time {
    #[inline]
    fn from(int: TimeInt) -> Self {
        Self::from_ns_since_epoch(int.as_i64())
    }
}

impl From<TimeInt> for Duration {
    #[inline]
    fn from(int: TimeInt) -> Self {
        Self::from_nanos(int.as_i64())
    }
}

impl From<TimeInt> for re_types_core::datatypes::TimeInt {
    #[inline]
    fn from(int: TimeInt) -> Self {
        Self(int.as_i64())
    }
}

impl std::ops::Neg for TimeInt {
    type Output = Self;

    #[inline]
    fn neg(self) -> Self::Output {
        Self(self.0.saturating_neg())
    }
}

impl std::ops::Add for TimeInt {
    type Output = Self;

    #[inline]
    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0.saturating_add(rhs.0))
    }
}

impl std::ops::Sub for TimeInt {
    type Output = Self;

    #[inline]
    fn sub(self, rhs: Self) -> Self::Output {
        Self(self.0.saturating_sub(rhs.0))
    }
}

impl std::ops::AddAssign for TimeInt {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl std::ops::SubAssign for TimeInt {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl std::iter::Sum for TimeInt {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        let mut sum = TimeInt(0);
        for item in iter {
            sum += item;
        }
        sum
    }
}