Skip to content
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 StreamExt::flat_map #2068

Merged
merged 6 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions futures-util/src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ pub use futures_core::stream::{FusedStream, Stream, TryStream};
#[allow(clippy::module_inception)]
mod stream;
pub use self::stream::{
Chain, Collect, Concat, Enumerate, Filter, FilterMap, Flatten, Fold, ForEach, Fuse, Inspect,
Map, Next, Peek, Peekable, Scan, SelectNextSome, Skip, SkipWhile, StreamExt, StreamFuture, Take,
TakeWhile, Then, Zip,
Chain, Collect, Concat, Enumerate, Filter, FilterMap, FlatMap, Flatten, Fold, ForEach, Fuse,
Inspect, Map, Next, Peek, Peekable, Scan, SelectNextSome, Skip, SkipWhile, StreamExt,
StreamFuture, Take, TakeWhile, Then, Zip,
};

#[cfg(feature = "std")]
Expand Down
7 changes: 5 additions & 2 deletions futures-util/src/stream/stream/flat_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct FlatMap<St, U, F> {
inner_stream: Option<U>,
}

impl<St: Unpin, U, F> Unpin for FlatMap<St, U, F> {}
impl<St: Unpin, U: Unpin, F> Unpin for FlatMap<St, U, F> {}

impl<St, U, F> fmt::Debug for FlatMap<St, U, F>
where
Expand Down Expand Up @@ -130,7 +130,10 @@ where

if let Some(inner_stream_size_hint) = self.inner_stream.as_ref().map(|st| st.size_hint()) {
(
stream_size_hint.0 + inner_stream_size_hint.0,
stream_size_hint
.0
.checked_add(inner_stream_size_hint.0)
.unwrap_or(stream_size_hint.0),
if no_stream_items_left {
inner_stream_size_hint.1
} else {
Expand Down
6 changes: 3 additions & 3 deletions futures-util/src/stream/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,11 @@ pub trait StreamExt: Stream {
/// Maps a stream like [`StreamExt::map`] but flattens nested `Stream`s.
///
/// [`StreamExt::map`] is very useful, but if it produces a `Stream` instead,
olegnn marked this conversation as resolved.
Show resolved Hide resolved
/// there's an extra layer of indirection. `flat_map()` will remove this extra
/// layer on its own.
/// you should chain cominators like `.map(f).flatten()` while this combinator
olegnn marked this conversation as resolved.
Show resolved Hide resolved
/// provides ability to write `.flat_map(f)` instead of chaining.
///
/// The provided closure which produce inner streams is executed over all elements
/// of this stream as they are made available.
/// of stream as last inner stream is terminated and next stream item is available.
///
/// Note that this function consumes the stream passed into it and returns a
/// wrapped version of it, similar to the existing `flat_map` methods in the
Expand Down
4 changes: 2 additions & 2 deletions futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ pub mod stream {
try_unfold, TryUnfold,

StreamExt,
Chain, Collect, Concat, Enumerate, Filter, FilterMap, Flatten, Fold,
Forward, ForEach, Fuse, StreamFuture, Inspect, Map, Next,
Chain, Collect, Concat, Enumerate, Filter, FilterMap, FlatMap, Flatten,
Fold, Forward, ForEach, Fuse, StreamFuture, Inspect, Map, Next,
SelectNextSome, Peek, Peekable, Scan, Skip, SkipWhile, Take, TakeWhile,
Then, Zip,

Expand Down