dev: add ordinal compare to udp primitive types

This commit is contained in:
Cameron Garnham 2024-07-15 14:37:57 +02:00
parent 3b917166ec
commit a4c1bbfe55
No known key found for this signature in database
GPG key ID: A0BE58692D29424F

View file

@ -18,7 +18,21 @@ impl AnnounceInterval {
} }
} }
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] impl Ord for AnnounceInterval {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.get().cmp(&other.0.get())
}
}
impl PartialOrd for AnnounceInterval {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(&other))
}
}
#[derive(
PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes,
)]
#[repr(transparent)] #[repr(transparent)]
pub struct InfoHash(pub [u8; 20]); pub struct InfoHash(pub [u8; 20]);
@ -32,6 +46,18 @@ impl ConnectionId {
} }
} }
impl Ord for ConnectionId {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.get().cmp(&other.0.get())
}
}
impl PartialOrd for ConnectionId {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(&other))
}
}
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
#[repr(transparent)] #[repr(transparent)]
pub struct TransactionId(pub I32); pub struct TransactionId(pub I32);
@ -42,6 +68,18 @@ impl TransactionId {
} }
} }
impl Ord for TransactionId {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.get().cmp(&other.0.get())
}
}
impl PartialOrd for TransactionId {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(&other))
}
}
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
#[repr(transparent)] #[repr(transparent)]
pub struct NumberOfBytes(pub I64); pub struct NumberOfBytes(pub I64);
@ -52,6 +90,18 @@ impl NumberOfBytes {
} }
} }
impl Ord for NumberOfBytes {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.get().cmp(&other.0.get())
}
}
impl PartialOrd for NumberOfBytes {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(&other))
}
}
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
#[repr(transparent)] #[repr(transparent)]
pub struct NumberOfPeers(pub I32); pub struct NumberOfPeers(pub I32);
@ -62,6 +112,18 @@ impl NumberOfPeers {
} }
} }
impl Ord for NumberOfPeers {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.get().cmp(&other.0.get())
}
}
impl PartialOrd for NumberOfPeers {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(&other))
}
}
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
#[repr(transparent)] #[repr(transparent)]
pub struct NumberOfDownloads(pub I32); pub struct NumberOfDownloads(pub I32);
@ -72,6 +134,18 @@ impl NumberOfDownloads {
} }
} }
impl Ord for NumberOfDownloads {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.get().cmp(&other.0.get())
}
}
impl PartialOrd for NumberOfDownloads {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(&other))
}
}
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
#[repr(transparent)] #[repr(transparent)]
pub struct Port(pub U16); pub struct Port(pub U16);
@ -82,6 +156,18 @@ impl Port {
} }
} }
impl Ord for Port {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.get().cmp(&other.0.get())
}
}
impl PartialOrd for Port {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(&other))
}
}
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)]
#[repr(transparent)] #[repr(transparent)]
pub struct PeerKey(pub I32); pub struct PeerKey(pub I32);
@ -92,14 +178,30 @@ impl PeerKey {
} }
} }
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, AsBytes, FromBytes, FromZeroes)] impl Ord for PeerKey {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.get().cmp(&other.0.get())
}
}
impl PartialOrd for PeerKey {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(&other))
}
}
#[derive(
PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Hash, AsBytes, FromBytes, FromZeroes,
)]
#[repr(C, packed)] #[repr(C, packed)]
pub struct ResponsePeer<I: Ip> { pub struct ResponsePeer<I: Ip> {
pub ip_address: I, pub ip_address: I,
pub port: Port, pub port: Port,
} }
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] #[derive(
PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes,
)]
#[repr(transparent)] #[repr(transparent)]
pub struct Ipv4AddrBytes(pub [u8; 4]); pub struct Ipv4AddrBytes(pub [u8; 4]);
@ -117,7 +219,9 @@ impl From<Ipv4Addr> for Ipv4AddrBytes {
} }
} }
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] #[derive(
PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes,
)]
#[repr(transparent)] #[repr(transparent)]
pub struct Ipv6AddrBytes(pub [u8; 16]); pub struct Ipv6AddrBytes(pub [u8; 16]);