fluxion_stream_time/
instant_timestamped.rs

1// Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>
2// Licensed under the Apache License, Version 2.0
3// http://www.apache.org/licenses/LICENSE-2.0
4
5use core::cmp::Ordering;
6use core::fmt::Debug;
7use core::ops::Deref;
8use fluxion_core::{HasTimestamp, Timestamped};
9use fluxion_runtime::runtime::Runtime;
10
11/// A timestamped value using a Timer's Instant type for monotonic time operations.
12///
13/// This type wraps a value with a timestamp from the provided Timer implementation,
14/// implementing the `Timestamped` trait. This enables time-based
15/// operators like `delay`, `debounce`, and `throttle`.
16///
17/// # Example
18///
19/// ```rust,no_run
20/// # #[cfg(all(feature = "runtime-tokio", not(target_arch = "wasm32")))]
21/// use fluxion_stream_time::InstantTimestamped;
22/// # #[cfg(all(feature = "runtime-tokio", not(target_arch = "wasm32")))]
23/// use fluxion_runtime::impls::tokio::{TokioRuntime, TokioTimer};
24/// # #[cfg(all(feature = "runtime-tokio", not(target_arch = "wasm32")))]
25/// use fluxion_runtime::timer::Timer;
26///
27/// # #[cfg(all(feature = "runtime-tokio", not(target_arch = "wasm32")))]
28/// # fn main() {
29/// let timer = TokioTimer;
30/// let item: InstantTimestamped<i32, TokioRuntime> = InstantTimestamped::new(42, timer.now());
31/// # }
32/// # #[cfg(not(all(feature = "runtime-tokio", not(target_arch = "wasm32"))))]
33/// # fn main() {}
34/// ```
35#[derive(Debug)]
36pub struct InstantTimestamped<T, R: Runtime> {
37    pub value: T,
38    pub timestamp: R::Instant,
39}
40
41impl<T, R: Runtime> Clone for InstantTimestamped<T, R>
42where
43    T: Clone,
44{
45    fn clone(&self) -> Self {
46        Self {
47            value: self.value.clone(),
48            timestamp: self.timestamp,
49        }
50    }
51}
52
53impl<T, R: Runtime> InstantTimestamped<T, R> {
54    /// Creates a new timestamped value with the given timestamp.
55    pub fn new(value: T, timestamp: R::Instant) -> Self {
56        Self { value, timestamp }
57    }
58}
59
60impl<T, R: Runtime> HasTimestamp for InstantTimestamped<T, R> {
61    type Timestamp = R::Instant;
62
63    fn timestamp(&self) -> Self::Timestamp {
64        self.timestamp
65    }
66}
67
68impl<T, R: Runtime> Timestamped for InstantTimestamped<T, R>
69where
70    T: Clone,
71{
72    type Inner = T;
73
74    fn into_inner(self) -> Self::Inner {
75        self.value
76    }
77
78    fn with_timestamp(inner: Self::Inner, timestamp: Self::Timestamp) -> Self {
79        Self::new(inner, timestamp)
80    }
81}
82
83impl<T, R: Runtime> PartialEq for InstantTimestamped<T, R>
84where
85    T: PartialEq,
86{
87    fn eq(&self, other: &Self) -> bool {
88        self.timestamp == other.timestamp && self.value == other.value
89    }
90}
91
92impl<T, R: Runtime> Eq for InstantTimestamped<T, R> where T: Eq {}
93
94impl<T, R: Runtime> PartialOrd for InstantTimestamped<T, R>
95where
96    T: PartialOrd,
97{
98    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
99        self.timestamp.partial_cmp(&other.timestamp)
100    }
101}
102
103impl<T, R: Runtime> Ord for InstantTimestamped<T, R>
104where
105    T: Ord,
106{
107    fn cmp(&self, other: &Self) -> Ordering {
108        self.timestamp.cmp(&other.timestamp)
109    }
110}
111
112impl<T, R: Runtime> Deref for InstantTimestamped<T, R> {
113    type Target = T;
114
115    fn deref(&self) -> &Self::Target {
116        &self.value
117    }
118}