diff --git a/rustfmt.toml b/rustfmt.toml index 6e33442..ff642d2 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,10 +1,3 @@ -edition="2024" -version = "Two" hard_tabs=true imports_granularity="Module" group_imports="StdExternalCrate" -max_width=80 -format_code_in_doc_comments=true -doc_comment_code_block_width=70 -unstable_features = true -comment_width=70 diff --git a/src/discrete_bounds.rs b/src/discrete_bounds.rs index ac92dd7..2c66e61 100644 --- a/src/discrete_bounds.rs +++ b/src/discrete_bounds.rs @@ -17,6 +17,10 @@ You should have received a copy of the GNU Affero General Public License along with range_bounds_map. If not, see . */ +use std::ops::Bound; + +use crate::stepable::Stepable; + pub struct DiscreteBounds { start: DiscreteBound, end: DiscreteBound, @@ -26,3 +30,23 @@ pub enum DiscreteBound { Included(I), Unbounded, } + +impl DiscreteBound +where + I: Stepable, +{ + pub fn start(bound: Bound) -> Self { + match bound { + Bound::Included(x) => DiscreteBound::Included(x), + Bound::Excluded(x) => DiscreteBound::Included(x.up().unwrap()), + Bound::Unbounded => DiscreteBound::Unbounded, + } + } + pub fn end(bound: Bound) -> Self { + match bound { + Bound::Included(x) => DiscreteBound::Included(x), + Bound::Excluded(x) => DiscreteBound::Included(x.down().unwrap()), + Bound::Unbounded => DiscreteBound::Unbounded, + } + } +} diff --git a/src/range_bounds_map.rs b/src/range_bounds_map.rs index c042a98..331f197 100644 --- a/src/range_bounds_map.rs +++ b/src/range_bounds_map.rs @@ -33,7 +33,7 @@ use serde::ser::SerializeMap; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::bound_ord::DiscreteBoundOrd; -use crate::discrete_bounds::DiscreteBounds; +use crate::discrete_bounds::{DiscreteBound, DiscreteBounds}; use crate::stepable::Stepable; use crate::utils::{ cmp_range_with_discrete_bound_ord, cut_range, flip_bound, is_valid_range, @@ -1759,7 +1759,7 @@ where /// A simple helper trait to make my implemtation nicer, if you /// already implement RangeBounds and Copy on your type then this will /// also be implemted. -pub trait DiscreteRange: Copy { +pub trait DiscreteRange { fn start(&self) -> DiscreteBoundOrd; fn end(&self) -> DiscreteBoundOrd; } @@ -1768,11 +1768,13 @@ where I: Copy + Stepable, K: RangeBounds + Copy, { - fn start(&self) -> Bound { - self.start_bound().cloned() + fn start(&self) -> DiscreteBoundOrd { + DiscreteBoundOrd::start(DiscreteBound::start( + self.start_bound().cloned(), + )) } - fn end(&self) -> Bound { - self.end_bound().cloned() + fn end(&self) -> DiscreteBoundOrd { + DiscreteBoundOrd::end(DiscreteBound::end(self.end_bound().cloned())) } } diff --git a/src/utils.rs b/src/utils.rs index e0f0435..669a9bb 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -59,8 +59,8 @@ where match a.start() < b.start() { true => { match ( - contains_bound_ord(a, DiscreteBoundOrd::start(b.start())), - contains_bound_ord(a, DiscreteBoundOrd::end(b.end())), + contains_bound_ord(a, b.start()), + contains_bound_ord(a, b.end()), ) { (false, false) => Config::LeftFirstNonOverlapping, (true, false) => Config::LeftFirstPartialOverlap, @@ -70,8 +70,8 @@ where } false => { match ( - contains_bound_ord(b, DiscreteBoundOrd::start(a.start())), - contains_bound_ord(b, DiscreteBoundOrd::end(a.end())), + contains_bound_ord(b, a.start()), + contains_bound_ord(b, a.end()), ) { (false, false) => Config::RightFirstNonOverlapping, (true, false) => Config::RightFirstPartialOverlap, @@ -83,9 +83,9 @@ where } enum SortedConfig { - NonOverlapping((Bound, Bound), (Bound, Bound)), - PartialOverlap((Bound, Bound), (Bound, Bound)), - Swallowed((Bound, Bound), (Bound, Bound)), + NonOverlapping((DiscreteBoundOrd, DiscreteBoundOrd), (DiscreteBoundOrd, DiscreteBoundOrd)), + PartialOverlap((DiscreteBoundOrd, DiscreteBoundOrd), (DiscreteBoundOrd, DiscreteBoundOrd)), + Swallowed((DiscreteBoundOrd, DiscreteBoundOrd), (DiscreteBoundOrd, DiscreteBoundOrd)), } fn sorted_config(a: A, b: B) -> SortedConfig where