Skip to content

Commit b189b1b

Browse files
committed
feat: impl Ord and Eq for FeedUpdate
1 parent 2bc4785 commit b189b1b

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

lazer/publisher_sdk/rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyth-lazer-publisher-sdk"
3-
version = "0.10.0"
3+
version = "0.11.0"
44
edition = "2021"
55
description = "Pyth Lazer Publisher SDK types."
66
license = "Apache-2.0"

lazer/publisher_sdk/rust/src/lib.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,95 @@ impl From<pyth_lazer_protocol::api::Channel> for state::Channel {
147147
result
148148
}
149149
}
150+
151+
impl Eq for PriceUpdate {}
152+
153+
impl Ord for PriceUpdate {
154+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
155+
(self.price, self.best_bid_price, self.best_ask_price).cmp(&(
156+
other.price,
157+
other.best_bid_price,
158+
other.best_ask_price,
159+
))
160+
}
161+
}
162+
163+
impl Eq for FundingRateUpdate {}
164+
165+
impl Ord for FundingRateUpdate {
166+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
167+
(
168+
self.price,
169+
self.rate,
170+
self.funding_rate_interval
171+
.as_ref()
172+
.map(|duration| (duration.seconds, duration.nanos)),
173+
)
174+
.cmp(&(
175+
other.price,
176+
other.rate,
177+
other
178+
.funding_rate_interval
179+
.as_ref()
180+
.map(|duration| (duration.seconds, duration.nanos)),
181+
))
182+
}
183+
}
184+
185+
impl PartialOrd for FundingRateUpdate {
186+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
187+
Some(self.cmp(other))
188+
}
189+
}
190+
191+
impl PartialOrd for PriceUpdate {
192+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
193+
Some(self.cmp(other))
194+
}
195+
}
196+
197+
impl Eq for Update {}
198+
199+
impl Ord for Update {
200+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
201+
match (self, other) {
202+
(Update::PriceUpdate(a), Update::PriceUpdate(b)) => a.cmp(b),
203+
(Update::FundingRateUpdate(a), Update::FundingRateUpdate(b)) => a.cmp(b),
204+
(Update::PriceUpdate(_), Update::FundingRateUpdate(_)) => std::cmp::Ordering::Less,
205+
(Update::FundingRateUpdate(_), Update::PriceUpdate(_)) => std::cmp::Ordering::Greater,
206+
}
207+
}
208+
}
209+
210+
impl PartialOrd for Update {
211+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
212+
Some(self.cmp(other))
213+
}
214+
}
215+
216+
impl Eq for FeedUpdate {}
217+
218+
// FeedUpdates are ordered first by source_timestamp, then by feed_id, then by update.
219+
impl Ord for FeedUpdate {
220+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
221+
(
222+
&self.source_timestamp.as_ref().map(|t| (t.seconds, t.nanos)),
223+
&self.feed_id,
224+
&self.update,
225+
)
226+
.cmp(&(
227+
&other
228+
.source_timestamp
229+
.as_ref()
230+
.map(|t| (t.seconds, t.nanos)),
231+
&other.feed_id,
232+
&other.update,
233+
))
234+
}
235+
}
236+
237+
impl PartialOrd for FeedUpdate {
238+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
239+
Some(self.cmp(other))
240+
}
241+
}

0 commit comments

Comments
 (0)