From 27fe0e4158b076eb0427aa6b61e7cd42262b0c0a Mon Sep 17 00:00:00 2001 From: ripytide Date: Sat, 1 Apr 2023 17:02:41 +0100 Subject: [PATCH] finished custom_ord monstrosity --- src/custom_ord_wrapper.rs | 70 +++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 71 insertions(+) create mode 100644 src/custom_ord_wrapper.rs diff --git a/src/custom_ord_wrapper.rs b/src/custom_ord_wrapper.rs new file mode 100644 index 0000000..3a7835c --- /dev/null +++ b/src/custom_ord_wrapper.rs @@ -0,0 +1,70 @@ +/* +Copyright 2022 James Forster + +This file is part of range_bounds_map. + +range_bounds_map is free software: you can redistribute it and/or +modify it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +range_bounds_map is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with range_bounds_map. If not, see . +*/ + +use std::cmp::Ordering; + +pub trait CustomOrd { + fn cmp(&self, a: &T) -> Ordering; +} + +pub enum CustomOrdWrapper { + CustomOrd(C), + Value(T), +} + +impl Ord for CustomOrdWrapper +where + C: CustomOrd, +{ + fn cmp(&self, other: &Self) -> Ordering { + match (self, other) { + ( + CustomOrdWrapper::CustomOrd(custom_ord), + CustomOrdWrapper::Value(value), + ) => custom_ord.cmp(value), + ( + CustomOrdWrapper::Value(value), + CustomOrdWrapper::CustomOrd(custom_ord), + ) => custom_ord.cmp(value), + _ => panic!( + "You must have exactly ONE Value and ONE CustomOrd when comparing CustomOrdWrapper's" + ), + } + } +} + +impl PartialOrd for CustomOrdWrapper +where + C: CustomOrd, +{ + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl Eq for CustomOrdWrapper where C: CustomOrd {} + +impl PartialEq for CustomOrdWrapper +where + C: CustomOrd, +{ + fn eq(&self, other: &Self) -> bool { + self.cmp(other).is_eq() + } +} diff --git a/src/lib.rs b/src/lib.rs index 507e14a..69d3512 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -226,6 +226,7 @@ along with range_bounds_map. If not, see . #![allow(clippy::tabs_in_doc_comments)] #![allow(clippy::needless_return)] pub(crate) mod bound_ord; +pub(crate) mod custom_ord_wrapper; pub mod range_bounds_map; pub mod range_bounds_set; pub mod try_from_bounds;