1use fluxion_core::{HasTimestamp, Timestamped};
10use std::fmt::Debug;
11
12#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
17pub struct WithPrevious<T> {
18 pub previous: Option<T>,
20 pub current: T,
22}
23
24impl<T> WithPrevious<T> {
25 pub fn new(previous: Option<T>, current: T) -> Self {
27 Self { previous, current }
28 }
29
30 pub fn has_previous(&self) -> bool {
32 self.previous.is_some()
33 }
34
35 pub fn as_pair(&self) -> Option<(&T, &T)> {
37 self.previous.as_ref().map(|prev| (prev, &self.current))
38 }
39}
40
41impl<T: Timestamped> HasTimestamp for WithPrevious<T> {
42 type Inner = T::Inner;
43 type Timestamp = T::Timestamp;
44
45 fn timestamp(&self) -> Self::Timestamp {
46 self.current.timestamp()
47 }
48}
49
50impl<T: Timestamped> Timestamped for WithPrevious<T> {
51 fn with_timestamp(value: Self::Inner, timestamp: Self::Timestamp) -> Self {
52 Self {
53 previous: None,
54 current: T::with_timestamp(value, timestamp),
55 }
56 }
57
58 fn with_fresh_timestamp(value: Self::Inner) -> Self {
59 Self {
60 previous: None,
61 current: T::with_fresh_timestamp(value),
62 }
63 }
64
65 fn into_inner(self) -> Self::Inner {
66 self.current.into_inner()
67 }
68}
69
70#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
86pub struct CombinedState<V, TS = u64>
87where
88 V: Clone + Debug + Ord,
89 TS: Clone + Debug + Ord,
90{
91 state: Vec<V>,
92 timestamp: TS,
93}
94
95impl<V, TS> CombinedState<V, TS>
96where
97 V: Clone + Debug + Ord,
98 TS: Clone + Debug + Ord,
99{
100 pub fn new(state: Vec<V>, timestamp: TS) -> Self {
102 Self { state, timestamp }
103 }
104
105 pub fn values(&self) -> &Vec<V> {
107 &self.state
108 }
109
110 pub fn len(&self) -> usize {
112 self.state.len()
113 }
114
115 pub fn is_empty(&self) -> bool {
117 self.state.is_empty()
118 }
119}
120
121impl<V, TS> HasTimestamp for CombinedState<V, TS>
122where
123 V: Clone + Debug + Ord,
124 TS: Clone + Debug + Ord + Copy + Send + Sync,
125{
126 type Inner = Self;
127 type Timestamp = TS;
128
129 fn timestamp(&self) -> Self::Timestamp {
130 self.timestamp
131 }
132}
133
134impl<V, TS> Timestamped for CombinedState<V, TS>
135where
136 V: Clone + Debug + Ord,
137 TS: Clone + Debug + Ord + Copy + Send + Sync,
138{
139 fn with_timestamp(value: Self::Inner, timestamp: Self::Timestamp) -> Self {
140 Self {
141 state: value.state,
142 timestamp,
143 }
144 }
145
146 fn with_fresh_timestamp(value: Self::Inner) -> Self {
147 value
150 }
151
152 fn into_inner(self) -> Self::Inner {
153 self
154 }
155}