fix(grouping): non-canonical ordering

The first attempt 02c44c92d0 caused
`Section.partial_cmp` and `Section.cmp` to call each other recursively.
That lead to a stack overflow when compiled without optimizations, and
an infinite loop when optimizations were enabled.

This commit now properly implements at first `Ord` for both `Section`
and `Group`, and from this derives `PartialEq` for each.
This commit is contained in:
steven-omaha
2023-10-10 16:34:25 +02:00
parent bd96a9074a
commit 0381fc7525
2 changed files with 3 additions and 7 deletions
+1 -4
View File
@@ -94,10 +94,7 @@ fn is_child_of_any_dir(path: &Path, dirs: &[PathBuf]) -> bool {
impl PartialOrd for Group { impl PartialOrd for Group {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match self.name.partial_cmp(&other.name) { Some(self.cmp(other))
Some(core::cmp::Ordering::Equal) => None,
ord => ord,
}
} }
} }
+2 -3
View File
@@ -67,14 +67,13 @@ impl Eq for Section {
impl PartialOrd for Section { impl PartialOrd for Section {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.name.partial_cmp(&other.name) Some(self.cmp(other))
} }
} }
impl Ord for Section { impl Ord for Section {
fn cmp(&self, other: &Self) -> std::cmp::Ordering { fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.partial_cmp(other) self.name.cmp(&other.name)
.expect("partial_cmp compares &str, which provide total order")
} }
} }