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 1 commit
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,61 @@ | ||
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: 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: 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 { | ||
if let Some(inner) = &mut self.inner { | ||
if let Some(item) = inner.next() { | ||
return Some(Ok(item)); | ||
} else { | ||
self.inner = None; | ||
} | ||
} else { | ||
match self.iter.next() { | ||
Some(Ok(ok)) => self.inner = Some(ok.into_iter()), | ||
Some(Err(e)) => return Some(Err(e)), | ||
None => 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, | ||
{ | ||
fn clone(&self) -> Self { | ||
Self { | ||
iter: self.iter.clone(), | ||
inner: self.inner.clone(), | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. Fixed and will push shortly. |
||
} |
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
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.
Do you really need this
else
? The only case where it isn't executed is ifself.inner
isSome(_)
andinner.next()
is alsoSome(_)
, but in that case you have an earlyreturn
. I would execute the contents of theelse
in any case, both if theif let
succeeds or not.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.
@SkiFire13 Your point is sound. In fact, we shouldn't even need to assign it to
None
in that case as it will just automatically fall through. That might hurt performance in the case wherenext()
is called on an empty iterator repeatedly, but I don't think that actually matters. So yes, let me make that change.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.
I think setting it to
None
might have a reason if you consider consistency in casenext
is called after the iterator ended. Should it try to advance the last inner iterator? Or the outer iterator? Currently the stdlib fuse both of them, but I guess it would be reasonable if you advance the outer iterator (in that case you need to set theself.inner
toNone
)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.
I realize now that I should not change setting the inner to
None
as it might incur a performance penalty when hitting severalErr
values in a row.