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

stream: impl Extend for StreamMap #4272

Merged
merged 1 commit into from
Nov 23, 2021
Merged

stream: impl Extend for StreamMap #4272

merged 1 commit into from
Nov 23, 2021

Conversation

davidpdrsn
Copy link
Member

Motivation

This allows StreamMap to be used with futures::stream::StreamExt::collect.

My use case is something like this:

let stream_map: StreamMap<_, _> = things
    .into_iter()
    .map(|thing| make_stream(thing)) // iterator of futures
    .collect::<FuturesUnordered<_>>() // stream of streams
    .collect::<StreamMap<_, _>>() // combine all the inner streams into one
    .await;

async fn make_stream(thing: Thing) -> impl Stream { ... }

Solution

Add Extend impl that delegates to the inner Vec.

This allows `StreamMap` to be used with
[`futures::stream::StreamExt::collect`][collect].

My use case is something like this:

```rust
let stream_map: Vec<impl Stream> = things
    .into_iter()
    .map(|thing| make_stream(thing)) // iterator of futures
    .collect::<FuturesUnordered<_>>() // stream of streams
    .collect::<StreamMap<_, _>>() // combine all the inner streams into one
    .await;

async fn make_stream(thing: Thing) -> impl Stream { ... }
```

[collect]: https://docs.rs/futures/0.3.17/futures/stream/trait.StreamExt.html#method.collect
@davidpdrsn davidpdrsn added the A-tokio-stream Area: The tokio-stream crate label Nov 23, 2021
@davidpdrsn davidpdrsn merged commit 3b33902 into master Nov 23, 2021
@davidpdrsn davidpdrsn deleted the dp/streammap-extend branch November 23, 2021 10:54
baum added a commit to baum/tokio that referenced this pull request Mar 29, 2023
…kable.

However, this implementation does not allow duplicate keys and would overwrite
existing ones similar to from_iter() behaviour.

Initially introduced: PR tokio-rs#4272 - tokio-rs#4272

Note: can not implement Extend trait, since this method has stricter
requirements than trait, as Extend does not require Key to be 'Eq + Hash'

Issue tokio-rs#4774 - tokio-rs#4774
baum added a commit to baum/tokio that referenced this pull request Mar 29, 2023
…kable.

However, this implementation does not allow duplicate keys and would overwrite
existing ones similar to from_iter() behaviour.

Initially introduced: PR tokio-rs#4272 - tokio-rs#4272

Note: can not implement Extend trait, since this method has stricter
requirements than trait, as Extend does not require Key to be 'Eq + Hash'

Issue tokio-rs#4774 - tokio-rs#4774
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-stream Area: The tokio-stream crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants