1
Fork 0

Replace all uses of generator in markdown documentation with coroutine

This commit is contained in:
Oli Scherer 2023-10-20 16:59:23 +00:00
parent 6fa47e15e7
commit 258af95a60
8 changed files with 52 additions and 52 deletions

View file

@ -1,10 +1,10 @@
This error occurs because a borrow in a generator persists across a This error occurs because a borrow in a coroutine persists across a
yield point. yield point.
Erroneous code example: Erroneous code example:
```compile_fail,E0626 ```compile_fail,E0626
# #![feature(generators, generator_trait, pin)] # #![feature(coroutines, coroutine_trait, pin)]
# use std::ops::Coroutine; # use std::ops::Coroutine;
# use std::pin::Pin; # use std::pin::Pin;
let mut b = || { let mut b = || {
@ -23,7 +23,7 @@ resolve the previous example by removing the borrow and just storing
the integer by value: the integer by value:
``` ```
# #![feature(generators, generator_trait, pin)] # #![feature(coroutines, coroutine_trait, pin)]
# use std::ops::Coroutine; # use std::ops::Coroutine;
# use std::pin::Pin; # use std::pin::Pin;
let mut b = || { let mut b = || {
@ -41,7 +41,7 @@ in those cases, something like the `Rc` or `Arc` types may be useful.
This error also frequently arises with iteration: This error also frequently arises with iteration:
```compile_fail,E0626 ```compile_fail,E0626
# #![feature(generators, generator_trait, pin)] # #![feature(coroutines, coroutine_trait, pin)]
# use std::ops::Coroutine; # use std::ops::Coroutine;
# use std::pin::Pin; # use std::pin::Pin;
let mut b = || { let mut b = || {
@ -57,7 +57,7 @@ Such cases can sometimes be resolved by iterating "by value" (or using
`into_iter()`) to avoid borrowing: `into_iter()`) to avoid borrowing:
``` ```
# #![feature(generators, generator_trait, pin)] # #![feature(coroutines, coroutine_trait, pin)]
# use std::ops::Coroutine; # use std::ops::Coroutine;
# use std::pin::Pin; # use std::pin::Pin;
let mut b = || { let mut b = || {
@ -72,7 +72,7 @@ Pin::new(&mut b).resume(());
If taking ownership is not an option, using indices can work too: If taking ownership is not an option, using indices can work too:
``` ```
# #![feature(generators, generator_trait, pin)] # #![feature(coroutines, coroutine_trait, pin)]
# use std::ops::Coroutine; # use std::ops::Coroutine;
# use std::pin::Pin; # use std::pin::Pin;
let mut b = || { let mut b = || {

View file

@ -1,28 +1,28 @@
A yield expression was used outside of the generator literal. A yield expression was used outside of the coroutine literal.
Erroneous code example: Erroneous code example:
```compile_fail,E0627 ```compile_fail,E0627
#![feature(generators, generator_trait)] #![feature(coroutines, coroutine_trait)]
fn fake_generator() -> &'static str { fn fake_coroutine() -> &'static str {
yield 1; yield 1;
return "foo" return "foo"
} }
fn main() { fn main() {
let mut generator = fake_generator; let mut coroutine = fake_coroutine;
} }
``` ```
The error occurs because keyword `yield` can only be used inside the generator The error occurs because keyword `yield` can only be used inside the coroutine
literal. This can be fixed by constructing the generator correctly. literal. This can be fixed by constructing the coroutine correctly.
``` ```
#![feature(generators, generator_trait)] #![feature(coroutines, coroutine_trait)]
fn main() { fn main() {
let mut generator = || { let mut coroutine = || {
yield 1; yield 1;
return "foo" return "foo"
}; };

View file

@ -1,13 +1,13 @@
More than one parameter was used for a generator. More than one parameter was used for a coroutine.
Erroneous code example: Erroneous code example:
```compile_fail,E0628 ```compile_fail,E0628
#![feature(generators, generator_trait)] #![feature(coroutines, coroutine_trait)]
fn main() { fn main() {
let generator = |a: i32, b: i32| { let coroutine = |a: i32, b: i32| {
// error: too many parameters for a generator // error: too many parameters for a coroutine
// Allowed only 0 or 1 parameter // Allowed only 0 or 1 parameter
yield a; yield a;
}; };
@ -15,15 +15,15 @@ fn main() {
``` ```
At present, it is not permitted to pass more than one explicit At present, it is not permitted to pass more than one explicit
parameter for a generator.This can be fixed by using parameter for a coroutine.This can be fixed by using
at most 1 parameter for the generator. For example, we might resolve at most 1 parameter for the coroutine. For example, we might resolve
the previous example by passing only one parameter. the previous example by passing only one parameter.
``` ```
#![feature(generators, generator_trait)] #![feature(coroutines, coroutine_trait)]
fn main() { fn main() {
let generator = |a: i32| { let coroutine = |a: i32| {
yield a; yield a;
}; };
} }

View file

@ -1,7 +1,7 @@
#### Note: this error code is no longer emitted by the compiler. #### Note: this error code is no longer emitted by the compiler.
When using generators (or async) all type variables must be bound so a When using coroutines (or async) all type variables must be bound so a
generator can be constructed. coroutine can be constructed.
Erroneous code example: Erroneous code example:
@ -15,7 +15,7 @@ async fn foo() {
In the above example `T` is unknowable by the compiler. In the above example `T` is unknowable by the compiler.
To fix this you must bind `T` to a concrete type such as `String` To fix this you must bind `T` to a concrete type such as `String`
so that a generator can then be constructed: so that a coroutine can then be constructed:
```edition2018 ```edition2018
async fn bar<T>() -> () {} async fn bar<T>() -> () {}

View file

@ -3,10 +3,10 @@ A `yield` clause was used in an `async` context.
Erroneous code example: Erroneous code example:
```compile_fail,E0727,edition2018 ```compile_fail,E0727,edition2018
#![feature(generators)] #![feature(coroutines)]
fn main() { fn main() {
let generator = || { let coroutine = || {
async { async {
yield; yield;
} }
@ -20,10 +20,10 @@ which is not yet supported.
To fix this error, you have to move `yield` out of the `async` block: To fix this error, you have to move `yield` out of the `async` block:
```edition2018 ```edition2018
#![feature(generators)] #![feature(coroutines)]
fn main() { fn main() {
let generator = || { let coroutine = || {
yield; yield;
}; };
} }

View file

@ -6,7 +6,7 @@ The tracking issue for this feature is: [#87417]
------------------------ ------------------------
Allows using the `#[track_caller]` attribute on closures and generators. Allows using the `#[track_caller]` attribute on closures and coroutines.
Calls made to the closure or generator will have caller information Calls made to the closure or coroutine will have caller information
available through `std::panic::Location::caller()`, just like using available through `std::panic::Location::caller()`, just like using
`#[track_caller]` on a function. `#[track_caller]` on a function.

View file

@ -5,31 +5,31 @@ each one organized by a "feature flag." That is, when using an unstable
feature of Rust, you must use a flag, like this: feature of Rust, you must use a flag, like this:
```rust ```rust
#![feature(generators, generator_trait)] #![feature(coroutines, coroutine_trait)]
use std::ops::{Generator, GeneratorState}; use std::ops::{Coroutine, CoroutineState};
use std::pin::Pin; use std::pin::Pin;
fn main() { fn main() {
let mut generator = || { let mut coroutine = || {
yield 1; yield 1;
return "foo" return "foo"
}; };
match Pin::new(&mut generator).resume(()) { match Pin::new(&mut coroutine).resume(()) {
GeneratorState::Yielded(1) => {} CoroutineState::Yielded(1) => {}
_ => panic!("unexpected value from resume"), _ => panic!("unexpected value from resume"),
} }
match Pin::new(&mut generator).resume(()) { match Pin::new(&mut coroutine).resume(()) {
GeneratorState::Complete("foo") => {} CoroutineState::Complete("foo") => {}
_ => panic!("unexpected value from resume"), _ => panic!("unexpected value from resume"),
} }
} }
``` ```
The `generators` feature [has a chapter][generators] describing how to use it. The `coroutines` feature [has a chapter][coroutines] describing how to use it.
[generators]: language-features/generators.md [coroutines]: language-features/coroutines.md
Because this documentation relates to unstable features, we make no guarantees Because this documentation relates to unstable features, we make no guarantees
that what is contained here is accurate or up to date. It's developed on a that what is contained here is accurate or up to date. It's developed on a

View file

@ -1,37 +1,37 @@
LL| |#![feature(generators, generator_trait)] LL| |#![feature(coroutines, coroutine_trait)]
LL| |#![allow(unused_assignments)] LL| |#![allow(unused_assignments)]
LL| | LL| |
LL| |use std::ops::{Generator, GeneratorState}; LL| |use std::ops::{Coroutine, CoroutineState};
LL| |use std::pin::Pin; LL| |use std::pin::Pin;
LL| | LL| |
LL| 1|fn main() { LL| 1|fn main() {
LL| 1| let mut generator = || { LL| 1| let mut coroutine = || {
LL| 1| yield 1; LL| 1| yield 1;
LL| 1| return "foo"; LL| 1| return "foo";
LL| 1| }; LL| 1| };
LL| | LL| |
LL| 1| match Pin::new(&mut generator).resume(()) { LL| 1| match Pin::new(&mut coroutine).resume(()) {
LL| 1| GeneratorState::Yielded(1) => {} LL| 1| CoroutineState::Yielded(1) => {}
LL| 0| _ => panic!("unexpected value from resume"), LL| 0| _ => panic!("unexpected value from resume"),
LL| | } LL| | }
LL| 1| match Pin::new(&mut generator).resume(()) { LL| 1| match Pin::new(&mut coroutine).resume(()) {
LL| 1| GeneratorState::Complete("foo") => {} LL| 1| CoroutineState::Complete("foo") => {}
LL| 0| _ => panic!("unexpected value from resume"), LL| 0| _ => panic!("unexpected value from resume"),
LL| | } LL| | }
LL| | LL| |
LL| 1| let mut generator = || { LL| 1| let mut coroutine = || {
LL| 1| yield 1; LL| 1| yield 1;
LL| 1| yield 2; LL| 1| yield 2;
LL| 0| yield 3; LL| 0| yield 3;
LL| 0| return "foo"; LL| 0| return "foo";
LL| 0| }; LL| 0| };
LL| | LL| |
LL| 1| match Pin::new(&mut generator).resume(()) { LL| 1| match Pin::new(&mut coroutine).resume(()) {
LL| 1| GeneratorState::Yielded(1) => {} LL| 1| CoroutineState::Yielded(1) => {}
LL| 0| _ => panic!("unexpected value from resume"), LL| 0| _ => panic!("unexpected value from resume"),
LL| | } LL| | }
LL| 1| match Pin::new(&mut generator).resume(()) { LL| 1| match Pin::new(&mut coroutine).resume(()) {
LL| 1| GeneratorState::Yielded(2) => {} LL| 1| CoroutineState::Yielded(2) => {}
LL| 0| _ => panic!("unexpected value from resume"), LL| 0| _ => panic!("unexpected value from resume"),
LL| | } LL| | }
LL| 1|} LL| 1|}