How to accept an async Stream as argument?

That feels good to me (so far).

Okay, so that's also a reason to go for Variant B in this particular case.

I still find it a bit annoyingly complex that I have to manually pin! the stream. Of course, within the current design of Rust, it makes sense; but since pinning (to the stack) doesn't come with any overhead (right?), it might be nicer to be able to write something like:

async fn foo(#[pin] some_stream: impl Stream<Item = SomeType>) {
    while /* … */ {
        let item_option = some_stream.next().await;
        /* … */
    }
}

Would it be possible to create such a macro?

By the way, dealing with pinning and unpinning reminds me of my following comment in another thread:

Dealing with Streams suddenly is a case where I have to understand pinning/unpinning as a "user" of Rust. I wonder why this feels so complex:

  • Is it because there are no keywords or designated syntax constructs for pin/unpin (i.e. because pinning and unpinning is implemented "on top" of Rust through traits, macros, and smart pointers hiding their value from the user, which ultimately leads to even more complex issues such as projections)?
  • Is it inherently complex (same as having to understand by-value vs & vs &mut)?
1 Like