From f98b8564f33d4afb2d0c81557da60f1eeeab1bb3 Mon Sep 17 00:00:00 2001 From: Will Song Date: Thu, 4 Apr 2024 13:21:33 -0400 Subject: [PATCH] follow rust guidelines and have PartialOrd rely on Ord when the type is Ord, also saving an unwrap --- .../degree_meta.rs | 39 +++++++++---------- .../src/program_library/report.rs | 29 +++++++------- 2 files changed, 33 insertions(+), 35 deletions(-) diff --git a/program_structure/src/intermediate_representation/degree_meta.rs b/program_structure/src/intermediate_representation/degree_meta.rs index ea486c0..80f83f8 100644 --- a/program_structure/src/intermediate_representation/degree_meta.rs +++ b/program_structure/src/intermediate_representation/degree_meta.rs @@ -17,32 +17,31 @@ pub enum Degree { // Degrees are linearly ordered. impl PartialOrd for Degree { fn partial_cmp(&self, other: &Degree) -> Option { - 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, + } } } diff --git a/program_structure/src/program_library/report.rs b/program_structure/src/program_library/report.rs index 07cc234..3a4816d 100644 --- a/program_structure/src/program_library/report.rs +++ b/program_structure/src/program_library/report.rs @@ -22,26 +22,25 @@ pub enum MessageCategory { /// Message categories are linearly ordered. impl PartialOrd for MessageCategory { fn partial_cmp(&self, other: &Self) -> Option { - 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, + } } }