From 3376a530cd5a9bb9d203d350208de315835cb42f Mon Sep 17 00:00:00 2001 From: ripytide Date: Fri, 31 Mar 2023 21:38:38 +0100 Subject: [PATCH] duplicated the from_slice* functions on the Set side too, And fixes all the signatures by replacing Unwraps with ? --- src/range_bounds_map.rs | 23 +++--- src/range_bounds_set.rs | 157 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 162 insertions(+), 18 deletions(-) diff --git a/src/range_bounds_map.rs b/src/range_bounds_map.rs index 206579f..c6a3b2e 100644 --- a/src/range_bounds_map.rs +++ b/src/range_bounds_map.rs @@ -1744,11 +1744,10 @@ where ) -> Result, OverlapError> { let mut map = RangeBoundsMap::new(); for (range_bounds, value) in slice { - map.insert_strict(range_bounds, value).unwrap(); + map.insert_strict(range_bounds, value)?; } return Ok(map); } - /// Allocate a `RangeBoundsMap` and move the given (`RangeBounds`, /// `Value`) pairs from the slice into the map using /// [`RangeBoundsMap::insert_merge_touching()`]. @@ -1777,17 +1776,16 @@ where #[trivial] pub fn from_slice_merge_touching( slice: [(K, V); N], - ) -> Result, OverlapError> + ) -> Result, OverlapOrTryFromBoundsError> where K: TryFromBounds, { let mut map = RangeBoundsMap::new(); for (range_bounds, value) in slice { - map.insert_merge_touching(range_bounds, value).unwrap(); + map.insert_merge_touching(range_bounds, value)?; } return Ok(map); } - /// Allocate a `RangeBoundsMap` and move the given (`RangeBounds`, /// `Value`) pairs from the slice into the map using /// [`RangeBoundsMap::insert_merge_overlapping()`]. @@ -1816,17 +1814,16 @@ where #[trivial] pub fn from_slice_merge_overlapping( slice: [(K, V); N], - ) -> Result, OverlapError> + ) -> Result, TryFromBoundsError> where K: TryFromBounds, { let mut map = RangeBoundsMap::new(); for (range_bounds, value) in slice { - map.insert_merge_overlapping(range_bounds, value).unwrap(); + map.insert_merge_overlapping(range_bounds, value)?; } return Ok(map); } - /// Allocate a `RangeBoundsMap` and move the given (`RangeBounds`, /// `Value`) pairs from the slice into the map using /// [`RangeBoundsMap::insert_merge_touching_or_overlapping()`]. @@ -1857,18 +1854,16 @@ where #[trivial] pub fn from_slice_merge_touching_or_overlapping( slice: [(K, V); N], - ) -> Result, OverlapError> + ) -> Result, TryFromBoundsError> where K: TryFromBounds, { let mut map = RangeBoundsMap::new(); for (range_bounds, value) in slice { - map.insert_merge_touching_or_overlapping(range_bounds, value) - .unwrap(); + map.insert_merge_touching_or_overlapping(range_bounds, value)?; } return Ok(map); } - /// Allocate a `RangeBoundsMap` and move the given (`RangeBounds`, /// `Value`) pairs from the slice into the map using /// [`RangeBoundsMap::insert_overwrite()`]. @@ -1897,14 +1892,14 @@ where #[trivial] pub fn from_slice_overwrite( slice: [(K, V); N], - ) -> Result, OverlapError> + ) -> Result, TryFromBoundsError> where V: Clone, K: TryFromBounds, { let mut map = RangeBoundsMap::new(); for (range_bounds, value) in slice { - map.insert_overwrite(range_bounds, value).unwrap(); + map.insert_overwrite(range_bounds, value)?; } return Ok(map); } diff --git a/src/range_bounds_set.rs b/src/range_bounds_set.rs index 688770d..2a30022 100644 --- a/src/range_bounds_set.rs +++ b/src/range_bounds_set.rs @@ -995,9 +995,8 @@ where /// from the slice into the set using /// [`RangeBoundsSet::insert_strict()`]. /// - /// If any of the given `RangeBounds` overlap any of the other - /// `RangeBounds` in the slice, then an [`OverlapError`] is - /// returned. + /// May return an `Err` while inserting. See + /// [`RangeBoundsSet::insert_strict()`] for details. /// /// # Panics /// @@ -1019,7 +1018,157 @@ where ) -> Result, OverlapError> { let mut set = RangeBoundsSet::new(); for range_bounds in slice { - set.insert_strict(range_bounds).unwrap(); + set.insert_strict(range_bounds)?; + } + return Ok(set); + } + /// Allocate a `RangeBoundsSet` and move the given `RangeBounds` + /// from the slice into the set using + /// [`RangeBoundsSet::insert_merge_touching()`]. + /// + /// May return an `Err` while inserting. See + /// [`RangeBoundsSet::insert_merge_touching()`] for details. + /// + /// # Panics + /// + /// Panics if any of the given `RangeBounds` is an invalid + /// `RangeBounds`. See [`Invalid + /// RangeBounds`](https://docs.rs/range_bounds_map/latest/range_bounds_map/index.html#Invalid-RangeBounds) + /// for more details. + /// + /// # Examples + /// ``` + /// use range_bounds_map::{RangeBoundsSet, TryFromBoundsError}; + /// + /// let set = RangeBoundsSet::from_slice_merge_touching([ + /// 1..4, + /// 4..8, + /// 8..100, + /// ]) + /// .unwrap(); + /// ``` + #[trivial] + pub fn from_slice_merge_touching( + slice: [K; N], + ) -> Result, OverlapOrTryFromBoundsError> + where + K: TryFromBounds, + { + let mut set = RangeBoundsSet::new(); + for range_bounds in slice { + set.insert_merge_touching(range_bounds)?; + } + return Ok(set); + } + /// Allocate a `RangeBoundsSet` and move the given `RangeBounds` + /// from the slice into the set using + /// [`RangeBoundsSet::insert_merge_overlapping()`]. + /// + /// May return an `Err` while inserting. See + /// [`RangeBoundsSet::insert_merge_overlapping()`] for details. + /// + /// # Panics + /// + /// Panics if any of the given `RangeBounds` is an invalid + /// `RangeBounds`. See [`Invalid + /// RangeBounds`](https://docs.rs/range_bounds_map/latest/range_bounds_map/index.html#Invalid-RangeBounds) + /// for more details. + /// + /// # Examples + /// ``` + /// use range_bounds_map::{RangeBoundsSet, TryFromBoundsError}; + /// + /// let set = RangeBoundsSet::from_slice_merge_overlapping([ + /// 1..4, + /// 4..8, + /// 8..100, + /// ]) + /// .unwrap(); + /// ``` + #[trivial] + pub fn from_slice_merge_overlapping( + slice: [K; N], + ) -> Result, TryFromBoundsError> + where + K: TryFromBounds, + { + let mut set = RangeBoundsSet::new(); + for range_bounds in slice { + set.insert_merge_overlapping(range_bounds)?; + } + return Ok(set); + } + /// Allocate a `RangeBoundsSet` and move the given `RangeBounds` + /// from the slice into the set using + /// [`RangeBoundsSet::insert_merge_touching_or_overlapping()`]. + /// + /// May return an `Err` while inserting. See + /// [`RangeBoundsSet::insert_merge_touching_or_overlapping()`] for details. + /// + /// # Panics + /// + /// Panics if any of the given `RangeBounds` is an invalid + /// `RangeBounds`. See [`Invalid + /// RangeBounds`](https://docs.rs/range_bounds_map/latest/range_bounds_map/index.html#Invalid-RangeBounds) + /// for more details. + /// + /// # Examples + /// ``` + /// use range_bounds_map::{RangeBoundsSet, TryFromBoundsError}; + /// + /// let set = + /// RangeBoundsSet::from_slice_merge_touching_or_overlapping([ + /// 1..4, + /// 4..8, + /// 8..100, + /// ]) + /// .unwrap(); + /// ``` + #[trivial] + pub fn from_slice_merge_touching_or_overlapping( + slice: [K; N], + ) -> Result, TryFromBoundsError> + where + K: TryFromBounds, + { + let mut set = RangeBoundsSet::new(); + for range_bounds in slice { + set.insert_merge_touching_or_overlapping(range_bounds)?; + } + return Ok(set); + } + /// Allocate a `RangeBoundsSet` and move the given `RangeBounds` + /// from the slice into the set using + /// [`RangeBoundsSet::insert_overwrite()`]. + /// + /// May return an `Err` while inserting. See + /// [`RangeBoundsSet::insert_overwrite()`] for details. + /// + /// # Panics + /// + /// Panics if any of the given `RangeBounds` is an invalid + /// `RangeBounds`. See [`Invalid + /// RangeBounds`](https://docs.rs/range_bounds_map/latest/range_bounds_map/index.html#Invalid-RangeBounds) + /// for more details. + /// + /// # Examples + /// ``` + /// use range_bounds_map::{RangeBoundsSet, TryFromBoundsError}; + /// + /// let set = + /// RangeBoundsSet::from_slice_overwrite([1..4, 4..8, 8..100]) + /// .unwrap(); + /// ``` + #[trivial] + pub fn from_slice_overwrite( + slice: [K; N], + ) -> Result, TryFromBoundsError> + where + K: TryFromBounds, + { + let mut set = RangeBoundsSet::new(); + for range_bounds in slice { + set.insert_overwrite(range_bounds)?; } return Ok(set); }