Skip to content

Commit 479b4c4

Browse files
NoneTheWisrsshah1ModProgmightyiamprabhpreet
committed
Add Itertools::enumerate_ok
Squashed commit of the following: commit 146fb0e Author: NoneTheWisr <[email protected]> Date: Sun Jan 16 12:44:41 2022 +0400 mob next [ci-skip] [ci skip] [skip ci] commit aa2db90 Author: NoneTheWisr <[email protected]> Date: Sun Jan 16 12:19:55 2022 +0400 mob next [ci-skip] [ci skip] [skip ci] commit f473fee Author: sshah1 <[email protected]> Date: Sun Jan 16 09:09:32 2022 +0100 mob next [ci-skip] [ci skip] [skip ci] commit ff4628c Author: Preet Dua <[email protected]> Date: Sat Jan 15 23:58:18 2022 -0800 mob next [ci-skip] [ci skip] [skip ci] commit 90bdeb6 Author: NoneTheWisr <[email protected]> Date: Sun Jan 16 11:46:53 2022 +0400 mob next [ci-skip] [ci skip] [skip ci] commit 7044ed3 Author: Shahar Dawn Or <[email protected]> Date: Sat Jan 15 16:58:27 2022 +0700 mob next [ci-skip] [ci skip] [skip ci] commit 659639e Author: NoneTheWisr <[email protected]> Date: Sat Jan 15 13:54:21 2022 +0400 mob next [ci-skip] [ci skip] [skip ci] commit a305d97 Author: Preet Dua <[email protected]> Date: Sat Jan 15 01:43:33 2022 -0800 mob next [ci-skip] [ci skip] [skip ci] commit f08c2af Author: Roland Fredenhagen <[email protected]> Date: Sat Jan 15 10:33:59 2022 +0100 mob next [ci-skip] [ci skip] [skip ci] commit 48bd032 Author: Shahar Dawn Or <[email protected]> Date: Sat Jan 15 16:22:06 2022 +0700 mob next [ci-skip] [ci skip] [skip ci] commit db02884 Author: NoneTheWisr <[email protected]> Date: Sat Jan 15 13:10:44 2022 +0400 mob next [ci-skip] [ci skip] [skip ci] Co-authored-by: sshah1 <[email protected]> Co-authored-by: Roland Fredenhagen <[email protected]> Co-authored-by: Shahar Dawn Or <[email protected]> Co-authored-by: Preet Dua <[email protected]>
1 parent 6c4fc2f commit 479b4c4

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/adaptors/mod.rs

+44
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,50 @@ impl_tuple_combination!(Tuple10Combination Tuple9Combination; a b c d e f g h i)
839839
impl_tuple_combination!(Tuple11Combination Tuple10Combination; a b c d e f g h i j);
840840
impl_tuple_combination!(Tuple12Combination Tuple11Combination; a b c d e f g h i j k);
841841

842+
/// An iterator adapter to enumerate values within a nested `Result::Ok`.
843+
///
844+
/// See [`.enumerate_ok()`](crate::Itertools::enumerate_ok) for more information.
845+
#[derive(Clone)]
846+
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
847+
pub struct EnumerateOk<I> {
848+
iter: I,
849+
index: usize
850+
}
851+
852+
/// Create a new `EnumerateOk` iterator.
853+
pub fn enumerate_ok<I, T, E>(iter: I) -> EnumerateOk<I>
854+
where
855+
I: Iterator<Item = Result<T, E>>,
856+
{
857+
EnumerateOk {
858+
iter,
859+
index: 0
860+
}
861+
}
862+
863+
impl<I> fmt::Debug for EnumerateOk<I>
864+
where
865+
I: fmt::Debug,
866+
{
867+
debug_fmt_fields!(EnumerateOk, iter);
868+
}
869+
870+
impl<I,T,E> Iterator for EnumerateOk<I>
871+
where I: Iterator<Item = Result<T,E>>,
872+
{
873+
type Item = Result<(usize,T),E>;
874+
875+
fn next(&mut self) -> Option<Self::Item> {
876+
self.iter.next().map(|item| {
877+
item.map(|v| {
878+
let index = self.index;
879+
self.index +=1;
880+
(index,v)
881+
})
882+
})
883+
}
884+
}
885+
842886
/// An iterator adapter to filter values within a nested `Result::Ok`.
843887
///
844888
/// See [`.filter_ok()`](crate::Itertools::filter_ok) for more information.

src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub mod structs {
9999
Batching,
100100
MapInto,
101101
MapOk,
102+
EnumerateOk,
102103
Merge,
103104
MergeBy,
104105
TakeWhileRef,
@@ -846,6 +847,23 @@ pub trait Itertools : Iterator {
846847
self.map_ok(f)
847848
}
848849

850+
/// Return an iterator adaptor that adds an index to `Result::Ok`
851+
/// values. `Result::Err` values are unchanged. The index is incremented
852+
/// only for `Result::Ok` values.
853+
///
854+
/// ```
855+
/// use itertools::Itertools;
856+
///
857+
/// let input = vec![Ok(41), Err(false), Ok(11)];
858+
/// let it = input.into_iter().enumerate_ok();
859+
/// itertools::assert_equal(it, vec![Ok((0, 41)), Err(false), Ok((1, 11))]);
860+
/// ```
861+
fn enumerate_ok<T, E>(self) -> EnumerateOk<Self>
862+
where Self: Iterator<Item = Result<T, E>> + Sized,
863+
{
864+
adaptors::enumerate_ok(self)
865+
}
866+
849867
/// Return an iterator adaptor that applies the provided closure
850868
/// to every `Result::Ok` value. `Result::Err` values are
851869
/// unchanged.

0 commit comments

Comments
 (0)