1- This error occurs because a borrow in a coroutine persists across a
1+ This error occurs because a borrow in a movable coroutine persists across a
22yield point.
33
44Erroneous code example:
@@ -15,19 +15,35 @@ let mut b = #[coroutine] || {
1515Pin::new(&mut b).resume(());
1616```
1717
18- At present, it is not permitted to have a yield that occurs while a
19- borrow is still in scope. To resolve this error, the borrow must
20- either be "contained" to a smaller scope that does not overlap the
21- yield or else eliminated in another way. So, for example, we might
22- resolve the previous example by removing the borrow and just storing
23- the integer by value:
18+ Coroutines may be either unmarked, or marked with ` static ` . If it is unmarked,
19+ then the coroutine is considered "movable". At present, it is not permitted to
20+ have a yield in a movable coroutine that occurs while a borrow is still in
21+ scope. To resolve this error, the coroutine may be marked ` static ` :
22+
23+ ```
24+ # #![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
25+ # use std::ops::Coroutine;
26+ # use std::pin::Pin;
27+ let mut b = #[coroutine] static || { // <-- note the static keyword
28+ let a = &String::from("hello, world");
29+ yield ();
30+ println!("{}", a);
31+ };
32+ let b = std::pin::pin!(b);
33+ b.as_mut().resume(());
34+ ```
35+
36+ If the coroutine must remain movable, for example to be used as ` Unpin `
37+ without pinning it on the stack or in an allocation, we can alternatively
38+ resolve the previous example by removing the borrow and just storing the
39+ type by value:
2440
2541```
2642# #![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
2743# use std::ops::Coroutine;
2844# use std::pin::Pin;
2945let mut b = #[coroutine] || {
30- let a = 3 ;
46+ let a = String::from("hello, world") ;
3147 yield ();
3248 println!("{}", a);
3349};
0 commit comments