diff --git a/src/bound_ord.rs b/src/bound_ord.rs index 91a035a..9f750de 100644 --- a/src/bound_ord.rs +++ b/src/bound_ord.rs @@ -67,6 +67,18 @@ impl BoundOrd { Bound::Unbounded => BoundOrd::EndUnbounded, } } + + #[trivial] + pub fn as_ref(&self) -> BoundOrd<&T> { + //I can't believe this is neccessary but apparently so + match self { + BoundOrd::Included(x) => BoundOrd::Included(x), + BoundOrd::StartExcluded(x) => BoundOrd::StartExcluded(x), + BoundOrd::StartUnbounded => BoundOrd::StartUnbounded, + BoundOrd::EndExcluded(x) => BoundOrd::EndExcluded(x), + BoundOrd::EndUnbounded => BoundOrd::EndUnbounded, + } + } } impl Ord for BoundOrd diff --git a/src/custom_ord_wrapper.rs b/src/custom_range_bounds_ord_wrapper.rs similarity index 78% rename from src/custom_ord_wrapper.rs rename to src/custom_range_bounds_ord_wrapper.rs index d58afa6..2f28413 100644 --- a/src/custom_ord_wrapper.rs +++ b/src/custom_range_bounds_ord_wrapper.rs @@ -23,16 +23,13 @@ use std::ops::RangeBounds; use crate::bound_ord::BoundOrd; pub enum CustomRangeBoundsOrdWrapper { - //non real BoundOrd(BoundOrd), - - //real RangeBounds(K), } impl Ord for CustomRangeBoundsOrdWrapper where - I: Ord + Clone, + I: Ord, K: RangeBounds, { fn cmp(&self, other: &Self) -> Ordering { @@ -40,15 +37,21 @@ where ( CustomRangeBoundsOrdWrapper::RangeBounds(range_bounds), CustomRangeBoundsOrdWrapper::BoundOrd(bound_ord), - ) => cmp_range_bounds_with_bound_ord(range_bounds, bound_ord), + ) => cmp_range_bounds_with_bound_ord( + range_bounds, + bound_ord.as_ref(), + ), ( CustomRangeBoundsOrdWrapper::BoundOrd(bound_ord), CustomRangeBoundsOrdWrapper::RangeBounds(range_bounds), - ) => cmp_range_bounds_with_bound_ord(range_bounds, bound_ord) - .reverse(), + ) => cmp_range_bounds_with_bound_ord( + range_bounds, + bound_ord.as_ref(), + ) + .reverse(), _ => { panic!( - "You cannot compare a Non-Real CustomOrdWrapper with another Non-Real CustomOrdWrapper!" + "Must have ONE of each real RangeBounds and non-real BoundOrd!" ); } } @@ -57,18 +60,20 @@ where fn cmp_range_bounds_with_bound_ord( range_bounds: &K, - bound_ord: &BoundOrd, + bound_ord: BoundOrd<&I>, ) -> Ordering where - I: Ord + Clone, + I: Ord, K: RangeBounds, { - let start_bound_ord = BoundOrd::start(range_bounds.start_bound().cloned()); - let end_bound_ord = BoundOrd::end(range_bounds.end_bound().cloned()); + //optimisation remove cloning here and all trait bounds that are + //reliant on this + let start_bound_ord = BoundOrd::start(range_bounds.start_bound()); + let end_bound_ord = BoundOrd::end(range_bounds.end_bound()); - if bound_ord < &start_bound_ord { + if bound_ord < start_bound_ord { Ordering::Greater - } else if bound_ord > &end_bound_ord { + } else if bound_ord > end_bound_ord { Ordering::Less } else { Ordering::Equal @@ -77,7 +82,7 @@ where impl PartialOrd for CustomRangeBoundsOrdWrapper where - I: Ord + Clone, + I: Ord, K: RangeBounds, { fn partial_cmp(&self, other: &Self) -> Option { @@ -87,14 +92,14 @@ where impl Eq for CustomRangeBoundsOrdWrapper where - I: Ord + Clone, + I: Ord, K: RangeBounds, { } impl PartialEq for CustomRangeBoundsOrdWrapper where - I: Ord + Clone, + I: Ord, K: RangeBounds, { fn eq(&self, other: &Self) -> bool { diff --git a/src/lib.rs b/src/lib.rs index 69d3512..9a013cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -226,7 +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(crate) mod custom_range_bounds_ord_wrapper; pub mod range_bounds_map; pub mod range_bounds_set; pub mod try_from_bounds; diff --git a/src/range_bounds_map.rs b/src/range_bounds_map.rs index 4ab0b08..13fba0b 100644 --- a/src/range_bounds_map.rs +++ b/src/range_bounds_map.rs @@ -33,7 +33,6 @@ use serde::ser::SerializeMap; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::bound_ord::BoundOrd; -use crate::custom_ord_wrapper::CustomOrdWrapper; use crate::TryFromBounds; /// An ordered map of non-overlapping [`RangeBounds`] based on [`BTreeMap`].