From 0381fc75258241a11054a614761c4105d86e7e53 Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:34:25 +0200 Subject: [PATCH] fix(grouping): non-canonical ordering The first attempt 02c44c92d0c6c083adb92ad0e61536d40d7b844e 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. --- crates/pacdef_core/src/grouping/group.rs | 5 +---- crates/pacdef_core/src/grouping/section.rs | 5 ++--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/crates/pacdef_core/src/grouping/group.rs b/crates/pacdef_core/src/grouping/group.rs index 8ea0c7b..c8a0d3a 100644 --- a/crates/pacdef_core/src/grouping/group.rs +++ b/crates/pacdef_core/src/grouping/group.rs @@ -94,10 +94,7 @@ fn is_child_of_any_dir(path: &Path, dirs: &[PathBuf]) -> bool { impl PartialOrd for Group { fn partial_cmp(&self, other: &Self) -> Option { - match self.name.partial_cmp(&other.name) { - Some(core::cmp::Ordering::Equal) => None, - ord => ord, - } + Some(self.cmp(other)) } } diff --git a/crates/pacdef_core/src/grouping/section.rs b/crates/pacdef_core/src/grouping/section.rs index 00ad032..f261937 100644 --- a/crates/pacdef_core/src/grouping/section.rs +++ b/crates/pacdef_core/src/grouping/section.rs @@ -67,14 +67,13 @@ impl Eq for Section { impl PartialOrd for Section { fn partial_cmp(&self, other: &Self) -> Option { - self.name.partial_cmp(&other.name) + Some(self.cmp(other)) } } impl Ord for Section { fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.partial_cmp(other) - .expect("partial_cmp compares &str, which provide total order") + self.name.cmp(&other.name) } }