]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issue-8827.rs
Rollup merge of #31610 - Manishearth:doc-clarify-txrx, r=steveklabnik
[rust.git] / src / test / run-pass / issue-8827.rs
1 // Copyright 2014 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
11 // ignore-emscripten no threads support
12
13 #![feature(std_misc)]
14
15 use std::thread;
16 use std::sync::mpsc::{channel, Receiver};
17
18 fn periodical(n: isize) -> Receiver<bool> {
19     let (chan, port) = channel();
20     thread::spawn(move|| {
21         loop {
22             for _ in 1..n {
23                 match chan.send(false) {
24                     Ok(()) => {}
25                     Err(..) => break,
26                 }
27             }
28             match chan.send(true) {
29                 Ok(()) => {}
30                 Err(..) => break
31             }
32         }
33     });
34     return port;
35 }
36
37 fn integers() -> Receiver<isize> {
38     let (chan, port) = channel();
39     thread::spawn(move|| {
40         let mut i = 1;
41         loop {
42             match chan.send(i) {
43                 Ok(()) => {}
44                 Err(..) => break,
45             }
46             i = i + 1;
47         }
48     });
49     return port;
50 }
51
52 fn main() {
53     let ints = integers();
54     let threes = periodical(3);
55     let fives = periodical(5);
56     for _ in 1..100 {
57         match (ints.recv().unwrap(), threes.recv().unwrap(), fives.recv().unwrap()) {
58             (_, true, true) => println!("FizzBuzz"),
59             (_, true, false) => println!("Fizz"),
60             (_, false, true) => println!("Buzz"),
61             (i, false, false) => println!("{}", i)
62         }
63     }
64 }