diff --git a/src/range_bounds_map.rs b/src/range_bounds_map.rs index 5ea2e6b..0a46830 100644 --- a/src/range_bounds_map.rs +++ b/src/range_bounds_map.rs @@ -648,7 +648,7 @@ where /// Cuts a given `RangeBounds` out of the map and returns an /// iterator of the full or partial `RangeBounds` that were cut in - /// a tuple with their `Value`s. + /// as `((Bound, Bound), Value)`. /// /// If the remaining `RangeBounds` left in the map after the cut /// or the `RangeBounds` returned in the iterator are not able to @@ -676,12 +676,25 @@ where /// RangeBoundsMap::try_from([(1..2, false), (40..100, false)]) /// .unwrap(); /// - /// assert_eq!(base.cut(&(2..40)), Ok(())); + /// assert_eq!( + /// base.cut(&(2..40)).collect::>(), + /// [ + /// ((Bound::Included(2), Bound::Excluded(4)), false), + /// ((Bound::Included(4), Bound::Excluded(8)), true), + /// ((Bound::Included(8), Bound::Excluded(40)), false), + /// ] + /// ); /// assert_eq!(base, after_cut); /// assert_eq!(base.cut(&(60..=80)), Err(TryFromBoundsError)); /// ``` #[tested] - pub fn cut(&mut self, range_bounds: &Q) -> Result, Bound), V)>, TryFromBoundsError> + pub fn cut( + &mut self, + range_bounds: &Q, + ) -> Result< + impl DoubleEndedIterator, Bound), V)>, + TryFromBoundsError, + > where Q: RangeBounds, K: TryFromBounds, @@ -736,8 +749,9 @@ where .unwrap(); } - let mut removed_first = - removed.next().map(|(key, value)| (expand_cloned(&key), value)); + let mut removed_first = removed + .next() + .map(|(key, value)| (expand_cloned(&key), value)); let mut removed_last = removed .next_back() .map(|(key, value)| (expand_cloned(&key), value)); @@ -763,6 +777,55 @@ where } } + /// Identical to [`RangeBoundsMap::cut()`] except it returns an + /// iterator of `Result, Value), + /// TryFromBoundsError>` after applying TryFromBounds to the + /// `(Bound, Bound)`s in in the iterator returned by + /// [`RangeBoundsMap::cut()`]. + /// + /// # Examples + /// ``` + /// use range_bounds_map::{RangeBoundsMap, TryFromBoundsError}; + /// + /// let mut base = RangeBoundsMap::try_from([ + /// (1..4, false), + /// (4..8, true), + /// (8..100, false), + /// ]) + /// .unwrap(); + /// + /// let after_cut = + /// RangeBoundsMap::try_from([(1..2, false), (40..100, false)]) + /// .unwrap(); + /// + /// assert_eq!( + /// base.cut_same(&(2..40)).collect::>(), + /// [(Ok(2..4), false), (Ok(4..8), true), (Ok(8..40), false)] + /// ); + /// assert_eq!(base, after_cut); + /// assert_eq!(base.cut_same(&(60..=80)), Err(TryFromBoundsError)); + /// ``` + #[tested] + pub fn cut_same( + &mut self, + range_bounds: &Q, + ) -> Result< + impl DoubleEndedIterator, V)>, + TryFromBoundsError, + > + where + Q: RangeBounds, + K: TryFromBounds, + V: Clone, + { + Ok(self.cut(range_bounds)?.map(|((start, end), value)| { + ( + K::try_from_bounds(start, end).ok_or(TryFromBoundsError), + value, + ) + })) + } + /// Returns an iterator of `(Bound<&I>, Bound<&I>)` over all the /// maximally-sized gaps in the map that are also within the given /// `outer_range_bounds`. @@ -1541,7 +1604,7 @@ where fn expand_cloned(range_bounds: &K) -> (Bound, Bound) where K: RangeBounds, - I: Clone + I: Clone, { cloned_bounds((range_bounds.start_bound(), range_bounds.end_bound())) } diff --git a/todo.md b/todo.md index 775d937..f4ff394 100644 --- a/todo.md +++ b/todo.md @@ -9,6 +9,9 @@ - make an expand function to go RangeBounds -> (Bound, Bound) rather than doing it manually everywhere +# Documentation +- replace `RangeBounds` with `K` where applicatble in docs + # features - make specifc RangeMap, RangeSet, RangeInclusiveMap... types for signature