### Example using #[async_try_stream] with ? Operator Source: https://github.com/taiki-e/futures-async-stream/blob/main/CHANGELOG.md Demonstrates how to use the `#[async_try_stream]` attribute to create a stream that can use the `?` operator for error propagation, yielding `Result` items. ```rust #![feature(generators)] use futures::stream::Stream; use futures_async_stream::async_try_stream; #[async_try_stream(ok = i32, error = Box)] async fn foo(stream: impl Stream) { #[for_await] for x in stream { yield x.parse()?; } } ``` -------------------------------- ### Example using Async Stream Functions in Traits Source: https://github.com/taiki-e/futures-async-stream/blob/main/CHANGELOG.md Illustrates how to define and implement traits that include methods marked with the `#[async_stream]` attribute, enabling async stream functionality within trait definitions. ```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 { self.0 += 1; yield self.0; } } } ``` -------------------------------- ### Manual Implementation of #[for_await] Source: https://github.com/taiki-e/futures-async-stream/blob/main/README.md Shows how to replicate the functionality of the `#[for_await]` macro using a `while let` loop, `.await`, `pin!`, and `StreamExt::next()`. ```rust use std::pin::pin; use futures::stream::{Stream, StreamExt}; async fn collect(stream: impl Stream) -> Vec { let mut vec = vec![]; let mut stream = pin!(stream); while let Some(value) = stream.next().await { vec.push(value); } vec } ``` -------------------------------- ### Manual Implementation of #[stream] Source: https://github.com/taiki-e/futures-async-stream/blob/main/README.md Provides a manual implementation of a stream combinator similar to what `#[stream]` might generate, demonstrating the underlying mechanics. ```rust use std::{ pin::Pin, task::{ready, Context, Poll}, }; use futures::stream::Stream; use pin_project::pin_project; fn foo(stream: S) -> impl Stream where S: Stream, { Foo { stream } } #[pin_project] struct Foo { #[pin] stream: S, } impl Stream for Foo where S: Stream, { type Item = i32; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { if let Some(x) = ready!(self.project().stream.poll_next(cx)) { Poll::Ready(Some(x.parse().unwrap())) } else { Poll::Ready(None) } } } ``` -------------------------------- ### Cargo.toml Dependencies Source: https://github.com/taiki-e/futures-async-stream/blob/main/README.md Specifies the necessary dependencies for using the futures-async-stream crate, including the crate itself and the futures 0.3 library. ```toml [dependencies] futures-async-stream = "0.2" futures = "0.3" ``` -------------------------------- ### Creating Streams with #[stream] on Async Block Source: https://github.com/taiki-e/futures-async-stream/blob/main/README.md Illustrates using the `#[stream]` macro on an async block to generate a stream of integers. This approach is useful for creating streams inline. ```rust #![feature(coroutines, proc_macro_hygiene, stmt_expr_attributes)] use futures::stream::Stream; use futures_async_stream::stream; fn foo() -> impl Stream { #[stream] async move { for i in 0..10 { yield i; } } } ``` -------------------------------- ### Creating Streams with #[stream] on Async Fn Source: https://github.com/taiki-e/futures-async-stream/blob/main/README.md Shows how to create an asynchronous stream using the `#[stream]` macro applied to an async function. It processes items from an input stream and yields transformed values. ```rust #![feature(coroutines)] use futures::stream::Stream; use futures_async_stream::stream; // Returns a stream of i32 #[stream(item = i32)] async fn foo(stream: impl Stream) { // `for_await` is built into `stream`. If you use `for_await` only in `stream`, there is no need to import `for_await`. #[for_await] for x in stream { yield x.parse().unwrap(); } } ``` -------------------------------- ### Using #[try_stream] with ? Operator Source: https://github.com/taiki-e/futures-async-stream/blob/main/README.md Demonstrates how to use the `#[try_stream]` macro to create a stream where items are `Result` types. The `?` operator can be used within the stream to propagate errors. ```rust #![feature(coroutines)] use futures::stream::Stream; use futures_async_stream::try_stream; #[try_stream(ok = i32, error = Box)] async fn foo(stream: impl Stream) { #[for_await] for x in stream { yield x.parse()?; } } ``` -------------------------------- ### Using #[for_await] for Stream Processing Source: https://github.com/taiki-e/futures-async-stream/blob/main/README.md Demonstrates how to use the `#[for_await]` macro to iterate over an asynchronous stream within an async function, collecting items into a Vec. ```rust #![feature(proc_macro_hygiene, stmt_expr_attributes)] use futures::stream::Stream; use futures_async_stream::for_await; async fn collect(stream: impl Stream) -> Vec { let mut vec = vec![]; #[for_await] for value in stream { vec.push(value); } vec } ``` -------------------------------- ### Trait Implementation with Boxed Stream Return Type Source: https://github.com/taiki-e/futures-async-stream/blob/main/README.md Shows the concrete implementation of a trait method that returns a boxed stream, generated using the `#[stream(boxed)]` macro. This clarifies the return type for trait objects. ```rust #![feature(coroutines)] use std::pin::Pin; use futures::stream::Stream; use futures_async_stream::stream; // The trait itself can be defined without unstable features. trait Foo { fn method(&mut self) -> Pin + Send + '_>>; } struct Bar(u32); impl Foo for Bar { #[stream(boxed, item = u32)] async fn method(&mut self) { while self.0 < u32::MAX { self.0 += 1; yield self.0; } } } ``` -------------------------------- ### Using #[stream] with Boxed Streams in Traits Source: https://github.com/taiki-e/futures-async-stream/blob/main/README.md Demonstrates how to implement an async stream function within a trait using `#[stream(boxed)]`. This allows for dynamic dispatch and trait object usage for streams. ```rust #![feature(coroutines)] use futures_async_stream::stream; trait Foo { #[stream(boxed, item = u32)] async fn method(&mut self); } struct Bar(u32); impl Foo for Bar { #[stream(boxed, item = u32)] async fn method(&mut self) { while self.0 < u32::MAX { self.0 += 1; yield self.0; } } } ``` -------------------------------- ### Update Rust Feature Flag for Coroutines Source: https://github.com/taiki-e/futures-async-stream/blob/main/CHANGELOG.md Shows the required change in the Rust feature flag from `generators` to `coroutines` when upgrading to nightly-2023-10-21 or later, reflecting an update in the coroutine API. ```diff - #![feature(generators)] + #![feature(coroutines)] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.