rrpack_basis/frames/
timed_event.rs1use rill_protocol::io::provider::Timestamp;
2use serde::{Deserialize, Serialize};
3use std::cmp::Ordering;
4use std::time::{SystemTime, SystemTimeError};
5
6#[derive(Debug, Clone, Deserialize, Serialize)]
7pub struct TimedEvent<T> {
8 pub timestamp: Timestamp,
9 pub event: T,
10}
11
12impl<T> TimedEvent<T> {
13 pub fn into_inner(self) -> T {
14 self.event
15 }
16}
17
18impl<T> Ord for TimedEvent<T> {
19 fn cmp(&self, other: &Self) -> Ordering {
20 self.timestamp.cmp(&other.timestamp)
21 }
22}
23
24impl<T> PartialOrd for TimedEvent<T> {
25 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
26 Some(self.cmp(other))
27 }
28}
29
30impl<T> PartialEq for TimedEvent<T> {
31 fn eq(&self, other: &Self) -> bool {
32 self.timestamp == other.timestamp
33 }
34}
35
36impl<T> Eq for TimedEvent<T> {}
37
38pub fn timed<T>(event: T) -> Option<TimedEvent<T>> {
40 time_to_ts(None)
41 .map(move |timestamp| TimedEvent { timestamp, event })
42 .ok()
43}
44
45pub fn time_to_ts(opt_system_time: Option<SystemTime>) -> Result<Timestamp, SystemTimeError> {
48 opt_system_time
49 .unwrap_or_else(SystemTime::now)
50 .duration_since(SystemTime::UNIX_EPOCH)
51 .map(Timestamp::from)
52}