Skip to content

Commit

Permalink
fix Ord for AuditTx
Browse files Browse the repository at this point in the history
  • Loading branch information
ValuedMammal committed Feb 19, 2025
1 parent 1e690c5 commit 0d03c7a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
38 changes: 18 additions & 20 deletions src/audittx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ impl AuditTx {
}
}

/// Defines how a scored mempool entry is prioritized by the modified queue
#[derive(Debug)]
pub struct TxPriority {
pub uid: usize,
pub order: u32,
pub score: f64,
}

impl PartialEq for AuditTx {
fn eq(&self, other: &Self) -> bool {
self.uid == other.uid
Expand All @@ -76,21 +68,28 @@ impl PartialEq for AuditTx {

impl Eq for AuditTx {}

#[allow(clippy::non_canonical_partial_ord_impl)]
impl PartialOrd for AuditTx {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
impl Ord for AuditTx {
fn cmp(&self, other: &Self) -> Ordering {
let a = (self.score, self.order, self.uid);
let b = (other.score, other.order, other.uid);
compare_audit_tx(a, b)
}
}

impl Ord for AuditTx {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).expect("ordering is Some")
impl PartialOrd for AuditTx {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

/// Defines how a scored mempool entry is prioritized by the modified queue
#[derive(Debug)]
pub struct TxPriority {
pub uid: usize,
pub order: u32,
pub score: f64,
}

impl PartialEq for TxPriority {
fn eq(&self, other: &Self) -> bool {
self.uid == other.uid
Expand All @@ -99,17 +98,16 @@ impl PartialEq for TxPriority {

impl Eq for TxPriority {}

#[allow(clippy::non_canonical_partial_ord_impl)]
impl PartialOrd for TxPriority {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
impl Ord for TxPriority {
fn cmp(&self, other: &Self) -> Ordering {
let a = (self.score, self.order, self.uid);
let b = (other.score, other.order, other.uid);
compare_audit_tx(a, b)
}
}

impl Ord for TxPriority {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).expect("ordering is Some")
impl PartialOrd for TxPriority {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
8 changes: 4 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ pub fn median_from_sorted(seq: &[f64]) -> f64 {
}

/// `a` and `b` as (score, order, uid)
pub fn compare_audit_tx(a: (f64, u32, usize), b: (f64, u32, usize)) -> Option<Ordering> {
pub fn compare_audit_tx(a: (f64, u32, usize), b: (f64, u32, usize)) -> Ordering {
if (a.0 - b.0).abs() > f64::EPSILON {
// a != b
a.0.partial_cmp(&b.0)
a.0.total_cmp(&b.0)
} else if a.1 != b.1 {
a.1.partial_cmp(&b.1)
a.1.cmp(&b.1)
} else {
a.2.partial_cmp(&b.2)
a.2.cmp(&b.2)
}
}

Expand Down

0 comments on commit 0d03c7a

Please sign in to comment.