Skip to content

Commit a4c1bbf

Browse files
committed
dev: add ordinal compare to udp primitive types
1 parent 3b91716 commit a4c1bbf

File tree

1 file changed

+108
-4
lines changed

1 file changed

+108
-4
lines changed

crates/udp_protocol/src/common.rs

+108-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,21 @@ impl AnnounceInterval {
1818
}
1919
}
2020

21-
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
21+
impl Ord for AnnounceInterval {
22+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
23+
self.0.get().cmp(&other.0.get())
24+
}
25+
}
26+
27+
impl PartialOrd for AnnounceInterval {
28+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
29+
Some(self.cmp(&other))
30+
}
31+
}
32+
33+
#[derive(
34+
PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes,
35+
)]
2236
#[repr(transparent)]
2337
pub struct InfoHash(pub [u8; 20]);
2438

@@ -32,6 +46,18 @@ impl ConnectionId {
3246
}
3347
}
3448

49+
impl Ord for ConnectionId {
50+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
51+
self.0.get().cmp(&other.0.get())
52+
}
53+
}
54+
55+
impl PartialOrd for ConnectionId {
56+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
57+
Some(self.cmp(&other))
58+
}
59+
}
60+
3561
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
3662
#[repr(transparent)]
3763
pub struct TransactionId(pub I32);
@@ -42,6 +68,18 @@ impl TransactionId {
4268
}
4369
}
4470

71+
impl Ord for TransactionId {
72+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
73+
self.0.get().cmp(&other.0.get())
74+
}
75+
}
76+
77+
impl PartialOrd for TransactionId {
78+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
79+
Some(self.cmp(&other))
80+
}
81+
}
82+
4583
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
4684
#[repr(transparent)]
4785
pub struct NumberOfBytes(pub I64);
@@ -52,6 +90,18 @@ impl NumberOfBytes {
5290
}
5391
}
5492

93+
impl Ord for NumberOfBytes {
94+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
95+
self.0.get().cmp(&other.0.get())
96+
}
97+
}
98+
99+
impl PartialOrd for NumberOfBytes {
100+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
101+
Some(self.cmp(&other))
102+
}
103+
}
104+
55105
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
56106
#[repr(transparent)]
57107
pub struct NumberOfPeers(pub I32);
@@ -62,6 +112,18 @@ impl NumberOfPeers {
62112
}
63113
}
64114

115+
impl Ord for NumberOfPeers {
116+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
117+
self.0.get().cmp(&other.0.get())
118+
}
119+
}
120+
121+
impl PartialOrd for NumberOfPeers {
122+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
123+
Some(self.cmp(&other))
124+
}
125+
}
126+
65127
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
66128
#[repr(transparent)]
67129
pub struct NumberOfDownloads(pub I32);
@@ -72,6 +134,18 @@ impl NumberOfDownloads {
72134
}
73135
}
74136

137+
impl Ord for NumberOfDownloads {
138+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
139+
self.0.get().cmp(&other.0.get())
140+
}
141+
}
142+
143+
impl PartialOrd for NumberOfDownloads {
144+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
145+
Some(self.cmp(&other))
146+
}
147+
}
148+
75149
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
76150
#[repr(transparent)]
77151
pub struct Port(pub U16);
@@ -82,6 +156,18 @@ impl Port {
82156
}
83157
}
84158

159+
impl Ord for Port {
160+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
161+
self.0.get().cmp(&other.0.get())
162+
}
163+
}
164+
165+
impl PartialOrd for Port {
166+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
167+
Some(self.cmp(&other))
168+
}
169+
}
170+
85171
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
86172
#[repr(transparent)]
87173
pub struct PeerKey(pub I32);
@@ -92,14 +178,30 @@ impl PeerKey {
92178
}
93179
}
94180

95-
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, AsBytes, FromBytes, FromZeroes)]
181+
impl Ord for PeerKey {
182+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
183+
self.0.get().cmp(&other.0.get())
184+
}
185+
}
186+
187+
impl PartialOrd for PeerKey {
188+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
189+
Some(self.cmp(&other))
190+
}
191+
}
192+
193+
#[derive(
194+
PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Hash, AsBytes, FromBytes, FromZeroes,
195+
)]
96196
#[repr(C, packed)]
97197
pub struct ResponsePeer<I: Ip> {
98198
pub ip_address: I,
99199
pub port: Port,
100200
}
101201

102-
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
202+
#[derive(
203+
PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes,
204+
)]
103205
#[repr(transparent)]
104206
pub struct Ipv4AddrBytes(pub [u8; 4]);
105207

@@ -117,7 +219,9 @@ impl From<Ipv4Addr> for Ipv4AddrBytes {
117219
}
118220
}
119221

120-
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
222+
#[derive(
223+
PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes,
224+
)]
121225
#[repr(transparent)]
122226
pub struct Ipv6AddrBytes(pub [u8; 16]);
123227

0 commit comments

Comments
 (0)