Skip to content

Add IntoStream for all Iterator<_> - #3106

Closed
matthiasbeyer wants to merge 1 commit into
tokio-rs:masterfrom
matthiasbeyer:iter-into-stream
Closed

matthiasbeyer wants to merge 1 commit into
tokio-rs:masterfrom
matthiasbeyer:iter-into-stream

Conversation

@matthiasbeyer

Copy link
Copy Markdown
Contributor

Motivation

I just started recently using tokio (or writing async-await rust), and I found it very inconvenient (and slightly harder to read) to write

let i = collection.iter()
    .map(something);

tokio::stream::iter(i)
    .map(something_else)
    //...

But instead I wanted to write

collection.iter()
    .map(something)
    .into_stream()
    .map(something_else)
    // ...

Which is the problem I try to solve with this PR.

Solution

This PR thus implements an extension trait for all std::iter::Iterators which adds the into_stream() conveniance function.

Note

Please note that this is my first PR to this project and although I've read the contribution guidelines, I want to apologize in advance for any mistakes made.

Thank you for this awesome library!

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
@taiki-e taiki-e self-assigned this Nov 7, 2020

@taiki-e taiki-e left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I think this PR's approach has some issues:

  • APIs that allow all iterators to be used as streams unconditionally without additional annotations may not be preferable, as iterators may block threads.
  • If you adopt the name IntoStream, for consistency with IntoIterator/IntoFuture, it preferable to be implemented in all streams, not all iterators. (And implement IntoStream for some iterators/collections)
  • If implement IntoStream for all streams, since Stream is an upstream crate (futures-core)'s trait, adding IntoStream in a downstream crate (tokio) has some limitations. (see async-rs/async-std#213 and rust-lang/futures-rs#1879) (In this case, I recommend doing in upstream)

@taiki-e taiki-e added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-stream Module: tokio/stream labels Nov 7, 2020
@Darksonn

Darksonn commented Nov 7, 2020

Copy link
Copy Markdown
Member

This should probably go in the futures crate, modulo @taiki-e's concerns.

@matthiasbeyer

Copy link
Copy Markdown
Contributor Author

Okay, thanks for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-stream Module: tokio/stream

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants