-
Notifications
You must be signed in to change notification settings - Fork 341
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 Stream::flatten and Stream::flat_map #367
Merged
+196
−2
Merged
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
ec98b41
feat: Add FlattenCompat struct
k-nasa bb14164
feat: Add Stream trait for FlattenCompat
k-nasa 2dee289
Add FlatMap struct
k-nasa 2187a2a
feat: Add Stream::flat_map
k-nasa cd86208
Add Flatten struct
k-nasa 8138afb
feat: Add Stream trait for Flatten
k-nasa 176359a
Add Stream::flatten
k-nasa 1c1e223
Merge branch 'master' into add_stream_flatten
k-nasa 410d16e
Add docs + To unstable feature
k-nasa 3297a0f
Merge branch 'master' into add_stream_flatten
k-nasa 271b6f4
fix: Using pin_project!
k-nasa 00e7e58
fix type def
k-nasa 001368d
$cargo fmt
k-nasa 0c5abee
to unstable stream::flat_map, stream::flatten
k-nasa b66ffa6
update recursion_limit
k-nasa 7ce721f
Update src/lib.rs
k-nasa b7b5df1
Update src/stream/stream/flatten.rs
k-nasa 6168952
Update src/stream/stream/flatten.rs
k-nasa bf3508f
Update src/stream/stream/flatten.rs
k-nasa 8932cec
Update src/stream/stream/flatten.rs
k-nasa 61b7a09
Fix type declaration
k-nasa 13a08b0
Narrow the disclosure range of FlatMap::new
k-nasa 37f14b0
Narrow the disclosure range of Flatten::new
k-nasa a42ae2f
Narrow the disclosure range of FlattenCompat::new
k-nasa 6889762
fix: Split FlattenCompat logic to Flatten and FlatMap
k-nasa 040227f
Merge branch 'master' into add_stream_flatten
k-nasa ae7adf2
fix: Remove unused import
k-nasa 1554b04
$cargo fmt
k-nasa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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,158 @@ | ||
use pin_project_lite::pin_project; | ||
use std::pin::Pin; | ||
|
||
use crate::prelude::*; | ||
use crate::stream::stream::map::Map; | ||
use crate::stream::{IntoStream, Stream}; | ||
use crate::task::{Context, Poll}; | ||
|
||
pin_project! { | ||
/// This `struct` is created by the [`flat_map`] method on [`Stream`]. See its | ||
/// documentation for more. | ||
/// | ||
/// [`flat_map`]: trait.Stream.html#method.flat_map | ||
/// [`Stream`]: trait.Stream.html | ||
#[allow(missing_debug_implementations)] | ||
pub struct FlatMap<S, U, T, F> { | ||
#[pin] | ||
inner: FlattenCompat<Map<S, F, T, U>, U>, | ||
} | ||
} | ||
|
||
impl<S, U, F> FlatMap<S, U, S::Item, F> | ||
where | ||
S: Stream, | ||
U: IntoStream, | ||
F: FnMut(S::Item) -> U, | ||
{ | ||
pub(super) fn new(stream: S, f: F) -> FlatMap<S, U, S::Item, F> { | ||
FlatMap { | ||
inner: FlattenCompat::new(stream.map(f)), | ||
} | ||
} | ||
} | ||
|
||
impl<S, U, F> Stream for FlatMap<S, U, S::Item, F> | ||
where | ||
S: Stream, | ||
S::Item: IntoStream<IntoStream = U, Item = U::Item>, | ||
U: Stream, | ||
F: FnMut(S::Item) -> U, | ||
{ | ||
type Item = U::Item; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
self.project().inner.poll_next(cx) | ||
} | ||
} | ||
|
||
pin_project! { | ||
/// This `struct` is created by the [`flatten`] method on [`Stream`]. See its | ||
/// documentation for more. | ||
/// | ||
/// [`flatten`]: trait.Stream.html#method.flatten | ||
/// [`Stream`]: trait.Stream.html | ||
#[allow(missing_debug_implementations)] | ||
pub struct Flatten<S, U> { | ||
#[pin] | ||
inner: FlattenCompat<S, U> | ||
} | ||
} | ||
|
||
impl<S> Flatten<S, S::Item> | ||
where | ||
S: Stream, | ||
S::Item: IntoStream, | ||
{ | ||
pub(super) fn new(stream: S) -> Flatten<S, S::Item> { | ||
Flatten { | ||
inner: FlattenCompat::new(stream), | ||
} | ||
} | ||
} | ||
|
||
impl<S, U> Stream for Flatten<S, <S::Item as IntoStream>::IntoStream> | ||
where | ||
S: Stream, | ||
S::Item: IntoStream<IntoStream = U, Item = U::Item>, | ||
U: Stream, | ||
{ | ||
type Item = U::Item; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
self.project().inner.poll_next(cx) | ||
} | ||
} | ||
|
||
pin_project! { | ||
/// Real logic of both `Flatten` and `FlatMap` which simply delegate to | ||
/// this type. | ||
#[derive(Clone, Debug)] | ||
struct FlattenCompat<S, U> { | ||
#[pin] | ||
stream: S, | ||
#[pin] | ||
frontiter: Option<U>, | ||
k-nasa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
impl<S, U> FlattenCompat<S, U> { | ||
/// Adapts an iterator by flattening it, for use in `flatten()` and `flat_map()`. | ||
fn new(stream: S) -> FlattenCompat<S, U> { | ||
FlattenCompat { | ||
stream, | ||
frontiter: None, | ||
} | ||
} | ||
} | ||
|
||
impl<S, U> Stream for FlattenCompat<S, U> | ||
where | ||
S: Stream, | ||
S::Item: IntoStream<IntoStream = U, Item = U::Item>, | ||
U: Stream, | ||
{ | ||
type Item = U::Item; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let mut this = self.project(); | ||
loop { | ||
if let Some(inner) = this.frontiter.as_mut().as_pin_mut() { | ||
if let item @ Some(_) = futures_core::ready!(inner.poll_next(cx)) { | ||
return Poll::Ready(item); | ||
} | ||
} | ||
|
||
match futures_core::ready!(this.stream.as_mut().poll_next(cx)) { | ||
None => return Poll::Ready(None), | ||
Some(inner) => this.frontiter.set(Some(inner.into_stream())), | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::FlattenCompat; | ||
|
||
use crate::prelude::*; | ||
use crate::task; | ||
|
||
use std::collections::VecDeque; | ||
|
||
#[test] | ||
fn test_poll_next() -> std::io::Result<()> { | ||
let inner1: VecDeque<u8> = vec![1, 2, 3].into_iter().collect(); | ||
let inner2: VecDeque<u8> = vec![4, 5, 6].into_iter().collect(); | ||
|
||
let s: VecDeque<_> = vec![inner1, inner2].into_iter().collect(); | ||
|
||
task::block_on(async move { | ||
let flat = FlattenCompat::new(s); | ||
let v: Vec<u8> = flat.collect().await; | ||
|
||
assert_eq!(v, vec![1, 2, 3, 4, 5, 6]); | ||
Ok(()) | ||
}) | ||
} | ||
} |
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.
Instead of exporting
flatten::{Flatten, FlatMap}
I think it'd be preferable to export them from separate files.The
FlattenCompat
is a nice idea, but I'm generally wary of introducing abstractions here, preferring to duplicate some of the logic instead. While this is more lines of code, it's also somewhat simpler. Would you be okay with splitting the two impls into two files, and removing the sharedFlattenCompat
struct?Thanks!
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.
Yh!