Skip to content

Add stream::from_iter #400

Closed
Closed
@yoshuawuyts

Description

@yoshuawuyts

It's quite common to want to create a stream from an iterator. Currently we're having a lot of VecDequeue in our examples because it implements Stream, but that might not stay around (see rust-lang/futures-rs#1879).

So I propose we add a new function stream::from_iter that returns a struct FromIter, similar to how we have stream::from_fn that returns FromFn.

This is mostly as a pragmatic fix for the fact that we can't implement IntoStream for std types ourselves (see #129), and probably in general useful when porting codebases. The value of this function should decrease if we can resolve that, but in order to do so the VecDequeue must be removed first, so in order to break the somewhat recursive dependency there I propose we add this function.

Because we'd want to use this function to replace all uses of VecDequeue we currently have in our examples, and we're in somewhat of time pressure (futures@0.3.0 is in 9 days), I'd like to propose we make an exception here and introduce this without the "unstable" marker.

edit: unsure about stability. More in follow-up comment.

cc/ @stjepang

Examples

let nums = stream::from_iter(vec![0, 1, 2, 3, 4]);

while let Some(num) = nums.next().await {
    dbg!(num);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions