From 2efd51d83253c599f0028ef511aa5d9e69973926 Mon Sep 17 00:00:00 2001 From: ripytide Date: Thu, 20 Apr 2023 18:17:55 +0100 Subject: [PATCH] compiling with no errors whoop whoop! --- README.md | 2 +- src/{bound_ord.rs => discrete_bound_ord.rs} | 6 +- src/discrete_bounds.rs | 4 +- src/lib.rs | 8 +-- src/range_bounds_map.rs | 36 +++++----- src/range_bounds_set.rs | 75 +++++++++------------ src/test_ranges.rs | 2 - src/utils.rs | 18 ++--- 8 files changed, 67 insertions(+), 84 deletions(-) rename src/{bound_ord.rs => discrete_bound_ord.rs} (98%) diff --git a/README.md b/README.md index ebe854e..d4dde0e 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ See Wikipedia's article on mathematical Intervals: # Improvements/Caveats -- I had to create a new trait: [`TryFromBounds`] rather than using +- I had to create a new trait: [`TryFromDiscreteBounds`] rather than using `TryFrom<(Bound, Bound)>` (relys on upstream to impl, see [this thread](https://internals.rust-lang.org/t/range-should-impl-tryfrom-bound-bound)) diff --git a/src/bound_ord.rs b/src/discrete_bound_ord.rs similarity index 98% rename from src/bound_ord.rs rename to src/discrete_bound_ord.rs index 1b153c3..8655071 100644 --- a/src/bound_ord.rs +++ b/src/discrete_bound_ord.rs @@ -29,7 +29,7 @@ use crate::stepable::Stepable; /// /// [`Step`]: std::iter::Step #[derive(Debug, Copy, Clone, Serialize, Deserialize)] -pub(crate) enum DiscreteBoundOrd { +pub enum DiscreteBoundOrd { Included(T), StartUnbounded, EndUnbounded, @@ -51,7 +51,7 @@ impl DiscreteBoundOrd { pub fn up_if_finite(&self) -> DiscreteBoundOrd where - I: Stepable, + I: Stepable + Copy, { match self { DiscreteBoundOrd::Included(x) => DiscreteBoundOrd::Included(x.up().unwrap()), @@ -60,7 +60,7 @@ impl DiscreteBoundOrd { } pub fn down_if_finite(&self) -> DiscreteBoundOrd where - I: Stepable, + I: Stepable + Copy, { match self { DiscreteBoundOrd::Included(x) => DiscreteBoundOrd::Included(x.down().unwrap()), diff --git a/src/discrete_bounds.rs b/src/discrete_bounds.rs index f322712..3f9741f 100644 --- a/src/discrete_bounds.rs +++ b/src/discrete_bounds.rs @@ -19,7 +19,7 @@ along with range_bounds_map. If not, see . use std::ops::{Bound, RangeBounds}; -use crate::bound_ord::DiscreteBoundOrd; +use crate::discrete_bound_ord::DiscreteBoundOrd; use crate::stepable::Stepable; use crate::try_from_discrete_bounds::TryFromDiscreteBounds; @@ -37,7 +37,7 @@ pub enum DiscreteBound { impl DiscreteBound where - I: Stepable, + I: Stepable + Copy, { pub fn start(bound: Bound) -> Self { match bound { diff --git a/src/lib.rs b/src/lib.rs index acd1f92..639739c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,7 +147,7 @@ along with range_bounds_map. If not, see . //! //! # Improvements/Caveats //! -//! - I had to create a new trait: [`TryFromBounds`] rather than using +//! - I had to create a new trait: [`TryFromDiscreteBounds`] rather than using //! `TryFrom<(Bound, Bound)>` (relys on upstream to impl, see [this //! thread](https://internals.rust-lang.org/t/range-should-impl-tryfrom-bound-bound)) //! @@ -223,13 +223,13 @@ along with range_bounds_map. If not, see . #![allow(clippy::tabs_in_doc_comments)] #![allow(clippy::needless_return)] -//todo rename me -pub(crate) mod bound_ord; pub mod test_ranges; pub(crate) mod utils; -pub mod stepable; pub mod discrete_bounds; +pub(crate) mod discrete_bound_ord; + +pub mod stepable; pub mod try_from_discrete_bounds; pub mod range_bounds_map; diff --git a/src/range_bounds_map.rs b/src/range_bounds_map.rs index 9d793cc..a24d193 100644 --- a/src/range_bounds_map.rs +++ b/src/range_bounds_map.rs @@ -30,7 +30,7 @@ use serde::de::{MapAccess, Visitor}; use serde::ser::SerializeMap; use serde::{Deserialize, Deserializer, Serialize, Serializer}; -use crate::bound_ord::DiscreteBoundOrd; +use crate::discrete_bound_ord::DiscreteBoundOrd; use crate::discrete_bounds::{DiscreteBound, DiscreteBounds}; use crate::stepable::Stepable; use crate::try_from_discrete_bounds::TryFromDiscreteBounds; @@ -143,7 +143,7 @@ pub struct RangeBoundsMap { #[derive(PartialEq, Debug)] pub struct OverlapError; -/// An error type to represent a failed [`TryFromBounds`] within a +/// An error type to represent a failed [`TryFromDiscreteBounds`] within a /// method. /// /// There are several methods that return this error, and some of the @@ -161,7 +161,7 @@ pub struct OverlapError; /// Bound::Exclusive(8))`. However, since the `RangeBounds` type of /// this `RangeBoundsMap` is `Range<{integer}>` the latter of the two /// new `RangeBounds` is "unrepresentable", and hence will fail to be -/// created via [`TryFromBounds`] and [`RangeBoundsMap::cut()`] will +/// created via [`TryFromDiscreteBounds`] and [`RangeBoundsMap::cut()`] will /// return Err(TryFromDiscreteBoundsError). /// /// ``` @@ -201,7 +201,7 @@ pub struct OverlapError; /// use std::ops::{Bound, RangeBounds}; /// /// use range_bounds_map::{ -/// OverlapOrTryFromDiscreteBoundsError, RangeBoundsMap, TryFromBounds, +/// OverlapOrTryFromDiscreteBoundsError, RangeBoundsMap, TryFromDiscreteBounds, /// TryFromDiscreteBoundsError, /// }; /// @@ -234,7 +234,7 @@ pub struct OverlapError; /// } /// } /// -/// impl TryFromBounds for MultiBounds { +/// impl TryFromDiscreteBounds for MultiBounds { /// fn try_from_bounds( /// start_bound: Bound, /// end_bound: Bound, @@ -262,7 +262,7 @@ pub struct OverlapError; /// MultiBounds::Exclusive(4, 6), /// false /// ), -/// Err(OverlapOrTryFromDiscreteBoundsError::TryFromBounds( +/// Err(OverlapOrTryFromDiscreteBoundsError::TryFromDiscreteBounds( /// TryFromDiscreteBoundsError /// )) /// ); @@ -275,7 +275,7 @@ pub struct TryFromDiscreteBoundsError; #[derive(PartialEq, Debug)] pub enum OverlapOrTryFromDiscreteBoundsError { Overlap(OverlapError), - TryFromBounds(TryFromDiscreteBoundsError), + TryFromDiscreteBounds(TryFromDiscreteBoundsError), } impl RangeBoundsMap @@ -685,7 +685,7 @@ where /// 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 [`TryFromBounds`] trait then a + /// not be able be created with the [`TryFromDiscreteBounds`] trait then a /// [`TryFromDiscreteBoundsError`] will be returned and the map will not /// be cut at all. /// @@ -1097,7 +1097,7 @@ where /// /// If the range merges with one or two touching ranges and the /// merged-together range cannot be created with the - /// [`TryFromBounds`] trait then a [`TryFromDiscreteBoundsError`] will be + /// [`TryFromDiscreteBounds`] trait then a [`TryFromDiscreteBoundsError`] will be /// returned. /// /// # Panics @@ -1180,7 +1180,7 @@ where selfy.inner.remove(touching_end_comp(range.end())); }, ) - .map_err(OverlapOrTryFromDiscreteBoundsError::TryFromBounds) + .map_err(OverlapOrTryFromDiscreteBoundsError::TryFromDiscreteBounds) } /// Adds a new entry to the map and merges into other ranges in @@ -1196,7 +1196,7 @@ where /// /// If the range merges with one or two touching ranges and the /// merged-together range cannot be created with the - /// [`TryFromBounds`] trait then a [`TryFromDiscreteBoundsError`] will be + /// [`TryFromDiscreteBounds`] trait then a [`TryFromDiscreteBoundsError`] will be /// returned. /// /// # Panics @@ -1289,7 +1289,7 @@ where } }, ) - .map_err(OverlapOrTryFromDiscreteBoundsError::TryFromBounds) + .map_err(OverlapOrTryFromDiscreteBoundsError::TryFromDiscreteBounds) } /// Adds a new entry to the map and merges into other ranges in @@ -1302,7 +1302,7 @@ where /// returned. /// /// If the range merges other ranges and the merged-together range - /// cannot be created with the [`TryFromBounds`] trait then a + /// cannot be created with the [`TryFromDiscreteBounds`] trait then a /// [`TryFromDiscreteBoundsError`] will be returned. /// /// # Panics @@ -1389,7 +1389,7 @@ where /// returned. /// /// If the range merges other ranges and the merged-together range - /// cannot be created with the [`TryFromBounds`] trait then a + /// cannot be created with the [`TryFromDiscreteBounds`] trait then a /// [`TryFromDiscreteBoundsError`] will be returned. /// /// # Panics @@ -1486,7 +1486,7 @@ where /// same `V: Clone` trait bound applies. /// /// If the remaining ranges left after the cut are not able to be - /// created with the [`TryFromBounds`] trait then a + /// created with the [`TryFromDiscreteBounds`] trait then a /// [`TryFromDiscreteBoundsError`] will be returned. /// /// # Panics @@ -2247,7 +2247,7 @@ mod tests { assert_insert_merge_touching( special(), (mee(6, 7), true), - Err(OverlapOrTryFromDiscreteBoundsError::TryFromBounds( + Err(OverlapOrTryFromDiscreteBoundsError::TryFromDiscreteBounds( TryFromDiscreteBoundsError, )), None::<[_; 0]>, @@ -2261,7 +2261,7 @@ mod tests { assert_insert_merge_touching( special(), (mee(12, 15), true), - Err(OverlapOrTryFromDiscreteBoundsError::TryFromBounds( + Err(OverlapOrTryFromDiscreteBoundsError::TryFromDiscreteBounds( TryFromDiscreteBoundsError, )), None::<[_; 0]>, @@ -2381,7 +2381,7 @@ mod tests { assert_insert_merge_touching_if_values_equal( special(), (mee(12, 15), false), - Err(OverlapOrTryFromDiscreteBoundsError::TryFromBounds( + Err(OverlapOrTryFromDiscreteBoundsError::TryFromDiscreteBounds( TryFromDiscreteBoundsError, )), None::<[_; 0]>, diff --git a/src/range_bounds_set.rs b/src/range_bounds_set.rs index 461fe27..ef3dced 100644 --- a/src/range_bounds_set.rs +++ b/src/range_bounds_set.rs @@ -1,16 +1,16 @@ use std::fmt; use std::marker::PhantomData; -use std::ops::Bound; use serde::de::{SeqAccess, Visitor}; use serde::ser::SerializeSeq; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::discrete_bounds::DiscreteBounds; -use crate::range_bounds_map::{IntoIter as RangeBoundsMapIntoIter, DiscreteRange}; +use crate::range_bounds_map::{DiscreteRange, IntoIter as RangeBoundsMapIntoIter}; +use crate::stepable::Stepable; +use crate::try_from_discrete_bounds::TryFromDiscreteBounds; use crate::{ - OverlapError, OverlapOrTryFromDiscreteBoundsError, RangeBoundsMap, - TryFromDiscreteBoundsError, + OverlapError, OverlapOrTryFromDiscreteBoundsError, RangeBoundsMap, TryFromDiscreteBoundsError, }; /// An ordered set of non-overlapping ranges based on [`RangeBoundsMap`]. @@ -33,8 +33,8 @@ pub struct RangeBoundsSet { impl RangeBoundsSet where - I: Ord + Copy, - K: DiscreteRange, + I: Ord + Copy + Stepable, + K: DiscreteRange + Copy, { /// See [`RangeBoundsMap::new()`] for more details. pub fn new() -> Self { @@ -53,22 +53,19 @@ where /// See [`RangeBoundsMap::overlaps()`] for more details. pub fn overlaps(&self, range: Q) -> bool where - Q: DiscreteRange, + Q: DiscreteRange + Copy, { self.inner.overlaps(range) } /// See [`RangeBoundsMap::overlapping()`] for more details. - pub fn overlapping( - &self, - range: Q, - ) -> impl DoubleEndedIterator + pub fn overlapping(&self, range: Q) -> impl DoubleEndedIterator where - Q: DiscreteRange, + Q: DiscreteRange + Copy, { self.inner.overlapping(range).map(first) } /// See [`RangeBoundsMap::get_entry_at_point()`] for more details. - pub fn get_at_point(&self, point: I) -> Result, Bound)> { + pub fn get_at_point(&self, point: I) -> Result> { self.inner.get_entry_at_point(point).map(first).copied() } /// See [`RangeBoundsMap::contains_point()`] for more details. @@ -80,12 +77,9 @@ where self.inner.iter().map(first) } /// See [`RangeBoundsMap::remove_overlapping()`] for more details. - pub fn remove_overlapping<'a, Q>( - &'a mut self, - range: Q, - ) -> impl Iterator + '_ + pub fn remove_overlapping<'a, Q>(&'a mut self, range: Q) -> impl Iterator + '_ where - Q: DiscreteRange + 'a, + Q: DiscreteRange + Copy + 'a, { self.inner.remove_overlapping(range).map(first) } @@ -93,13 +87,10 @@ where pub fn cut<'a, Q>( &'a mut self, range: Q, - ) -> Result< - impl Iterator, Bound)> + '_, - TryFromDiscreteBoundsError, - > + ) -> Result> + '_, TryFromDiscreteBoundsError> where - Q: DiscreteRange + 'a, - K: TryFrom>, + Q: DiscreteRange + Copy + 'a, + K: TryFromDiscreteBounds, { self.inner.cut(range).map(|x| x.map(first)) } @@ -107,16 +98,16 @@ where pub fn gaps<'a, Q>( &'a self, range: Q, - ) -> impl DoubleEndedIterator, Bound)> + '_ + ) -> impl DoubleEndedIterator> + '_ where - Q: DiscreteRange + 'a, + Q: DiscreteRange + Copy + 'a, { self.inner.gaps(range) } /// See [`RangeBoundsMap::contains_range()`] for more details. pub fn contains_range(&self, range: Q) -> bool where - Q: DiscreteRange, + Q: DiscreteRange + Copy, { self.inner.contains_range(range) } @@ -130,17 +121,14 @@ where range: K, ) -> Result where - K: TryFrom>, + K: TryFromDiscreteBounds, { self.inner.insert_merge_touching(range, ()) } /// See [`RangeBoundsMap::insert_merge_overlapping()`] for more details. - pub fn insert_merge_overlapping( - &mut self, - range: K, - ) -> Result + pub fn insert_merge_overlapping(&mut self, range: K) -> Result where - K: TryFrom>, + K: TryFromDiscreteBounds, { self.inner.insert_merge_overlapping(range, ()) } @@ -150,17 +138,14 @@ where range: K, ) -> Result where - K: TryFrom>, + K: TryFromDiscreteBounds, { self.inner.insert_merge_touching_or_overlapping(range, ()) } /// See [`RangeBoundsMap::insert_overwrite()`] for more details. - pub fn insert_overwrite( - &mut self, - range: K, - ) -> Result<(), TryFromDiscreteBoundsError> + pub fn insert_overwrite(&mut self, range: K) -> Result<(), TryFromDiscreteBoundsError> where - K: TryFrom>, + K: TryFromDiscreteBounds, { self.inner.insert_overwrite(range, ()) } @@ -232,8 +217,8 @@ where impl Serialize for RangeBoundsSet where - I: Ord + Copy, - K: DiscreteRange + Serialize, + I: Ord + Copy + Stepable, + K: DiscreteRange + Copy + Serialize, { fn serialize(&self, serializer: S) -> Result where @@ -249,8 +234,8 @@ where impl<'de, I, K> Deserialize<'de> for RangeBoundsSet where - I: Ord + Copy, - K: DiscreteRange + Deserialize<'de>, + I: Ord + Copy + Stepable, + K: DiscreteRange + Copy + Deserialize<'de>, { fn deserialize(deserializer: D) -> Result where @@ -270,8 +255,8 @@ struct RangeBoundsSetVisitor { impl<'de, I, K> Visitor<'de> for RangeBoundsSetVisitor where - I: Ord + Copy, - K: DiscreteRange + Deserialize<'de>, + I: Ord + Copy + Stepable, + K: DiscreteRange + Copy + Deserialize<'de>, { type Value = RangeBoundsSet; diff --git a/src/test_ranges.rs b/src/test_ranges.rs index 5765dc8..3bca44a 100644 --- a/src/test_ranges.rs +++ b/src/test_ranges.rs @@ -1,5 +1,3 @@ -use std::ops::Bound; - use crate::discrete_bounds::{DiscreteBound, DiscreteBounds}; use crate::stepable::Stepable; diff --git a/src/utils.rs b/src/utils.rs index 8099964..6041073 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -19,7 +19,7 @@ along with range_bounds_map. If not, see . use std::cmp::Ordering; -use crate::bound_ord::DiscreteBoundOrd; +use crate::discrete_bound_ord::DiscreteBoundOrd; use crate::discrete_bounds::DiscreteBounds; use crate::range_bounds_map::DiscreteRange; use crate::stepable::Stepable; @@ -53,8 +53,8 @@ pub(crate) enum Config { } pub(crate) fn config(a: A, b: B) -> Config where - A: DiscreteRange, - B: DiscreteRange, + A: DiscreteRange + Copy, + B: DiscreteRange + Copy, I: Ord, { match a.start() < b.start() { @@ -99,8 +99,8 @@ enum SortedConfig { } fn sorted_config(a: A, b: B) -> SortedConfig where - A: DiscreteRange, - B: DiscreteRange, + A: DiscreteRange + Copy, + B: DiscreteRange + Copy, I: Ord, { let ae = (a.start(), a.end()); @@ -132,8 +132,8 @@ pub(crate) struct CutResult { } pub(crate) fn cut_range(base: B, cut: C) -> CutResult where - B: DiscreteRange, - C: DiscreteRange, + B: DiscreteRange + Copy, + C: DiscreteRange + Copy, I: Ord + Copy + Stepable, { let mut result = CutResult { @@ -222,8 +222,8 @@ where pub(crate) fn overlaps(a: A, b: B) -> bool where - A: DiscreteRange, - B: DiscreteRange, + A: DiscreteRange + Copy, + B: DiscreteRange + Copy, I: Ord, { !matches!(sorted_config(a, b), SortedConfig::NonOverlapping(_, _))