diff --git a/src/discrete_bound_ord.rs b/src/discrete_bound_ord.rs deleted file mode 100644 index 8655071..0000000 --- a/src/discrete_bound_ord.rs +++ /dev/null @@ -1,143 +0,0 @@ -/* -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; - -use serde::{Deserialize, Serialize}; - -use crate::discrete_bounds::DiscreteBound; -use crate::stepable::Stepable; - -/// An newtype of [`Bound`] to implement [`Ord`] on types that -/// implement [`Step`]. -/// -/// [`Step`]: std::iter::Step -#[derive(Debug, Copy, Clone, Serialize, Deserialize)] -pub enum DiscreteBoundOrd { - Included(T), - StartUnbounded, - EndUnbounded, -} - -impl DiscreteBoundOrd { - pub(crate) fn start(discrete_bound: DiscreteBound) -> Self { - match discrete_bound { - DiscreteBound::Included(point) => DiscreteBoundOrd::Included(point), - DiscreteBound::Unbounded => DiscreteBoundOrd::StartUnbounded, - } - } - pub(crate) fn end(discrete_bound: DiscreteBound) -> Self { - match discrete_bound { - DiscreteBound::Included(point) => DiscreteBoundOrd::Included(point), - DiscreteBound::Unbounded => DiscreteBoundOrd::EndUnbounded, - } - } - - pub fn up_if_finite(&self) -> DiscreteBoundOrd - where - I: Stepable + Copy, - { - match self { - DiscreteBoundOrd::Included(x) => DiscreteBoundOrd::Included(x.up().unwrap()), - x => *x, - } - } - pub fn down_if_finite(&self) -> DiscreteBoundOrd - where - I: Stepable + Copy, - { - match self { - DiscreteBoundOrd::Included(x) => DiscreteBoundOrd::Included(x.down().unwrap()), - x => *x, - } - } -} - -impl Ord for DiscreteBoundOrd -where - T: Ord, -{ - #[rustfmt::skip] - fn cmp(&self, other: &Self) -> Ordering { - match (self, other) { - (DiscreteBoundOrd::Included(start1), DiscreteBoundOrd::Included(start2)) => start1.cmp(start2), - (DiscreteBoundOrd::Included(_), DiscreteBoundOrd::EndUnbounded) => Ordering::Less, - (DiscreteBoundOrd::Included(_), DiscreteBoundOrd::StartUnbounded) => Ordering::Greater, - - (DiscreteBoundOrd::StartUnbounded, DiscreteBoundOrd::Included(_)) => Ordering::Less, - (DiscreteBoundOrd::StartUnbounded, DiscreteBoundOrd::StartUnbounded) => Ordering::Equal, - (DiscreteBoundOrd::StartUnbounded, DiscreteBoundOrd::EndUnbounded) => Ordering::Less, - - (DiscreteBoundOrd::EndUnbounded, DiscreteBoundOrd::Included(_)) => Ordering::Greater, - (DiscreteBoundOrd::EndUnbounded, DiscreteBoundOrd::EndUnbounded) => Ordering::Equal, - (DiscreteBoundOrd::EndUnbounded, DiscreteBoundOrd::StartUnbounded) => Ordering::Greater, - } -} -} - -impl PartialOrd for DiscreteBoundOrd -where - T: Ord, -{ - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} - -impl PartialEq for DiscreteBoundOrd -where - T: Ord, -{ - fn eq(&self, other: &Self) -> bool { - self.cmp(other).is_eq() - } -} - -impl Eq for DiscreteBoundOrd where T: Ord {} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn mass_start_bound_partial_ord_test() { - //Included - assert!(DiscreteBoundOrd::Included(2) == DiscreteBoundOrd::Included(2)); - assert!(DiscreteBoundOrd::Included(2) <= DiscreteBoundOrd::Included(2)); - assert!(DiscreteBoundOrd::Included(2) >= DiscreteBoundOrd::Included(2)); - assert!(DiscreteBoundOrd::Included(0) < DiscreteBoundOrd::Included(2)); - assert!(DiscreteBoundOrd::Included(2) > DiscreteBoundOrd::Included(0)); - - assert!(DiscreteBoundOrd::Included(2) > DiscreteBoundOrd::StartUnbounded); - - assert!(DiscreteBoundOrd::Included(2) < DiscreteBoundOrd::EndUnbounded); - - //StartUnbounded - assert!(DiscreteBoundOrd::StartUnbounded:: == DiscreteBoundOrd::StartUnbounded); - assert!(DiscreteBoundOrd::StartUnbounded:: <= DiscreteBoundOrd::StartUnbounded); - assert!(DiscreteBoundOrd::StartUnbounded:: >= DiscreteBoundOrd::StartUnbounded); - - assert!(DiscreteBoundOrd::StartUnbounded:: < DiscreteBoundOrd::EndUnbounded); - - //EndUnbounded - assert!(DiscreteBoundOrd::EndUnbounded:: == DiscreteBoundOrd::EndUnbounded); - assert!(DiscreteBoundOrd::EndUnbounded:: <= DiscreteBoundOrd::EndUnbounded); - assert!(DiscreteBoundOrd::EndUnbounded:: >= DiscreteBoundOrd::EndUnbounded); - } -} diff --git a/src/discrete_bounds.rs b/src/discrete_bounds.rs index a9006c4..7b5719c 100644 --- a/src/discrete_bounds.rs +++ b/src/discrete_bounds.rs @@ -19,87 +19,18 @@ along with range_bounds_map. If not, see . use std::ops::{Bound, RangeBounds}; -use crate::discrete_bound_ord::DiscreteBoundOrd; -use crate::stepable::Stepable; -use crate::try_from_discrete_bounds::TryFromDiscreteBounds; - #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct DiscreteBounds { - pub start: DiscreteBound, - pub end: DiscreteBound, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum DiscreteBound { - Included(I), - Unbounded, -} - -impl DiscreteBound -where - I: Stepable + Copy, -{ - 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, - } - } - - pub fn up_if_finite(&self) -> DiscreteBound { - match self { - DiscreteBound::Included(x) => DiscreteBound::Included(x.up().unwrap()), - DiscreteBound::Unbounded => DiscreteBound::Unbounded, - } - } - pub fn down_if_finite(&self) -> DiscreteBound { - match self { - DiscreteBound::Included(x) => DiscreteBound::Included(x.down().unwrap()), - DiscreteBound::Unbounded => DiscreteBound::Unbounded, - } - } -} - -impl From> for DiscreteBound { - fn from(discrete_bound_ord: DiscreteBoundOrd) -> Self { - match discrete_bound_ord { - DiscreteBoundOrd::Included(x) => DiscreteBound::Included(x), - DiscreteBoundOrd::StartUnbounded => DiscreteBound::Unbounded, - DiscreteBoundOrd::EndUnbounded => DiscreteBound::Unbounded, - } - } + //both are always included + pub start: I, + pub end: I, } impl RangeBounds for DiscreteBounds { fn start_bound(&self) -> Bound<&I> { - match self.start { - DiscreteBound::Included(ref x) => Bound::Included(x), - DiscreteBound::Unbounded => Bound::Unbounded, - } + Bound::Included(&self.start) } fn end_bound(&self) -> Bound<&I> { - match self.end { - DiscreteBound::Included(ref x) => Bound::Included(x), - DiscreteBound::Unbounded => Bound::Unbounded, - } - } -} - -impl TryFromDiscreteBounds for DiscreteBounds { - fn try_from_discrete_bounds( - discrete_bounds: DiscreteBounds, - ) -> Result - where - Self: Sized, - { - Ok(discrete_bounds) + Bound::Included(&self.end) } } diff --git a/src/lib.rs b/src/lib.rs index 9d49b1e..5fe1b2c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -220,18 +220,14 @@ along with range_bounds_map. If not, see . pub mod test_ranges; pub(crate) mod utils; -pub(crate) mod discrete_bound_ord; pub mod discrete_bounds; - pub mod stepable; -pub mod try_from_discrete_bounds; pub mod range_bounds_map; pub mod range_bounds_set; +pub use crate::discrete_bounds::DiscreteBounds; pub use crate::range_bounds_map::{ OverlapError, OverlapOrTryFromDiscreteBoundsError, RangeBoundsMap, }; pub use crate::range_bounds_set::RangeBoundsSet; -pub use crate::try_from_discrete_bounds::{TryFromDiscreteBounds, TryFromDiscreteBoundsError}; -pub use crate::discrete_bounds::DiscreteBounds; diff --git a/src/range_bounds_map.rs b/src/range_bounds_map.rs index 599f12a..ce1e9ee 100644 --- a/src/range_bounds_map.rs +++ b/src/range_bounds_map.rs @@ -30,11 +30,9 @@ use serde::de::{MapAccess, Visitor}; use serde::ser::SerializeMap; use serde::{Deserialize, Deserializer, Serialize, Serializer}; -use crate::discrete_bound_ord::DiscreteBoundOrd; -use crate::discrete_bounds::{DiscreteBound, DiscreteBounds}; -use crate::stepable::Stepable; -use crate::try_from_discrete_bounds::{TryFromDiscreteBounds, TryFromDiscreteBoundsError}; -use crate::utils::{cmp_discrete_bound_ord_with_range, cut_range, is_valid_range, overlaps}; +use crate::discrete_bounds::DiscreteBounds; +use crate::stepable::Discrete; +use crate::utils::{cmp_point_with_range, cut_range, is_valid_range, overlaps}; /// An ordered map of non-overlapping ranges based on [`BTreeMap`]. /// @@ -140,17 +138,9 @@ pub struct RangeBoundsMap { #[derive(PartialEq, Debug)] pub struct OverlapError; -/// An error type to represent either an [`OverlapError`] or a -/// [`TryFromDiscreteBoundsError`]. -#[derive(PartialEq, Debug)] -pub enum OverlapOrTryFromDiscreteBoundsError { - Overlap(OverlapError), - TryFromDiscreteBounds(TryFromDiscreteBoundsError), -} - impl RangeBoundsMap where - I: Ord + Copy + Stepable, + I: Ord + Copy + Discrete, K: DiscreteRange + Copy, { /// Makes a new, empty `RangeBoundsMap`. @@ -370,8 +360,7 @@ where /// assert_eq!(map.get_at_point(1), Some(&true)); /// ``` pub fn get_at_point_mut(&mut self, point: I) -> Option<&mut V> { - self.inner - .get_mut(overlapping_comp(DiscreteBoundOrd::Included(point))) + self.inner.get_mut(overlapping_comp(point)) } /// Returns `true` if the map contains a range that overlaps the @@ -430,26 +419,24 @@ where /// ``` pub fn get_entry_at_point(&self, point: I) -> Result<(&K, &V), DiscreteBounds> { self.inner - .get_key_value(overlapping_comp(DiscreteBoundOrd::Included(point))) - .ok_or_else(|| self.get_gap_at_raw(DiscreteBoundOrd::Included(point))) + .get_key_value(overlapping_comp(point)) + .ok_or_else(|| self.get_gap_at_raw(point)) } - fn get_gap_at_raw(&self, discrete_bound_ord: DiscreteBoundOrd) -> DiscreteBounds { - let lower = self.inner.upper_bound( - overlapping_comp(discrete_bound_ord), - SearchBoundCustom::Included, - ); - let upper = self.inner.lower_bound( - overlapping_comp(discrete_bound_ord), - SearchBoundCustom::Included, - ); + fn get_gap_at_raw(&self, point: I) -> DiscreteBounds { + let lower = self + .inner + .upper_bound(overlapping_comp(point), SearchBoundCustom::Included); + let upper = self + .inner + .lower_bound(overlapping_comp(point), SearchBoundCustom::Included); DiscreteBounds { - start: lower.key().map_or(DiscreteBound::Unbounded, |lower| { - DiscreteBound::from(lower.end()).up_if_finite() - }), - end: upper.key().map_or(DiscreteBound::Unbounded, |upper| { - DiscreteBound::from(upper.start()).down_if_finite() - }), + start: lower + .key() + .map_or(I::MIN, |lower| lower.end().up().unwrap()), + end: upper + .key() + .map_or(I::MAX, |upper| upper.start().down().unwrap()), } } @@ -601,13 +588,10 @@ where /// ); /// assert_eq!(base, after_cut); /// ``` - pub fn cut<'a, Q>( - &'a mut self, - range: Q, - ) -> Result, V)> + '_, TryFromDiscreteBoundsError> + pub fn cut<'a, Q>(&'a mut self, range: Q) -> impl Iterator, V)> + '_ where Q: DiscreteRange + Copy + 'a, - K: TryFromDiscreteBounds, + K: From>, V: Clone, { invalid_range_panic(range); @@ -636,10 +620,10 @@ where &mut self, range: Q, single_overlapping_range: K, - ) -> Result, V)>, TryFromDiscreteBoundsError> + ) -> impl Iterator, V)> where Q: DiscreteRange + Copy, - K: TryFromDiscreteBounds, + K: From>, V: Clone, { invalid_range_panic(range); @@ -670,10 +654,10 @@ where range: Q, left_overlapping: Option, right_overlapping: Option, - ) -> Result, V)> + '_, TryFromDiscreteBoundsError> + ) -> impl Iterator, V)> + '_ where Q: DiscreteRange + Copy + 'a, - K: TryFromDiscreteBounds, + K: From>, V: Clone, { invalid_range_panic(range); @@ -799,41 +783,24 @@ where (Some(mut start_gap), Some(mut end_gap)) => { if start_gap.start() == end_gap.start() { //it's the same gap - if let DiscreteBoundOrd::Included(outer_range_start) = outer_range.start() { - start_gap.start = DiscreteBound::Included(outer_range_start); - } - if let DiscreteBoundOrd::Included(outer_range_end) = outer_range.end() { - start_gap.end = DiscreteBound::Included(outer_range_end); - } + start_gap.start = outer_range.start(); + start_gap.end = outer_range.end(); (Some(start_gap), None) } else { //it's different gaps - // - //trim the start gap to the outer range - if let DiscreteBoundOrd::Included(outer_range_start) = outer_range.start() { - start_gap.start = DiscreteBound::Included(outer_range_start); - } - //trim the end gap to the outer range - if let DiscreteBoundOrd::Included(outer_range_end) = outer_range.end() { - end_gap.end = DiscreteBound::Included(outer_range_end); - } + start_gap.start = outer_range.start(); + end_gap.end = outer_range.end(); (Some(start_gap), Some(end_gap)) } } (Some(mut start_gap), None) => { - //trim the start gap to the outer range - if let DiscreteBoundOrd::Included(outer_range_start) = outer_range.start() { - start_gap.start = DiscreteBound::Included(outer_range_start); - } + start_gap.start = outer_range.start(); (Some(start_gap), None) } (None, Some(mut end_gap)) => { - //trim the end gap to the outer range - if let DiscreteBoundOrd::Included(outer_range_end) = outer_range.end() { - end_gap.end = DiscreteBound::Included(outer_range_end); - } + end_gap.end = outer_range.end(); (None, Some(end_gap)) } (None, None) => (None, None), @@ -942,9 +909,9 @@ where get_end: G2, remove_start: R1, remove_end: R2, - ) -> Result + ) -> K where - K: TryFromDiscreteBounds, + K: From>, G1: FnOnce(&Self, &V) -> Option, G2: FnOnce(&Self, &V) -> Option, R1: FnOnce(&mut Self, &V), @@ -1043,21 +1010,17 @@ where /// [(ie(1, 8), true), (ie(10, 16), false)] /// ); /// ``` - pub fn insert_merge_touching( - &mut self, - range: K, - value: V, - ) -> Result + pub fn insert_merge_touching(&mut self, range: K, value: V) -> Result where - K: TryFromDiscreteBounds, + K: From>, { invalid_range_panic(range); if self.overlaps(range) { - return Err(OverlapOrTryFromDiscreteBoundsError::Overlap(OverlapError)); + return Err(OverlapError); } - self.insert_merge_with_comps( + Ok(self.insert_merge_with_comps( range, value, |selfy, _| { @@ -1080,8 +1043,7 @@ where |selfy, _| { selfy.inner.remove(touching_end_comp(range.end())); }, - ) - .map_err(OverlapOrTryFromDiscreteBoundsError::TryFromDiscreteBounds) + )) } /// Adds a new entry to the map and merges into other ranges in @@ -1146,15 +1108,15 @@ where &mut self, range: K, value: V, - ) -> Result + ) -> Result where - K: TryFromDiscreteBounds, + K: From>, V: Eq, { invalid_range_panic(range); if self.overlaps(range) { - return Err(OverlapOrTryFromDiscreteBoundsError::Overlap(OverlapError)); + return Err(OverlapError); } let get_start = |selfy: &Self, value: &V| { @@ -1174,7 +1136,7 @@ where .copied() }; - self.insert_merge_with_comps( + Ok(self.insert_merge_with_comps( range, value, get_start, @@ -1189,8 +1151,7 @@ where selfy.inner.remove(touching_end_comp(range.end())); } }, - ) - .map_err(OverlapOrTryFromDiscreteBoundsError::TryFromDiscreteBounds) + )) } /// Adds a new entry to the map and merges into other ranges in @@ -1248,13 +1209,9 @@ where /// [(ie(1, 4), false), (ie(4, 8), false), (ie(10, 16), false)] /// ); /// ``` - pub fn insert_merge_overlapping( - &mut self, - range: K, - value: V, - ) -> Result + pub fn insert_merge_overlapping(&mut self, range: K, value: V) -> K where - K: TryFromDiscreteBounds, + K: From>, { invalid_range_panic(range); @@ -1335,13 +1292,9 @@ where /// [(ie(1, 8), false), (ie(10, 16), false)] /// ); /// ``` - pub fn insert_merge_touching_or_overlapping( - &mut self, - range: K, - value: V, - ) -> Result + pub fn insert_merge_touching_or_overlapping(&mut self, range: K, value: V) -> K where - K: TryFromDiscreteBounds, + K: From>, { invalid_range_panic(range); @@ -1412,17 +1365,15 @@ where /// [(ie(2, 4), false), (ie(4, 6), true), (ie(6, 8), false)] /// ); /// ``` - pub fn insert_overwrite(&mut self, range: K, value: V) -> Result<(), TryFromDiscreteBoundsError> + pub fn insert_overwrite(&mut self, range: K, value: V) where - K: TryFromDiscreteBounds, + K: From>, V: Clone, { invalid_range_panic(range); let _ = self.cut(range)?; self.insert_unchecked(range, value); - - return Ok(()); } /// Returns the first entry in the map, if any. @@ -1520,88 +1471,38 @@ where fn double_comp() -> impl FnMut(&K, &K) -> Ordering where K: DiscreteRange, - I: Ord + Stepable, + I: Ord + Discrete, { |inner_range: &K, new_range: &K| new_range.start().cmp(&inner_range.start()) } -fn overlapping_comp(bound: DiscreteBoundOrd) -> impl FnMut(&K) -> Ordering +fn overlapping_comp(point: I) -> impl FnMut(&K) -> Ordering where I: Ord + Copy, K: DiscreteRange + Copy, { - move |inner_range: &K| cmp_discrete_bound_ord_with_range(bound, *inner_range) + move |inner_range: &K| cmp_point_with_range(point, *inner_range) } -fn touching_start_comp(start: DiscreteBoundOrd) -> impl FnMut(&K) -> Ordering +fn touching_start_comp(start: I) -> impl FnMut(&K) -> Ordering where - I: Ord + Copy + Stepable, + I: Ord + Copy + Discrete, K: DiscreteRange, { - move |inner_range: &K| match (inner_range.end(), start) { - //we only allow Ordering::Equal here since if they are equal - //then the ranges would be touching - (DiscreteBoundOrd::Included(end), DiscreteBoundOrd::Included(start)) - if end.up().unwrap() == start => - { - Ordering::Equal - } - - (end, start) => { - let normal_result = start.cmp(&end); - - //we overide any Equals to a non-Equal since we - //don't want non-touching matches - match normal_result { - Ordering::Equal => Ordering::Greater, - x => x, - } - } - } + move |inner_range: &K| start.cmp(&inner_range.end().up().unwrap()) } -fn touching_end_comp(end: DiscreteBoundOrd) -> impl FnMut(&K) -> Ordering +fn touching_end_comp(end: I) -> impl FnMut(&K) -> Ordering where - I: Ord + Copy + Stepable, + I: Ord + Copy + Discrete, K: DiscreteRange, { - move |inner_range: &K| match (end, inner_range.start()) { - //we only allow Ordering::Equal here since if they are equal - //then the ranges would be touching - (DiscreteBoundOrd::Included(end), DiscreteBoundOrd::Included(start)) - if end.up().unwrap() == start => - { - Ordering::Equal - } - - (end, _start) => { - let normal_result = end.cmp(&inner_range.start()); - - //we overide any Equals to a non-Equal since we - //don't want non-touching matches - match normal_result { - Ordering::Equal => Ordering::Less, - x => x, - } - } - } + move |inner_range: &K| end.cmp(&inner_range.start().down().unwrap()) } /// 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 { - fn start(&self) -> DiscreteBoundOrd; - fn end(&self) -> DiscreteBoundOrd; -} -impl DiscreteRange for K -where - I: Copy + Stepable, - K: RangeBounds + Copy, -{ - fn start(&self) -> DiscreteBoundOrd { - DiscreteBoundOrd::start(DiscreteBound::start(self.start_bound().cloned())) - } - fn end(&self) -> DiscreteBoundOrd { - DiscreteBoundOrd::end(DiscreteBound::end(self.end_bound().cloned())) - } + fn start(&self) -> I; + fn end(&self) -> I; } // Trait Impls ========================== @@ -1646,7 +1547,7 @@ impl Default for RangeBoundsMap { impl Serialize for RangeBoundsMap where - I: Ord + Copy + Stepable, + I: Ord + Copy + Discrete, K: DiscreteRange + Copy + Serialize, V: Serialize, { @@ -1664,7 +1565,7 @@ where impl<'de, I, K, V> Deserialize<'de> for RangeBoundsMap where - I: Ord + Copy + Stepable, + I: Ord + Copy + Discrete, K: DiscreteRange + Copy + Deserialize<'de>, V: Deserialize<'de>, { @@ -1688,7 +1589,7 @@ struct RangeBoundsMapVisitor { impl<'de, I, K, V> Visitor<'de> for RangeBoundsMapVisitor where - I: Ord + Copy + Stepable, + I: Ord + Copy + Discrete, K: DiscreteRange + Copy + Deserialize<'de>, V: Deserialize<'de>, { @@ -1717,7 +1618,6 @@ mod tests { use super::*; use crate::test_ranges::{ee, ei, ie, ii, iu, ue, ui, uu}; - use crate::try_from_discrete_bounds::TryFromDiscreteBounds; use crate::utils::{config, Config, CutResult}; //only every other number to allow mathematical_overlapping_definition @@ -1926,13 +1826,13 @@ mod tests { ); } - fn assert_cut( + fn assert_cut( mut before: RangeBoundsMap, to_cut: Q, result: Result<[(DiscreteBounds, V); Y], TryFromDiscreteBoundsError>, after: Option<[(K, V); N]>, ) where - I: Ord + Debug + Copy + Stepable, + I: Ord + Debug + Copy + Discrete, K: DiscreteRange + TryFromDiscreteBounds + PartialEq + Debug + Copy, Q: DiscreteRange + Copy, V: PartialEq + Debug + Clone, @@ -2031,7 +1931,7 @@ mod tests { result: Result, after: Option<[(K, V); N]>, ) where - I: Ord + Debug + Copy + Stepable, + I: Ord + Debug + Copy + Discrete, K: DiscreteRange + TryFromDiscreteBounds + PartialEq + Debug + Copy, V: PartialEq + Debug + Clone, { @@ -2096,7 +1996,7 @@ mod tests { result: Result, after: Option<[(K, V); N]>, ) where - I: Ord + Debug + Copy + Stepable, + I: Ord + Debug + Copy + Discrete, K: DiscreteRange + TryFromDiscreteBounds + PartialEq + Debug + Copy, V: Eq + Debug + Clone, { @@ -2162,7 +2062,7 @@ mod tests { result: Result, after: Option<[(K, V); N]>, ) where - I: Ord + Debug + Copy + Stepable, + I: Ord + Debug + Copy + Discrete, K: DiscreteRange + TryFromDiscreteBounds + PartialEq + Debug + Copy, V: PartialEq + Debug + Clone, { @@ -2248,7 +2148,7 @@ mod tests { result: Result, after: Option<[(K, V); N]>, ) where - I: Ord + Debug + Copy + Stepable, + I: Ord + Debug + Copy + Discrete, K: DiscreteRange + TryFromDiscreteBounds + PartialEq + Debug + Copy, V: PartialEq + Debug + Clone, { diff --git a/src/range_bounds_set.rs b/src/range_bounds_set.rs index ef3dced..c9823e4 100644 --- a/src/range_bounds_set.rs +++ b/src/range_bounds_set.rs @@ -7,7 +7,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::discrete_bounds::DiscreteBounds; use crate::range_bounds_map::{DiscreteRange, IntoIter as RangeBoundsMapIntoIter}; -use crate::stepable::Stepable; +use crate::stepable::Discrete; use crate::try_from_discrete_bounds::TryFromDiscreteBounds; use crate::{ OverlapError, OverlapOrTryFromDiscreteBoundsError, RangeBoundsMap, TryFromDiscreteBoundsError, @@ -33,7 +33,7 @@ pub struct RangeBoundsSet { impl RangeBoundsSet where - I: Ord + Copy + Stepable, + I: Ord + Copy + Discrete, K: DiscreteRange + Copy, { /// See [`RangeBoundsMap::new()`] for more details. @@ -217,7 +217,7 @@ where impl Serialize for RangeBoundsSet where - I: Ord + Copy + Stepable, + I: Ord + Copy + Discrete, K: DiscreteRange + Copy + Serialize, { fn serialize(&self, serializer: S) -> Result @@ -234,7 +234,7 @@ where impl<'de, I, K> Deserialize<'de> for RangeBoundsSet where - I: Ord + Copy + Stepable, + I: Ord + Copy + Discrete, K: DiscreteRange + Copy + Deserialize<'de>, { fn deserialize(deserializer: D) -> Result @@ -255,7 +255,7 @@ struct RangeBoundsSetVisitor { impl<'de, I, K> Visitor<'de> for RangeBoundsSetVisitor where - I: Ord + Copy + Stepable, + I: Ord + Copy + Discrete, K: DiscreteRange + Copy + Deserialize<'de>, { type Value = RangeBoundsSet; diff --git a/src/stepable.rs b/src/stepable.rs index 2157464..a447e6c 100644 --- a/src/stepable.rs +++ b/src/stepable.rs @@ -19,7 +19,10 @@ along with range_bounds_map. If not, see . use std::iter::Step; -pub trait Stepable { +pub trait Discrete { + const MIN: Self; + const MAX: Self; + fn up(self) -> Option where Self: Sized; @@ -28,7 +31,7 @@ pub trait Stepable { Self: Sized; } -impl Stepable for T +impl Discrete for T where T: Sized + Step, { diff --git a/src/test_ranges.rs b/src/test_ranges.rs index 14e3f49..3b98b7a 100644 --- a/src/test_ranges.rs +++ b/src/test_ranges.rs @@ -1,7 +1,7 @@ use std::ops::{Bound, RangeBounds}; use crate::discrete_bounds::{DiscreteBound, DiscreteBounds}; -use crate::stepable::Stepable; +use crate::stepable::Discrete; use crate::{TryFromDiscreteBounds, TryFromDiscreteBoundsError}; pub fn uu() -> DiscreteBounds { diff --git a/src/try_from_discrete_bounds.rs b/src/try_from_discrete_bounds.rs deleted file mode 100644 index 123c652..0000000 --- a/src/try_from_discrete_bounds.rs +++ /dev/null @@ -1,12 +0,0 @@ -use crate::discrete_bounds::DiscreteBounds; - -#[derive(PartialEq, Debug)] -pub struct TryFromDiscreteBoundsError; - -pub trait TryFromDiscreteBounds { - fn try_from_discrete_bounds( - discrete_bounds: DiscreteBounds, - ) -> Result - where - Self: Sized; -} diff --git a/src/utils.rs b/src/utils.rs index b6372f8..c025524 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -19,22 +19,21 @@ along with range_bounds_map. If not, see . use std::cmp::Ordering; -use crate::discrete_bound_ord::DiscreteBoundOrd; use crate::discrete_bounds::DiscreteBounds; use crate::range_bounds_map::DiscreteRange; -use crate::stepable::Stepable; +use crate::stepable::Discrete; -pub(crate) fn cmp_discrete_bound_ord_with_range( - discrete_bound_ord: DiscreteBoundOrd, - range: A, +pub(crate) fn cmp_point_with_range( + point: I, + range: K, ) -> Ordering where - A: DiscreteRange, - B: Ord, + I: Ord, + K: DiscreteRange, { - if discrete_bound_ord < range.start() { + if point < range.start() { Ordering::Less - } else if discrete_bound_ord > range.end() { + } else if point > range.end() { Ordering::Greater } else { Ordering::Equal @@ -121,7 +120,7 @@ where A: DiscreteRange, I: Ord, { - cmp_discrete_bound_ord_with_range(discrete_bound_ord, range).is_eq() + cmp_point_with_range(discrete_bound_ord, range).is_eq() } #[derive(Debug)] @@ -134,7 +133,7 @@ pub(crate) fn cut_range(base: B, cut: C) -> CutResult where B: DiscreteRange + Copy, C: DiscreteRange + Copy, - I: Ord + Copy + Stepable, + I: Ord + Copy + Discrete, { let mut result = CutResult { before_cut: None,