From cdaeac1b955e4ddc96bcfce13b4b08ed3d73eeb2 Mon Sep 17 00:00:00 2001 From: ripytide Date: Fri, 21 Apr 2023 13:25:54 +0100 Subject: [PATCH] all tests and doc tests and clippy passing --- src/discrete_finite_bounds.rs | 11 ----- src/lib.rs | 45 +++++++++--------- src/range_bounds_map.rs | 86 +++++++++++++---------------------- 3 files changed, 53 insertions(+), 89 deletions(-) diff --git a/src/discrete_finite_bounds.rs b/src/discrete_finite_bounds.rs index a9db405..f7a1760 100644 --- a/src/discrete_finite_bounds.rs +++ b/src/discrete_finite_bounds.rs @@ -17,8 +17,6 @@ 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, RangeBounds}; - use crate::range_bounds_map::FiniteRange; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -28,15 +26,6 @@ pub struct DiscreteFiniteBounds { pub end: I, } -impl RangeBounds for DiscreteFiniteBounds { - fn start_bound(&self) -> Bound<&I> { - Bound::Included(&self.start) - } - fn end_bound(&self) -> Bound<&I> { - Bound::Included(&self.end) - } -} - impl FiniteRange for DiscreteFiniteBounds where I: Copy, diff --git a/src/lib.rs b/src/lib.rs index 46d39da..7681210 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,33 +43,32 @@ along with range_bounds_map. If not, see . //! //! use range_bounds_map::test_ranges::ie; //! use range_bounds_map::RangeBoundsMap; +//! use range_bounds_map::FiniteRange; //! //! #[derive(Debug, Copy, Clone)] //! enum Reservation { -//! // Start, End (Inclusive-Inclusive) +//! // Start, End (Inclusive-Exclusive) //! Finite(i8, i8), -//! // Start (Exclusive) +//! // Start (Inclusive-Forever) //! Infinite(i8), //! } //! -//! // First, we need to implement RangeBounds -//! impl RangeBounds for Reservation { -//! fn start_bound(&self) -> Bound<&i8> { -//! match self { -//! Reservation::Finite(start, _) => { -//! Bound::Included(start) -//! } -//! Reservation::Infinite(start) => { -//! Bound::Excluded(start) -//! } -//! } -//! } -//! fn end_bound(&self) -> Bound<&i8> { -//! match self { -//! Reservation::Finite(_, end) => Bound::Included(end), -//! Reservation::Infinite(_) => Bound::Unbounded, -//! } -//! } +//! // First, we need to implement FiniteRange +//! impl FiniteRange for Reservation { +//! fn start(&self) -> i8 { +//! match self { +//! Reservation::Finite(start, _) => *start, +//! Reservation::Infinite(start) => *start, +//! } +//! } +//! fn end(&self) -> i8 { +//! match self { +//! //the end is exclusive so we take off 1 with checking +//! //for compile time error overflow detection +//! Reservation::Finite(_, end) => end.checked_sub(1).unwrap(), +//! Reservation::Infinite(_) => i8::MAX, +//! } +//! } //! } //! //! // Next we can create a custom typed RangeBoundsMap @@ -220,13 +219,13 @@ along with range_bounds_map. If not, see . pub mod test_ranges; pub(crate) mod utils; -pub mod discrete_finite_bounds; pub mod discrete_finite; +pub mod discrete_finite_bounds; pub mod range_bounds_map; pub mod range_bounds_set; -pub use crate::discrete_finite_bounds::DiscreteFiniteBounds; pub use crate::discrete_finite::DiscreteFinite; -pub use crate::range_bounds_map::{OverlapError, RangeBoundsMap}; +pub use crate::discrete_finite_bounds::DiscreteFiniteBounds; +pub use crate::range_bounds_map::{FiniteRange, OverlapError, RangeBoundsMap}; pub use crate::range_bounds_set::RangeBoundsSet; diff --git a/src/range_bounds_map.rs b/src/range_bounds_map.rs index 282a76e..1891b37 100644 --- a/src/range_bounds_map.rs +++ b/src/range_bounds_map.rs @@ -76,6 +76,7 @@ use crate::utils::{cmp_point_with_range, cut_range, is_valid_range, overlaps}; /// use std::ops::{Bound, RangeBounds}; /// /// use range_bounds_map::RangeBoundsMap; +/// use range_bounds_map::FiniteRange; /// /// // An Exclusive-Exclusive range is not provided by any /// // std::ops ranges so let't make our own!. @@ -94,13 +95,15 @@ use crate::utils::{cmp_point_with_range, cut_range, is_valid_range, overlaps}; /// } /// } /// -/// // Implement RangeBounds on our new type -/// impl RangeBounds for ExEx { -/// fn start_bound(&self) -> Bound<&u8> { -/// Bound::Excluded(&self.start) +/// // Implement FiniteRange on our new type +/// impl FiniteRange for ExEx { +/// fn start(&self) -> u8 { +/// //we are exclusive so need to step the values up +/// self.start.checked_add(1).unwrap() /// } -/// fn end_bound(&self) -> Bound<&u8> { -/// Bound::Excluded(&self.end) +/// fn end(&self) -> u8 { +/// //we are exclusive so need to step the values down +/// self.end.checked_sub(1).unwrap() /// } /// } /// @@ -541,11 +544,6 @@ where /// Cuts a given range out of the map and returns an iterator of /// the full or partial ranges that were cut. /// - /// If the remaining ranges left in the map after the cut would - /// not be able be created with the [`TryFromDiscreteFiniteBounds`] trait then a - /// [`TryFromDiscreteFiniteBoundsError`] will be returned and the map will not - /// be cut at all. - /// /// `V` must implement `Clone` as if you try to cut out the center /// of a range in the map it will split into two different entries /// using `Clone`. Or if you partially cut a range then @@ -561,24 +559,24 @@ where /// ``` /// use std::ops::Bound; /// - /// use range_bounds_map::test_ranges::{ie, ie_strict, ii}; - /// use range_bounds_map::{RangeBoundsMap, TryFromDiscreteFiniteBoundsError}; + /// use range_bounds_map::test_ranges::{ie, ii}; + /// use range_bounds_map::{RangeBoundsMap}; /// /// let mut base = RangeBoundsMap::from_slice_strict([ - /// (ie_strict(1, 4), false), - /// (ie_strict(4, 8), true), - /// (ie_strict(8, 100), false), + /// (ie(1, 4), false), + /// (ie(4, 8), true), + /// (ie(8, 100), false), /// ]) /// .unwrap(); /// /// let after_cut = RangeBoundsMap::from_slice_strict([ - /// (ie_strict(1, 2), false), - /// (ie_strict(40, 100), false), + /// (ie(1, 2), false), + /// (ie(40, 100), false), /// ]) /// .unwrap(); /// /// assert_eq!( - /// base.cut(ie(2, 40)).unwrap().collect::>(), + /// base.cut(ie(2, 40)).collect::>(), /// [ /// (ie(2, 4), false), /// (ie(4, 8), true), @@ -943,11 +941,6 @@ where /// map, then an [`OverlapError`] is returned and the map is not /// updated. /// - /// If the range merges with one or two touching ranges and the - /// merged-together range cannot be created with the - /// [`TryFromDiscreteFiniteBounds`] trait then a [`TryFromDiscreteFiniteBoundsError`] will be - /// returned. - /// /// # Panics /// /// Panics if the given range is an invalid range. See [`Invalid @@ -958,7 +951,7 @@ where /// ``` /// use range_bounds_map::test_ranges::ie; /// use range_bounds_map::{ - /// OverlapError, OverlapOrTryFromDiscreteFiniteBoundsError, RangeBoundsMap, + /// OverlapError, RangeBoundsMap, /// }; /// /// let mut map = RangeBoundsMap::from_slice_strict([ @@ -976,7 +969,7 @@ where /// // Overlapping /// assert_eq!( /// map.insert_merge_touching(ie(4, 8), false), - /// Err(OverlapOrTryFromDiscreteFiniteBoundsError::Overlap(OverlapError)), + /// Err(OverlapError), /// ); /// /// // Neither Touching or Overlapping @@ -1037,11 +1030,6 @@ where /// map, then an [`OverlapError`] is returned and the map is not /// updated. /// - /// If the range merges with one or two touching ranges and the - /// merged-together range cannot be created with the - /// [`TryFromDiscreteFiniteBounds`] trait then a [`TryFromDiscreteFiniteBoundsError`] will be - /// returned. - /// /// # Panics /// /// Panics if the given range is an invalid range. See [`Invalid @@ -1052,7 +1040,7 @@ where /// ``` /// use range_bounds_map::test_ranges::ie; /// use range_bounds_map::{ - /// OverlapError, OverlapOrTryFromDiscreteFiniteBoundsError, RangeBoundsMap, + /// OverlapError, RangeBoundsMap, /// }; /// /// let mut map = RangeBoundsMap::from_slice_strict([ @@ -1070,7 +1058,7 @@ where /// // Overlapping /// assert_eq!( /// map.insert_merge_touching_if_values_equal(ie(4, 8), false), - /// Err(OverlapOrTryFromDiscreteFiniteBoundsError::Overlap(OverlapError)), + /// Err(OverlapError), /// ); /// /// // Neither Touching or Overlapping @@ -1143,10 +1131,6 @@ where /// If successful then the newly inserted (possibly merged) range is /// returned. /// - /// If the range merges other ranges and the merged-together range - /// cannot be created with the [`TryFromDiscreteFiniteBounds`] trait then a - /// [`TryFromDiscreteFiniteBoundsError`] will be returned. - /// /// # Panics /// /// Panics if the given range is an invalid range. See [`Invalid @@ -1157,7 +1141,7 @@ where /// ``` /// use range_bounds_map::test_ranges::ie; /// use range_bounds_map::{ - /// OverlapError, OverlapOrTryFromDiscreteFiniteBoundsError, RangeBoundsMap, + /// OverlapError, RangeBoundsMap, /// }; /// /// let mut map = RangeBoundsMap::from_slice_strict([ @@ -1169,19 +1153,19 @@ where /// // Touching /// assert_eq!( /// map.insert_merge_overlapping(ie(4, 6), true), - /// Ok(ie(4, 6)) + /// ie(4, 6) /// ); /// /// // Overlapping /// assert_eq!( /// map.insert_merge_overlapping(ie(4, 8), false), - /// Ok(ie(4, 8)) + /// ie(4, 8) /// ); /// /// // Neither Touching or Overlapping /// assert_eq!( /// map.insert_merge_overlapping(ie(10, 16), false), - /// Ok(ie(10, 16)) + /// ie(10, 16) /// ); /// /// assert_eq!( @@ -1226,10 +1210,6 @@ where /// If successful then the newly inserted (possibly merged) range is /// returned. /// - /// If the range merges other ranges and the merged-together range - /// cannot be created with the [`TryFromDiscreteFiniteBounds`] trait then a - /// [`TryFromDiscreteFiniteBoundsError`] will be returned. - /// /// # Panics /// /// Panics if the given range is an invalid range. See [`Invalid @@ -1240,7 +1220,7 @@ where /// ``` /// use range_bounds_map::test_ranges::ie; /// use range_bounds_map::{ - /// OverlapError, OverlapOrTryFromDiscreteFiniteBoundsError, RangeBoundsMap, + /// OverlapError, RangeBoundsMap, /// }; /// /// let mut map = RangeBoundsMap::from_slice_strict([ @@ -1252,19 +1232,19 @@ where /// // Touching /// assert_eq!( /// map.insert_merge_touching_or_overlapping(ie(4, 6), true), - /// Ok(ie(1, 8)) + /// ie(1, 8) /// ); /// /// // Overlapping /// assert_eq!( /// map.insert_merge_touching_or_overlapping(ie(4, 8), false), - /// Ok(ie(1, 8)) + /// ie(1, 8) /// ); /// /// // Neither Touching or Overlapping /// assert_eq!( /// map.insert_merge_touching_or_overlapping(ie(10, 16), false), - /// Ok(ie(10, 16)) + /// ie(10, 16) /// ); /// /// assert_eq!( @@ -1319,10 +1299,6 @@ where /// followed by [`RangeBoundsMap::insert_strict()`]. Hence the /// same `V: Clone` trait bound applies. /// - /// If the remaining ranges left after the cut are not able to be - /// created with the [`TryFromDiscreteFiniteBounds`] trait then a - /// [`TryFromDiscreteFiniteBoundsError`] will be returned. - /// /// # Panics /// /// Panics if the given range is an invalid range. See [`Invalid @@ -1338,7 +1314,7 @@ where /// RangeBoundsMap::from_slice_strict([(ie(2, 8), false)]) /// .unwrap(); /// - /// assert_eq!(map.insert_overwrite(ie(4, 6), true), Ok(())); + /// map.insert_overwrite(ie(4, 6), true); /// /// assert_eq!( /// map.into_iter().collect::>(), @@ -1414,7 +1390,7 @@ where /// # Examples /// ``` /// use range_bounds_map::test_ranges::ie; - /// use range_bounds_map::{RangeBoundsMap, TryFromDiscreteFiniteBoundsError}; + /// use range_bounds_map::{RangeBoundsMap}; /// /// let map = RangeBoundsMap::from_slice_strict([ /// (ie(1, 4), false),