Skip to content

Commit

Permalink
Update to new coroutine API
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Oct 21, 2023
1 parent 5d123ab commit bf37805
Show file tree
Hide file tree
Showing 27 changed files with 92 additions and 83 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

## [Unreleased]

- Update to [new coroutine API since nightly-2023-10-21](https://github.com/rust-lang/rust/pull/116958).

On the user side of this crate, the only changes required are the following:

```diff
- #![feature(generators)]
+ #![feature(coroutines)]
```

## [0.2.8] - 2023-10-09

- Update to syn 2.0. ([#92](https://github.com/taiki-e/futures-async-stream/pull/92))
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Async stream for Rust and the futures crate.

This crate provides useful features for streams, using `async_await` and
unstable [`generators`](https://github.com/rust-lang/rust/issues/43122).
unstable [`coroutines`](https://github.com/rust-lang/rust/issues/43122).

## Usage

Expand Down Expand Up @@ -54,14 +54,14 @@ loops can only be used inside of `async` functions, closures, blocks,

## `#[stream]`

Creates streams via generators.
Creates streams via coroutines.

This is a reimplement of [futures-await]'s `#[stream]` for futures 0.3 and
is an experimental implementation of [the idea listed as the next step of
async/await](https://github.com/rust-lang/rfcs/blob/HEAD/text/2394-async_await.md#generators-and-streams).

```rust
#![feature(generators)]
#![feature(coroutines)]

use futures::stream::Stream;
use futures_async_stream::stream;
Expand All @@ -86,7 +86,7 @@ via the `yield` expression.
`#[stream]` can also be used on async blocks:

```rust
#![feature(generators, proc_macro_hygiene, stmt_expr_attributes)]
#![feature(coroutines, proc_macro_hygiene, stmt_expr_attributes)]

use futures::stream::Stream;
use futures_async_stream::stream;
Expand All @@ -110,7 +110,7 @@ You can use async stream functions in traits by passing `boxed` or
`boxed_local` as an argument.

```rust
#![feature(generators)]
#![feature(coroutines)]

use futures_async_stream::stream;

Expand Down Expand Up @@ -138,7 +138,7 @@ If you passed `boxed_local` instead of `boxed`, async stream function
returns a non-thread-safe stream (`Pin<Box<dyn Stream<Item = item> + 'lifetime>>`).

```rust
#![feature(generators)]
#![feature(coroutines)]

use std::pin::Pin;

Expand Down Expand Up @@ -170,7 +170,7 @@ returned stream is `Result` with `Ok` being the value yielded and `Err` the
error type returned by `?` operator or `return Err(...)`.

```rust
#![feature(generators)]
#![feature(coroutines)]

use futures::stream::Stream;
use futures_async_stream::try_stream;
Expand Down
8 changes: 4 additions & 4 deletions futures-async-stream-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn for_await(args: TokenStream, input: TokenStream) -> TokenStream {
expr.into_token_stream().into()
}

/// Creates streams via generators.
/// Creates streams via coroutines.
///
/// See crate level documentation for details.
#[proc_macro_attribute]
Expand All @@ -58,7 +58,7 @@ pub fn stream(args: TokenStream, input: TokenStream) -> TokenStream {
.into()
}

/// Creates streams via generators. This is equivalent to `#[stream]` on async blocks.
/// Creates streams via coroutines. This is equivalent to `#[stream]` on async blocks.
#[proc_macro]
pub fn stream_block(input: TokenStream) -> TokenStream {
let input = TokenStream::from(TokenTree::Group(Group::new(Delimiter::Brace, input)));
Expand All @@ -68,7 +68,7 @@ pub fn stream_block(input: TokenStream) -> TokenStream {
stream::parse_async(&mut expr, parse::Context::Stream).into()
}

/// Creates streams via generators.
/// Creates streams via coroutines.
///
/// See crate level documentation for details.
#[proc_macro_attribute]
Expand All @@ -78,7 +78,7 @@ pub fn try_stream(args: TokenStream, input: TokenStream) -> TokenStream {
.into()
}

/// Creates streams via generators. This is equivalent to `#[try_stream]` on async blocks.
/// Creates streams via coroutines. This is equivalent to `#[try_stream]` on async blocks.
#[proc_macro]
pub fn try_stream_block(input: TokenStream) -> TokenStream {
let input = TokenStream::from(TokenTree::Group(Group::new(Delimiter::Brace, input)));
Expand Down
8 changes: 4 additions & 4 deletions futures-async-stream-macro/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ fn expand_async_body(inputs: Punctuated<FnArg, Token![,]>) -> (Vec<FnArg>, Vec<S
// into:
//
// fn foo(self: <ty>, mut __arg1: <ty>) -> impl Stream<Item = u32> {
// from_generator(static move || {
// from_coroutine(static move || {
// let ref <ident> = __arg1;
//
// // ...
Expand Down Expand Up @@ -358,14 +358,14 @@ fn make_gen_body(
) -> TokenStream {
let (gen_function, ret_value, ret_ty) = match cx {
Context::Stream => (
quote!(::futures_async_stream::__private::stream::from_generator),
quote!(::futures_async_stream::__private::stream::from_coroutine),
TokenStream::new(),
quote!(()),
),
Context::TryStream => {
let error = error.map_or_else(|| quote!(_), ToTokens::to_token_stream);
(
quote!(::futures_async_stream::__private::try_stream::from_generator),
quote!(::futures_async_stream::__private::try_stream::from_coroutine),
quote!(::futures_async_stream::__private::Ok(())),
quote!(::futures_async_stream::__private::Result<(), #error>),
)
Expand All @@ -380,7 +380,7 @@ fn make_gen_body(
| -> #ret_ty {
let (): () = #block;

// Ensure that this closure is a generator, even if it doesn't
// Ensure that this closure is a coroutine, even if it doesn't
// have any `yield` statements.
#[allow(unreachable_code)]
{
Expand Down
10 changes: 5 additions & 5 deletions futures-async-stream-macro/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{

/// The scope in which `#[for_await]`, `.await`, or `yield` was called.
///
/// The type of generator depends on which scope is called.
/// The type of coroutine depends on which scope is called.
#[derive(Clone, Copy, PartialEq)]
pub(crate) enum Scope {
/// `async fn`, `async {}`, or `async ||`
Expand Down Expand Up @@ -93,8 +93,8 @@ impl Visitor {

let pinned = def_site_ident!("__pinned");

// It needs to adjust the type yielded by the macro because generators used internally by
// async fn yield `()` type, but generators used internally by `stream` yield
// It needs to adjust the type yielded by the macro because coroutines used internally by
// async fn yield `()` type, but coroutines used internally by `stream` yield
// `Poll<U>` type.
let match_next = match self.scope {
Scope::Future => {
Expand Down Expand Up @@ -226,8 +226,8 @@ impl Visitor {

/// Visits `<base>.await`.
///
/// It needs to adjust the type yielded by the macro because generators used internally by
/// async fn yield `()` type, but generators used internally by `stream` yield
/// It needs to adjust the type yielded by the macro because coroutines used internally by
/// async fn yield `()` type, but coroutines used internally by `stream` yield
/// `Poll<U>` type.
fn visit_await(&self, expr: &mut Expr) {
if !self.scope.is_stream() {
Expand Down
Loading

0 comments on commit bf37805

Please sign in to comment.