]>
Commit | Line | Data |
---|---|---|
ea8adc8c XL |
1 | // Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
b7449926 XL |
11 | // run-pass |
12 | ||
0531ce1d | 13 | #![feature(generators, generator_trait)] |
ea8adc8c XL |
14 | |
15 | use std::ops::{GeneratorState, Generator}; | |
16 | ||
17 | struct W<T>(T); | |
18 | ||
0531ce1d XL |
19 | // This impl isn't safe in general, but the generator used in this test is movable |
20 | // so it won't cause problems. | |
ea8adc8c XL |
21 | impl<T: Generator<Return = ()>> Iterator for W<T> { |
22 | type Item = T::Yield; | |
23 | ||
24 | fn next(&mut self) -> Option<Self::Item> { | |
0531ce1d | 25 | match unsafe { self.0.resume() } { |
ea8adc8c XL |
26 | GeneratorState::Complete(..) => None, |
27 | GeneratorState::Yielded(v) => Some(v), | |
28 | } | |
29 | } | |
30 | } | |
31 | ||
32 | fn test() -> impl Generator<Return=(), Yield=u8> { | |
33 | || { | |
34 | for i in 1..6 { | |
35 | yield i | |
36 | } | |
37 | } | |
38 | } | |
39 | ||
40 | fn main() { | |
41 | let end = 11; | |
42 | ||
43 | let closure_test = |start| { | |
44 | move || { | |
45 | for i in start..end { | |
46 | yield i | |
47 | } | |
48 | } | |
49 | }; | |
50 | ||
51 | assert!(W(test()).chain(W(closure_test(6))).eq(1..11)); | |
52 | } |