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
2
2
yield point.
3
3
4
4
Erroneous code example:
@@ -15,19 +15,35 @@ let mut b = #[coroutine] || {
15
15
Pin::new(&mut b).resume(());
16
16
```
17
17
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:
24
40
25
41
```
26
42
# #![feature(coroutines, coroutine_trait, stmt_expr_attributes)]
27
43
# use std::ops::Coroutine;
28
44
# use std::pin::Pin;
29
45
let mut b = #[coroutine] || {
30
- let a = 3 ;
46
+ let a = String::from("hello, world") ;
31
47
yield ();
32
48
println!("{}", a);
33
49
};
0 commit comments