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 28507ca commit daf9165
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ 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). This renames unstable `generator_trait` feature to `coroutine_trait`. The old cargo feature name is kept as a deprecated alias and will be removed in the next breaking release.

## [0.8.2] - 2023-08-10

- Update `syn` to 2. ([#158](https://github.com/taiki-e/auto_enums/pull/158), thanks @cuviper)
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ unstable = []

# Enable unstable features of [std|core] libraries

# Enable to use `[std|core]::ops::Generator` trait.
generator_trait = []
# Enable to use `[std|core]::ops::Coroutine` trait.
coroutine_trait = []
generator_trait = ["coroutine_trait"] # alias for coroutine_trait; TODO: remove in the next breaking release
# Enable to use `[std|core]::ops`'s `Fn`, `FnMut`, and `FnOnce` traits.
fn_traits = []
# Enable to use `[std|core]::iter::TrustedLen` trait.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ enum Foo<A, B> {
- Enable to use [tokio v0.2][tokio02] traits.
- **`tokio01`**
- Enable to use [tokio v0.1][tokio01] traits.
- **`generator_trait`**
- Enable to use `[std|core]::ops::Generator` trait.
- **`coroutine_trait`**
- Enable to use `[std|core]::ops::Coroutine` trait.
- Note that this feature is unstable and may cause incompatible changes between patch versions.
- **`fn_traits`**
- Enable to use `[std|core]::ops`'s `Fn`, `FnMut`, and `FnOnce` traits.
Expand Down
2 changes: 1 addition & 1 deletion src/auto_enum/type_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const TRAITS: &[&str] = &[
"Fn",
"FnMut",
"FnOnce",
"Generator",
"Coroutine",
"Future",
// std
"Read",
Expand Down
12 changes: 6 additions & 6 deletions src/derive/core/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,22 @@ pub(crate) mod range_bounds {
}
}

#[cfg(feature = "generator_trait")]
pub(crate) mod generator {
#[cfg(feature = "coroutine_trait")]
pub(crate) mod coroutine {
use quote::ToTokens;

use crate::derive::*;

pub(crate) const NAME: &[&str] = &["Generator"];
pub(crate) const NAME: &[&str] = &["Coroutine"];

pub(crate) fn derive(cx: &Context, data: &Data) -> Result<TokenStream> {
cx.needs_pin_projection();

let ident = &data.ident;
let pin = quote!(::core::pin::Pin);
let trait_: syn::Path = parse_quote!(::core::ops::Generator);
let trait_: syn::Path = parse_quote!(::core::ops::Coroutine);
let mut impl_ = EnumImpl::from_trait(data, trait_.clone(), None, parse_quote! {
trait Generator<R> {
trait Coroutine<R> {
type Yield;
type Return;
}
Expand All @@ -124,7 +124,7 @@ pub(crate) mod generator {
fn resume(
self: #pin<&mut Self>,
arg: R,
) -> ::core::ops::GeneratorState<Self::Yield, Self::Return> {
) -> ::core::ops::CoroutineState<Self::Yield, Self::Return> {
unsafe {
match self.get_unchecked_mut() { #(#resume,)* }
}
Expand Down
4 changes: 2 additions & 2 deletions src/enum_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ fn get_derive(s: &str) -> Option<DeriveFn> {
core::ops::fn_mut,
#[cfg(feature = "fn_traits")]
core::ops::fn_once,
#[cfg(feature = "generator_trait")]
core::ops::generator,
#[cfg(feature = "coroutine_trait")]
core::ops::coroutine,
core::future,
// std
#[cfg(feature = "std")]
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ Note that some traits have aliases.
- [`Fn`](https://doc.rust-lang.org/std/ops/trait.Fn.html) *(requires `"fn_traits"` and `"unstable"` crate features)*
- [`FnMut`](https://doc.rust-lang.org/std/ops/trait.FnMut.html) *(requires `"fn_traits"` and `"unstable"` crate features)*
- [`FnOnce`](https://doc.rust-lang.org/std/ops/trait.FnOnce.html) *(requires `"fn_traits"` and `"unstable"` crate features)*
- [`Generator`](https://doc.rust-lang.org/nightly/std/ops/trait.Generator.html) *(requires `"generator_trait"` and `"unstable"` crate features)*
- [`Coroutine`](https://doc.rust-lang.org/nightly/std/ops/trait.Coroutine.html) *(requires `"coroutine_trait"` and `"unstable"` crate features)*
### `[std|core]::convert`
Expand Down Expand Up @@ -792,8 +792,8 @@ These don't derive traits, but derive inherent methods instead.
- Enable to use [tokio v0.2][tokio02] traits.
- **`tokio01`**
- Enable to use [tokio v0.1][tokio01] traits.
- **`generator_trait`**
- Enable to use `[std|core]::ops::Generator` trait.
- **`coroutine_trait`**
- Enable to use `[std|core]::ops::Coroutine` trait.
- Note that this feature is unstable and may cause incompatible changes between patch versions.
- **`fn_traits`**
- Enable to use `[std|core]::ops`'s `Fn`, `FnMut`, and `FnOnce` traits.
Expand Down Expand Up @@ -872,9 +872,9 @@ Please be careful if you return another traits with the same name.
#![warn(rust_2018_idioms, single_use_lifetimes, unreachable_pub, clippy::pedantic)]
#![allow(clippy::doc_markdown, clippy::too_many_lines, clippy::manual_assert)]

#[cfg(all(feature = "generator_trait", not(feature = "unstable")))]
#[cfg(all(feature = "coroutine_trait", not(feature = "unstable")))]
compile_error!(
"The `generator_trait` feature requires the `unstable` feature as an explicit opt-in to unstable features"
"The `coroutine_trait` feature requires the `unstable` feature as an explicit opt-in to unstable features"
);

#[cfg(all(feature = "fn_traits", not(feature = "unstable")))]
Expand Down
2 changes: 1 addition & 1 deletion tests/auto_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
feature = "fn_traits",
feature(proc_macro_hygiene, stmt_expr_attributes, type_ascription)
)]
#![cfg_attr(feature = "generator_trait", feature(generator_trait))]
#![cfg_attr(feature = "coroutine_trait", feature(coroutine_trait))]
#![cfg_attr(feature = "fn_traits", feature(fn_traits, unboxed_closures))]
#![cfg_attr(feature = "trusted_len", feature(trusted_len))]
#![cfg_attr(not(feature = "std"), no_std)]
Expand Down
8 changes: 4 additions & 4 deletions tests/enum_derive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![cfg_attr(feature = "generator_trait", feature(generator_trait))]
#![cfg_attr(feature = "coroutine_trait", feature(coroutine_trait))]
#![cfg_attr(feature = "fn_traits", feature(fn_traits, unboxed_closures))]
#![cfg_attr(feature = "trusted_len", feature(trusted_len))]
#![warn(rust_2018_idioms, single_use_lifetimes)]
Expand Down Expand Up @@ -111,10 +111,10 @@ fn stable_std() {

// nightly

#[cfg(feature = "generator_trait")]
#[cfg(feature = "coroutine_trait")]
#[test]
fn generator_trait() {
#[enum_derive(Generator)]
fn coroutine_trait() {
#[enum_derive(Coroutine)]
enum Enum1<A, B> {
A(A),
B(B),
Expand Down

0 comments on commit daf9165

Please sign in to comment.