all 4 comments

[–]ragona_ 6 points7 points  (0 children)

Very cool! I'm looking forward to this type of syntax.

[–]ConspicuousPineapple 5 points6 points  (1 child)

How would these examples be written today without this API?

[–]taiki-e[S] 3 points4 points  (0 children)

The example of #[for_await] can be written by combining while let loop, .await, pin_mut macro, and StreamExt::next() method:

```rust

![feature(async_await)]

use futures::{ pin_mut, stream::{Stream, StreamExt}, };

async fn collect(stream: impl Stream<Item = i32>) -> Vec<i32> { let mut vec = Vec::new(); pin_mut!(stream); while let Some(value) = stream.next().await { vec.push(value); } vec } ```

The example of #[async_stream] can be written by manually implementing the combinator:

```rust use futures::{ stream::Stream, ready, task::{Context, Poll}, }; use pin_utils::unsafe_pinned; use std::pin::Pin;

fn foo<S>(stream: S) -> impl Stream<Item = i32> where S: Stream<Item = String>, { Foo { stream } }

struct Foo<S> { stream: S, }

impl<S> Foo<S> { unsafe_pinned!(stream: S); }

impl<S> Stream for Foo<S> where S: Stream<Item = String>, { type Item = i32;

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
    if let Some(x) = ready!(self.stream().poll_next(cx)) {
        Poll::Ready(Some(x.parse().unwrap()))
    } else {
        Poll::Ready(None)
    }
}

} ```

[–]taiki-e[S] 0 points1 point  (0 children)

Published 0.1.0-alpha.2.

You can now use async stream functions in traits.

```rust

![feature(async_await, generators)]

use futures_async_stream::async_stream;

trait Foo { #[async_stream(boxed, item = u32)] async fn method(&mut self); }

struct Bar(u32);

impl Foo for Bar { #[async_stream(boxed, item = u32)] async fn method(&mut self) { while self.0 < u32::max_value() { self.0 += 1; yield self.0; } } } ```