Skip to content

Commit 0663ced

Browse files
committed
nostr: custom impl Ord for Kind
1 parent 43fe4a6 commit 0663ced

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

crates/nostr/src/event/kind.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
//! Kind
66
7+
use core::cmp::Ordering;
78
use core::fmt;
89
use core::hash::{Hash, Hasher};
910
use core::num::ParseIntError;
@@ -27,7 +28,7 @@ pub const EPHEMERAL_RANGE: Range<u64> = 20_000..30_000;
2728
pub const PARAMETERIZED_REPLACEABLE_RANGE: Range<u64> = 30_000..40_000;
2829

2930
/// Event [`Kind`]
30-
#[derive(Debug, Clone, Copy, Eq, PartialOrd, Ord)]
31+
#[derive(Debug, Clone, Copy)]
3132
pub enum Kind {
3233
/// Metadata (NIP01 and NIP05)
3334
Metadata,
@@ -135,6 +136,35 @@ pub enum Kind {
135136
Custom(u64),
136137
}
137138

139+
impl PartialEq<Kind> for Kind {
140+
fn eq(&self, other: &Kind) -> bool {
141+
self.as_u64() == other.as_u64()
142+
}
143+
}
144+
145+
impl Eq for Kind {}
146+
147+
impl PartialOrd for Kind {
148+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
149+
Some(self.cmp(other))
150+
}
151+
}
152+
153+
impl Ord for Kind {
154+
fn cmp(&self, other: &Self) -> Ordering {
155+
self.as_u64().cmp(&other.as_u64())
156+
}
157+
}
158+
159+
impl Hash for Kind {
160+
fn hash<H>(&self, state: &mut H)
161+
where
162+
H: Hasher,
163+
{
164+
self.as_u64().hash(state);
165+
}
166+
}
167+
138168
impl Kind {
139169
/// Get [`Kind`] as `u32`
140170
pub fn as_u32(&self) -> u32 {
@@ -329,21 +359,6 @@ impl FromStr for Kind {
329359
}
330360
}
331361

332-
impl PartialEq<Kind> for Kind {
333-
fn eq(&self, other: &Kind) -> bool {
334-
self.as_u64() == other.as_u64()
335-
}
336-
}
337-
338-
impl Hash for Kind {
339-
fn hash<H>(&self, state: &mut H)
340-
where
341-
H: Hasher,
342-
{
343-
self.as_u64().hash(state);
344-
}
345-
}
346-
347362
impl Add<u64> for Kind {
348363
type Output = Self;
349364
fn add(self, rhs: u64) -> Self::Output {

0 commit comments

Comments
 (0)