re_log_types/index/
timeline.rs

1use crate::{ResolvedTimeRange, TimeType, TimestampFormat};
2
3re_string_interner::declare_new_type!(
4    /// The name of a timeline. Often something like `"log_time"` or `"frame_nr"`.
5    ///
6    /// This uniquely identifies a timeline.
7    #[cfg_attr(feature = "serde", derive(::serde::Deserialize, ::serde::Serialize))]
8    pub struct TimelineName;
9);
10
11impl TimelineName {
12    /// The log time timeline to which all API functions will always log.
13    ///
14    /// This timeline is automatically maintained by the SDKs and captures the wall-clock time at
15    /// which point the data was logged (according to the client's wall-clock).
16    #[inline]
17    pub fn log_time() -> Self {
18        Self::new("log_time")
19    }
20
21    /// The log tick timeline to which all API functions will always log.
22    ///
23    /// This timeline is automatically maintained by the SDKs and captures the logging tick at
24    /// which point the data was logged.
25    /// The logging tick is monotically incremented each time the client calls one of the logging
26    /// methods on a `RecordingStream`.
27    #[inline]
28    pub fn log_tick() -> Self {
29        Self::new("log_tick")
30    }
31}
32
33// ----------------------------------------------------------------------------
34
35/// A time frame/space, e.g. `log_time` or `frame_nr`, coupled with the type of time
36/// it keeps.
37#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
38#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
39pub struct Timeline {
40    /// Name of the timeline (e.g. `log_time`).
41    name: TimelineName,
42
43    /// Sequence or time?
44    typ: TimeType,
45}
46
47impl Timeline {
48    #[inline]
49    pub fn new(name: impl Into<TimelineName>, typ: TimeType) -> Self {
50        Self {
51            name: name.into(),
52            typ,
53        }
54    }
55
56    /// For things like camera frames or iteration count.
57    #[inline]
58    pub fn new_sequence(name: impl Into<TimelineName>) -> Self {
59        Self {
60            name: name.into(),
61            typ: TimeType::Sequence,
62        }
63    }
64
65    /// For relative times (e.g. seconds since start).
66    #[inline]
67    pub fn new_duration(name: impl Into<TimelineName>) -> Self {
68        Self {
69            name: name.into(),
70            typ: TimeType::DurationNs,
71        }
72    }
73
74    /// For absolute timestamps.
75    #[inline]
76    pub fn new_timestamp(name: impl Into<TimelineName>) -> Self {
77        Self {
78            name: name.into(),
79            typ: TimeType::TimestampNs,
80        }
81    }
82
83    #[deprecated(
84        since = "0.23.0",
85        note = "Use `Timeline::new_duration` or `new_timestamp` instead"
86    )]
87    #[inline]
88    pub fn new_temporal(name: impl Into<TimelineName>) -> Self {
89        Self::new_duration(name)
90    }
91
92    #[inline]
93    pub fn name(&self) -> &TimelineName {
94        &self.name
95    }
96
97    #[inline]
98    pub fn typ(&self) -> TimeType {
99        self.typ
100    }
101
102    /// The log time timeline to which all API functions will always log.
103    ///
104    /// This timeline is automatically maintained by the SDKs and captures the wall-clock time at
105    /// which point the data was logged (according to the client's wall-clock).
106    #[inline]
107    pub fn log_time() -> Self {
108        Self::new(TimelineName::log_time(), TimeType::TimestampNs)
109    }
110
111    /// The log tick timeline to which all API functions will always log.
112    ///
113    /// This timeline is automatically maintained by the SDKs and captures the logging tick at
114    /// which point the data was logged.
115    /// The logging tick is monotically incremented each time the client calls one of the logging
116    /// methods on a `RecordingStream`.
117    #[inline]
118    pub fn log_tick() -> Self {
119        Self::new(TimelineName::log_tick(), TimeType::Sequence)
120    }
121
122    /// Returns a formatted string of `time_range` on this `Timeline`.
123    #[inline]
124    pub fn format_time_range(
125        &self,
126        time_range: &ResolvedTimeRange,
127        timestamp_format: TimestampFormat,
128    ) -> String {
129        self.typ.format_range(*time_range, timestamp_format)
130    }
131
132    /// Returns a formatted string of `time_range` on this `Timeline`.
133    #[inline]
134    pub fn format_time_range_utc(&self, time_range: &ResolvedTimeRange) -> String {
135        self.format_time_range(time_range, TimestampFormat::Utc)
136    }
137
138    /// Returns the appropriate arrow datatype to represent this timeline.
139    #[inline]
140    pub fn datatype(&self) -> arrow::datatypes::DataType {
141        self.typ.datatype()
142    }
143}
144
145impl nohash_hasher::IsEnabled for Timeline {}
146
147impl re_byte_size::SizeBytes for TimelineName {
148    #[inline]
149    fn heap_size_bytes(&self) -> u64 {
150        0
151    }
152}
153
154impl re_byte_size::SizeBytes for Timeline {
155    #[inline]
156    fn heap_size_bytes(&self) -> u64 {
157        0
158    }
159}
160
161// required for [`nohash_hasher`].
162#[allow(clippy::derived_hash_with_manual_eq)]
163impl std::hash::Hash for Timeline {
164    #[inline]
165    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
166        state.write_u64(self.name.hash() ^ self.typ.hash());
167    }
168}