finished custom_ord monstrosity
This commit is contained in:
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use std::cmp::Ordering;
|
||||
|
||||
pub trait CustomOrd<T> {
|
||||
fn cmp(&self, a: &T) -> Ordering;
|
||||
}
|
||||
|
||||
pub enum CustomOrdWrapper<C, T> {
|
||||
CustomOrd(C),
|
||||
Value(T),
|
||||
}
|
||||
|
||||
impl<C, T> Ord for CustomOrdWrapper<C, T>
|
||||
where
|
||||
C: CustomOrd<T>,
|
||||
{
|
||||
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<C, T> PartialOrd for CustomOrdWrapper<C, T>
|
||||
where
|
||||
C: CustomOrd<T>,
|
||||
{
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, T> Eq for CustomOrdWrapper<C, T> where C: CustomOrd<T> {}
|
||||
|
||||
impl<C, T> PartialEq for CustomOrdWrapper<C, T>
|
||||
where
|
||||
C: CustomOrd<T>,
|
||||
{
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.cmp(other).is_eq()
|
||||
}
|
||||
}
|
||||
@@ -226,6 +226,7 @@ along with range_bounds_map. If not, see <https://www.gnu.org/licenses/>.
|
||||
#![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;
|
||||
|
||||
Reference in New Issue
Block a user