]> git.lizzy.rs Git - rust.git/blob - tests/ui/generator/yield-while-iterating.rs
Rollup merge of #106644 - alexcrichton:update-wasi-toolchain, r=cuviper
[rust.git] / tests / ui / generator / yield-while-iterating.rs
1 #![feature(generators, generator_trait)]
2
3 use std::ops::{GeneratorState, Generator};
4 use std::cell::Cell;
5 use std::pin::Pin;
6
7 fn yield_during_iter_owned_data(x: Vec<i32>) {
8     // The generator owns `x`, so we error out when yielding with a
9     // reference to it.  This winds up becoming a rather confusing
10     // regionck error -- in particular, we would freeze with the
11     // reference in scope, and it doesn't live long enough.
12     let _b = move || {
13         for p in &x { //~ ERROR
14             yield();
15         }
16     };
17 }
18
19 fn yield_during_iter_borrowed_slice(x: &[i32]) {
20     let _b = move || {
21         for p in x {
22             yield();
23         }
24     };
25 }
26
27 fn yield_during_iter_borrowed_slice_2() {
28     let mut x = vec![22_i32];
29     let _b = || {
30         for p in &x {
31             yield();
32         }
33     };
34     println!("{:?}", x);
35 }
36
37 fn yield_during_iter_borrowed_slice_3() {
38     // OK to take a mutable ref to `x` and yield
39     // up pointers from it:
40     let mut x = vec![22_i32];
41     let mut b = || {
42         for p in &mut x {
43             yield p;
44         }
45     };
46     Pin::new(&mut b).resume(());
47 }
48
49 fn yield_during_iter_borrowed_slice_4() {
50     // ...but not OK to do that while reading
51     // from `x` too
52     let mut x = vec![22_i32];
53     let mut b = || {
54         for p in &mut x {
55             yield p;
56         }
57     };
58     println!("{}", x[0]); //~ ERROR
59     Pin::new(&mut b).resume(());
60 }
61
62 fn yield_during_range_iter() {
63     // Should be OK.
64     let mut b = || {
65         let v = vec![1,2,3];
66         let len = v.len();
67         for i in 0..len {
68             let x = v[i];
69             yield x;
70         }
71     };
72     Pin::new(&mut b).resume(());
73 }
74
75 fn main() { }