From a4c1bbfe55c1c140b0a3e082ac273334adb0795f Mon Sep 17 00:00:00 2001 From: Cameron Garnham Date: Mon, 15 Jul 2024 14:37:57 +0200 Subject: [PATCH] dev: add ordinal compare to udp primitive types --- crates/udp_protocol/src/common.rs | 112 ++++++++++++++++++++++++++++-- 1 file changed, 108 insertions(+), 4 deletions(-) diff --git a/crates/udp_protocol/src/common.rs b/crates/udp_protocol/src/common.rs index 51fbfaf5..01e5b1cf 100644 --- a/crates/udp_protocol/src/common.rs +++ b/crates/udp_protocol/src/common.rs @@ -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 { + Some(self.cmp(&other)) + } +} + +#[derive( + PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes, +)] #[repr(transparent)] 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 { + Some(self.cmp(&other)) + } +} + #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] #[repr(transparent)] 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 { + Some(self.cmp(&other)) + } +} + #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] #[repr(transparent)] 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 { + Some(self.cmp(&other)) + } +} + #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] #[repr(transparent)] 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 { + Some(self.cmp(&other)) + } +} + #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] #[repr(transparent)] 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 { + Some(self.cmp(&other)) + } +} + #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] #[repr(transparent)] 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 { + Some(self.cmp(&other)) + } +} + #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, AsBytes, FromBytes, FromZeroes)] #[repr(transparent)] 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 { + Some(self.cmp(&other)) + } +} + +#[derive( + PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Hash, AsBytes, FromBytes, FromZeroes, +)] #[repr(C, packed)] pub struct ResponsePeer { pub ip_address: I, 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)] pub struct Ipv4AddrBytes(pub [u8; 4]); @@ -117,7 +219,9 @@ impl From 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)] pub struct Ipv6AddrBytes(pub [u8; 16]);