]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/generator/iterator-count.rs
New upstream version 1.29.0+dfsg1
[rustc.git] / src / test / run-pass / generator / iterator-count.rs
CommitLineData
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
0531ce1d 11#![feature(generators, generator_trait)]
ea8adc8c
XL
12
13use std::ops::{GeneratorState, Generator};
14
15struct W<T>(T);
16
0531ce1d
XL
17// This impl isn't safe in general, but the generator used in this test is movable
18// so it won't cause problems.
ea8adc8c
XL
19impl<T: Generator<Return = ()>> Iterator for W<T> {
20 type Item = T::Yield;
21
22 fn next(&mut self) -> Option<Self::Item> {
0531ce1d 23 match unsafe { self.0.resume() } {
ea8adc8c
XL
24 GeneratorState::Complete(..) => None,
25 GeneratorState::Yielded(v) => Some(v),
26 }
27 }
28}
29
30fn test() -> impl Generator<Return=(), Yield=u8> {
31 || {
32 for i in 1..6 {
33 yield i
34 }
35 }
36}
37
38fn main() {
39 let end = 11;
40
41 let closure_test = |start| {
42 move || {
43 for i in start..end {
44 yield i
45 }
46 }
47 };
48
49 assert!(W(test()).chain(W(closure_test(6))).eq(1..11));
50}