Skip to content

Commit

Permalink
Manually implement Ord for HTTPVersion
Browse files Browse the repository at this point in the history
As `Ord` requires a full order relationship, it makes little sense to
implement `PartialOrd` ourselves and then derive `Ord`, doing so might
even result in unexpected behaviour if the implementation of the derive
changes underneath us.

Here we move our comparison logic into the `impl Ord` and call that from
our `impl PartialOrd`.
  • Loading branch information
bradfier committed Apr 28, 2021
1 parent edcca34 commit 37e18c5
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ impl PartialEq for Method {
impl Eq for Method {}

/// HTTP version (usually 1.0 or 1.1).
#[derive(Debug, Clone, PartialEq, Eq, Ord)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HTTPVersion(pub u8, pub u8);

impl Display for HTTPVersion {
Expand All @@ -381,16 +381,22 @@ impl Display for HTTPVersion {
}
}

impl PartialOrd for HTTPVersion {
fn partial_cmp(&self, other: &HTTPVersion) -> Option<Ordering> {
impl Ord for HTTPVersion {
fn cmp(&self, other: &Self) -> Ordering {
let HTTPVersion(my_major, my_minor) = *self;
let HTTPVersion(other_major, other_minor) = *other;

if my_major != other_major {
return my_major.partial_cmp(&other_major);
return my_major.cmp(&other_major);
}

my_minor.partial_cmp(&other_minor)
my_minor.cmp(&other_minor)
}
}

impl PartialOrd for HTTPVersion {
fn partial_cmp(&self, other: &HTTPVersion) -> Option<Ordering> {
Some(self.cmp(other))
}
}

Expand Down

0 comments on commit 37e18c5

Please sign in to comment.