Futures 0.3: How to make Stream: 'static out of Stream: 'a

When dealing with Future: 'a one might create a Future: 'static by moving the borrowed value into async move { ... } like so:

struct A;

fn foo<'a>(_a: &'a A) -> impl Future<Output=()> + 'a {
    async {}

fn bar(a: A) -> impl Future<Output=()> + 'static {
    async move { foo(&a).await }

How to do it with Stream?

fn sfoo<'a>(_a: &'a A) -> impl Stream<Item=()> + 'a {

fn sbar(a: A) -> impl Stream<Item=()> + 'static {
    // What here?
    // Tried variations of:
    // let mut s = sfoo(&a);
    // poll_fn(move |cx| {
    //     s.poll_next_unpin(cx)
    // })


use async_stream::stream;

fn sbar(a: A) -> impl Stream<Item=()> + 'static {
    stream! {
        let s = sfoo(&a);
        while let Some(el) = s.next().await {
            yield el;

works, but async-stream uses "a lightweight sender" for yielding which would be nice to avoid. At least it feels like it should be avoidable, anyone has better ideas?

You can embed a future into a stream with e.g. then.

Could you please elaborate how does it solve this issue? A working playground with sbar implementation would be preferable.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.