New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add .flatten_ok() #527
Merged
Merged
add .flatten_ok() #527
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
80046c6
add .flatten_ok()
vadixidav 8421d27
address some flatten_ok() PR comments
vadixidav 2fb1784
add FusedIterator impl for FlattenOk
vadixidav 4c09d13
add size_hint impl for FlattenOk
vadixidav 38805c6
add inner_front,inner_back to FlattenOk and update methods
vadixidav 238578b
add impl of DoubleEndedIterator to FlattenOk
vadixidav fedb59e
added basic integration tests for .flatten_ok() without size_hint tests
vadixidav File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
use crate::size_hint; | ||
use std::{ | ||
fmt, | ||
iter::{DoubleEndedIterator, FusedIterator}, | ||
}; | ||
|
||
pub fn flatten_ok<I, T, E>(iter: I) -> FlattenOk<I, T, E> | ||
where | ||
I: Iterator<Item = Result<T, E>>, | ||
T: IntoIterator, | ||
{ | ||
FlattenOk { | ||
iter, | ||
inner_front: None, | ||
inner_back: None, | ||
} | ||
} | ||
|
||
/// An iterator adaptor that flattens `Result::Ok` values and | ||
/// allows `Result::Err` values through unchanged. | ||
/// | ||
/// See [`.flatten_ok()`](crate::Itertools::flatten_ok) for more information. | ||
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"] | ||
pub struct FlattenOk<I, T, E> | ||
where | ||
I: Iterator<Item = Result<T, E>>, | ||
T: IntoIterator, | ||
{ | ||
iter: I, | ||
inner_front: Option<T::IntoIter>, | ||
inner_back: Option<T::IntoIter>, | ||
} | ||
|
||
impl<I, T, E> Iterator for FlattenOk<I, T, E> | ||
where | ||
I: Iterator<Item = Result<T, E>>, | ||
T: IntoIterator, | ||
{ | ||
type Item = Result<T::Item, E>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
loop { | ||
// Handle the front inner iterator. | ||
if let Some(inner) = &mut self.inner_front { | ||
if let Some(item) = inner.next() { | ||
return Some(Ok(item)); | ||
} else { | ||
// This is necessary for the iterator to implement `FusedIterator` | ||
// with only the orginal iterator being fused. | ||
self.inner_front = None; | ||
} | ||
} | ||
|
||
match self.iter.next() { | ||
Some(Ok(ok)) => self.inner_front = Some(ok.into_iter()), | ||
Some(Err(e)) => return Some(Err(e)), | ||
None => { | ||
// Handle the back inner iterator. | ||
if let Some(inner) = &mut self.inner_back { | ||
if let Some(item) = inner.next() { | ||
return Some(Ok(item)); | ||
} else { | ||
// This is necessary for the iterator to implement `FusedIterator` | ||
// with only the orginal iterator being fused. | ||
self.inner_back = None; | ||
} | ||
} else { | ||
return None; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let inner_hint = |inner: &Option<T::IntoIter>| { | ||
inner | ||
.as_ref() | ||
.map(Iterator::size_hint) | ||
.unwrap_or((0, Some(0))) | ||
}; | ||
let inner_front = inner_hint(&self.inner_front); | ||
let inner_back = inner_hint(&self.inner_back); | ||
// The outer iterator `Ok` case could be (0, None) as we don't know its size_hint yet. | ||
let outer = match self.iter.size_hint() { | ||
(0, Some(0)) => (0, Some(0)), | ||
_ => (0, None), | ||
}; | ||
|
||
size_hint::add(size_hint::add(inner_front, inner_back), outer) | ||
} | ||
} | ||
|
||
impl<I, T, E> DoubleEndedIterator for FlattenOk<I, T, E> | ||
where | ||
I: DoubleEndedIterator<Item = Result<T, E>>, | ||
T: IntoIterator, | ||
T::IntoIter: DoubleEndedIterator, | ||
{ | ||
fn next_back(&mut self) -> Option<Self::Item> { | ||
loop { | ||
// Handle the back inner iterator. | ||
if let Some(inner) = &mut self.inner_back { | ||
if let Some(item) = inner.next_back() { | ||
return Some(Ok(item)); | ||
} else { | ||
// This is necessary for the iterator to implement `FusedIterator` | ||
// with only the orginal iterator being fused. | ||
self.inner_back = None; | ||
} | ||
} | ||
|
||
match self.iter.next_back() { | ||
Some(Ok(ok)) => self.inner_back = Some(ok.into_iter()), | ||
Some(Err(e)) => return Some(Err(e)), | ||
None => { | ||
// Handle the front inner iterator. | ||
if let Some(inner) = &mut self.inner_front { | ||
if let Some(item) = inner.next_back() { | ||
return Some(Ok(item)); | ||
} else { | ||
// This is necessary for the iterator to implement `FusedIterator` | ||
// with only the orginal iterator being fused. | ||
self.inner_front = None; | ||
} | ||
} else { | ||
return None; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<I, T, E> Clone for FlattenOk<I, T, E> | ||
where | ||
I: Iterator<Item = Result<T, E>> + Clone, | ||
T: IntoIterator, | ||
T::IntoIter: Clone, | ||
{ | ||
#[inline] | ||
clone_fields!(iter, inner_front, inner_back); | ||
} | ||
|
||
impl<I, T, E> fmt::Debug for FlattenOk<I, T, E> | ||
where | ||
I: Iterator<Item = Result<T, E>> + fmt::Debug, | ||
T: IntoIterator, | ||
T::IntoIter: fmt::Debug, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("FlattenOk") | ||
.field("iter", &self.iter) | ||
.field("inner_front", &self.inner_front) | ||
.field("inner_back", &self.inner_back) | ||
.finish() | ||
} | ||
} | ||
|
||
/// Only the iterator being flattened needs to implement [`FusedIterator`]. | ||
impl<I, T, E> FusedIterator for FlattenOk<I, T, E> | ||
where | ||
I: FusedIterator<Item = Result<T, E>>, | ||
T: IntoIterator, | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use itertools::{assert_equal, Itertools}; | ||
use std::{ops::Range, vec::IntoIter}; | ||
|
||
fn mix_data() -> IntoIter<Result<Range<i32>, bool>> { | ||
vec![Ok(0..2), Err(false), Ok(2..4), Err(true), Ok(4..6)].into_iter() | ||
} | ||
|
||
fn ok_data() -> IntoIter<Result<Range<i32>, bool>> { | ||
vec![Ok(0..2), Ok(2..4), Ok(4..6)].into_iter() | ||
} | ||
|
||
#[test] | ||
fn flatten_ok_mixed_expected_forward() { | ||
assert_equal( | ||
mix_data().flatten_ok(), | ||
vec![ | ||
Ok(0), | ||
Ok(1), | ||
Err(false), | ||
Ok(2), | ||
Ok(3), | ||
Err(true), | ||
Ok(4), | ||
Ok(5), | ||
], | ||
); | ||
} | ||
|
||
#[test] | ||
fn flatten_ok_mixed_expected_reverse() { | ||
assert_equal( | ||
mix_data().flatten_ok().rev(), | ||
vec![ | ||
Ok(5), | ||
Ok(4), | ||
Err(true), | ||
Ok(3), | ||
Ok(2), | ||
Err(false), | ||
Ok(1), | ||
Ok(0), | ||
], | ||
); | ||
} | ||
|
||
#[test] | ||
fn flatten_ok_collect_mixed_forward() { | ||
assert_eq!( | ||
mix_data().flatten_ok().collect::<Result<Vec<_>, _>>(), | ||
Err(false) | ||
); | ||
} | ||
|
||
#[test] | ||
fn flatten_ok_collect_mixed_reverse() { | ||
assert_eq!( | ||
mix_data().flatten_ok().rev().collect::<Result<Vec<_>, _>>(), | ||
Err(true) | ||
); | ||
} | ||
|
||
#[test] | ||
fn flatten_ok_collect_ok_forward() { | ||
assert_eq!( | ||
ok_data().flatten_ok().collect::<Result<Vec<_>, _>>(), | ||
Ok((0..6).collect()) | ||
); | ||
} | ||
|
||
#[test] | ||
fn flatten_ok_collect_ok_reverse() { | ||
assert_eq!( | ||
ok_data().flatten_ok().rev().collect::<Result<Vec<_>, _>>(), | ||
Ok((0..6).rev().collect()) | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you could use
debug_fmt_fields
for consistency here.