Skip to content

Commit

Permalink
Merge pull request #207 from da2ce7/20240715_udp_ordinal_compare
Browse files Browse the repository at this point in the history
dev: add ordinal compare to udp primitive types
  • Loading branch information
greatest-ape authored Jul 16, 2024
2 parents 3b91716 + a4c1bbf commit 62aab2d
Showing 1 changed file with 108 additions and 4 deletions.
112 changes: 108 additions & 4 deletions crates/udp_protocol/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
pub struct InfoHash(pub [u8; 20]);

Expand All @@ -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)]
#[repr(transparent)]
pub struct TransactionId(pub I32);
Expand All @@ -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)]
#[repr(transparent)]
pub struct NumberOfBytes(pub I64);
Expand All @@ -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)]
#[repr(transparent)]
pub struct NumberOfPeers(pub I32);
Expand All @@ -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)]
#[repr(transparent)]
pub struct NumberOfDownloads(pub I32);
Expand All @@ -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)]
#[repr(transparent)]
pub struct Port(pub U16);
Expand All @@ -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)]
#[repr(transparent)]
pub struct PeerKey(pub I32);
Expand All @@ -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)]
pub struct ResponsePeer<I: Ip> {
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]);

Expand All @@ -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)]
pub struct Ipv6AddrBytes(pub [u8; 16]);

Expand Down

0 comments on commit 62aab2d

Please sign in to comment.