follow rust guidelines and have PartialOrd rely on Ord when the type is Ord, also saving an unwrap

This commit is contained in:
Will Song
2024-04-04 13:21:33 -04:00
parent 01dcabe383
commit f98b8564f3
2 changed files with 33 additions and 35 deletions

View File

@@ -17,32 +17,31 @@ pub enum Degree {
// Degrees are linearly ordered.
impl PartialOrd<Degree> for Degree {
fn partial_cmp(&self, other: &Degree) -> Option<Ordering> {
use Degree::*;
match (self, other) {
// `Constant <= _`
(Constant, Constant) => Some(Ordering::Equal),
(Constant, Linear) | (Constant, Quadratic) | (Constant, NonQuadratic) => {
Some(Ordering::Less)
}
// `Linear <= _`
(Linear, Linear) => Some(Ordering::Equal),
(Linear, Quadratic) | (Linear, NonQuadratic) => Some(Ordering::Less),
// `Quadratic <= _`
(Quadratic, Quadratic) => Some(Ordering::Equal),
(Quadratic, NonQuadratic) => Some(Ordering::Less),
// `NonQuadratic <= _`
(NonQuadratic, NonQuadratic) => Some(Ordering::Equal),
// All other cases are on the form `_ >= _`.
_ => Some(Ordering::Greater),
}
Some(self.cmp(other))
}
}
// Degrees are linearly ordered.
impl Ord for Degree {
fn cmp(&self, other: &Degree) -> Ordering {
// `Degree::partial_cmp` always returns `Some(_)`.
self.partial_cmp(other).unwrap()
use Degree::*;
match (self, other) {
// `Constant <= _`
(Constant, Constant) => Ordering::Equal,
(Constant, Linear) | (Constant, Quadratic) | (Constant, NonQuadratic) => {
Ordering::Less
}
// `Linear <= _`
(Linear, Linear) => Ordering::Equal,
(Linear, Quadratic) | (Linear, NonQuadratic) => Ordering::Less,
// `Quadratic <= _`
(Quadratic, Quadratic) => Ordering::Equal,
(Quadratic, NonQuadratic) => Ordering::Less,
// `NonQuadratic <= _`
(NonQuadratic, NonQuadratic) => Ordering::Equal,
// All other cases are on the form `_ >= _`.
_ => Ordering::Greater,
}
}
}

View File

@@ -22,26 +22,25 @@ pub enum MessageCategory {
/// Message categories are linearly ordered.
impl PartialOrd for MessageCategory {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
use MessageCategory::*;
match (self, other) {
// `Info <= _`
(Info, Info) => Some(Ordering::Equal),
(Info, Warning) | (Info, Error) => Some(Ordering::Less),
// `Warning <= _`
(Warning, Warning) => Some(Ordering::Equal),
(Warning, Error) => Some(Ordering::Less),
// `Error <= _`
(Error, Error) => Some(Ordering::Equal),
// All other cases are on the form `_ >= _`.
_ => Some(Ordering::Greater),
}
Some(self.cmp(other))
}
}
impl Ord for MessageCategory {
fn cmp(&self, other: &Self) -> Ordering {
// `MessageCategory::partial_cmp` always returns `Some(_)`.
self.partial_cmp(other).unwrap()
use MessageCategory::*;
match (self, other) {
// `Info <= _`
(Info, Info) => Ordering::Equal,
(Info, Warning) | (Info, Error) => Ordering::Less,
// `Warning <= _`
(Warning, Warning) => Ordering::Equal,
(Warning, Error) => Ordering::Less,
// `Error <= _`
(Error, Error) => Ordering::Equal,
// All other cases are on the form `_ >= _`.
_ => Ordering::Greater,
}
}
}