From 270e21f8ea56c948bad7da5d69182c647fa7495c Mon Sep 17 00:00:00 2001 From: ripytide Date: Mon, 12 Dec 2022 01:59:24 +0000 Subject: [PATCH] added IntoIterator to set and map --- src/range_bounds_map.rs | 33 +++++++++++++++++++++++++++++++++ src/range_bounds_set.rs | 36 ++++++++++++++++++++++++++++++++++++ todo.md | 4 ++++ 3 files changed, 73 insertions(+) diff --git a/src/range_bounds_map.rs b/src/range_bounds_map.rs index 03b3bd0..c70e5bf 100644 --- a/src/range_bounds_map.rs +++ b/src/range_bounds_map.rs @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License along with range_bounds_map. If not, see . */ +use std::collections::btree_map::IntoValues; use std::collections::BTreeMap; use std::fmt::Debug; use std::iter::once; @@ -1452,6 +1453,38 @@ where } } +impl IntoIterator for RangeBoundsMap +where + K: RangeBounds, + I: Ord + Clone, +{ + type Item = (K, V); + type IntoIter = IntoIter; + #[trivial] + fn into_iter(self) -> Self::IntoIter { + return IntoIter { + inner: self.starts.into_values(), + }; + } +} +/// An owning iterator over the entries of a `RangeBoundsMap`. +/// +/// This `struct` is created by the [`into_iter`] method on +/// [`RangeBoundsMap`] (provided by the [`IntoIterator`] trait). See +/// its documentation for more. +/// +/// [`into_iter`]: IntoIterator::into_iter +/// [`IntoIterator`]: core::iter::IntoIterator +pub struct IntoIter { + inner: IntoValues, (K, V)>, +} +impl Iterator for IntoIter { + type Item = (K, V); + fn next(&mut self) -> Option { + self.inner.next() + } +} + impl Default for RangeBoundsMap where I: PartialOrd, diff --git a/src/range_bounds_set.rs b/src/range_bounds_set.rs index 01fccd7..475711a 100644 --- a/src/range_bounds_set.rs +++ b/src/range_bounds_set.rs @@ -23,6 +23,7 @@ use std::ops::{Bound, RangeBounds}; use labels::{tested, trivial}; use serde::{Deserialize, Serialize}; +use crate::range_bounds_map::IntoIter as MapIntoIter; use crate::{ OverlapError, OverlapOrTryFromBoundsError, RangeBoundsMap, TryFromBounds, TryFromBoundsError, @@ -802,6 +803,37 @@ where return output; } } +impl IntoIterator for RangeBoundsSet +where + K: RangeBounds, + I: Ord + Clone, +{ + type Item = K; + type IntoIter = IntoIter; + #[trivial] + fn into_iter(self) -> Self::IntoIter { + return IntoIter { + inner: self.map.into_iter(), + }; + } +} +/// An owning iterator over the entries of a `RangeBoundsSet`. +/// +/// This `struct` is created by the [`into_iter`] method on +/// [`RangeBoundsSet`] (provided by the [`IntoIterator`] trait). See +/// its documentation for more. +/// +/// [`into_iter`]: IntoIterator::into_iter +/// [`IntoIterator`]: core::iter::IntoIterator +pub struct IntoIter { + inner: MapIntoIter, +} +impl Iterator for IntoIter { + type Item = K; + fn next(&mut self) -> Option { + self.inner.next().map(first) + } +} impl Default for RangeBoundsSet where @@ -814,3 +846,7 @@ where } } } + +fn first((a, _): (A, B)) -> A { + a +} diff --git a/todo.md b/todo.md index d17d8b4..b598b42 100644 --- a/todo.md +++ b/todo.md @@ -8,10 +8,13 @@ logic based RangeBounds functions - make an expand function to go RangeBounds -> (Bound, Bound) rather than doing it manually everywhere +- rename overwrite to insert_forceful +- replace instances of |(key, _)| with fn first() # Documentation - replace `RangeBounds` with `K` where applicatble in docs +- replace rust types URL links with direct rust links # features @@ -37,6 +40,7 @@ - remove most rustfmt::skips and cargo fmt - check toml meta-data, github meta-data and readme opener - copy map to set again +- review todo.txt - copy readme to lib.rs docs again - take a look around idiomatic rust for a bit first - run is_labelled again and check they are accurate