]> git.lizzy.rs Git - rust.git/blob - src/test/run-make/emit/test-26235.rs
Auto merge of #30457 - Manishearth:rollup, r=Manishearth
[rust.git] / src / test / run-make / emit / test-26235.rs
1 // Copyright 2015 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 // Checks for issue #26235
12
13 fn main() {
14     use std::thread;
15
16     type Key = u32;
17     const NUM_THREADS: usize = 2;
18
19     #[derive(Clone,Copy)]
20     struct Stats<S> {
21         upsert: S,
22         delete: S,
23         insert: S,
24         update: S
25     };
26
27     impl<S> Stats<S> where S: Copy {
28         fn dot<B, F, T>(self, s: Stats<T>, f: F) -> Stats<B> where F: Fn(S, T) -> B {
29             let Stats { upsert: u1, delete: d1, insert: i1, update: p1 } = self;
30             let Stats { upsert: u2, delete: d2, insert: i2, update: p2 } = s;
31             Stats { upsert: f(u1, u2), delete: f(d1, d2), insert: f(i1, i2), update: f(p1, p2) }
32         }
33
34         fn new(init: S) -> Self {
35             Stats { upsert: init, delete: init, insert: init, update: init }
36         }
37     }
38
39     fn make_threads() -> Vec<thread::JoinHandle<()>> {
40         let mut t = Vec::with_capacity(NUM_THREADS);
41         for _ in 0..NUM_THREADS {
42             t.push(thread::spawn(move || {}));
43         }
44         t
45     }
46
47     let stats = [Stats::new(0); NUM_THREADS];
48     make_threads();
49
50     {
51         let Stats { ref upsert, ref delete, ref insert, ref update } = stats.iter().fold(
52             Stats::new(0), |res, &s| res.dot(s, |x: Key, y: Key| x.wrapping_add(y)));
53         println!("upserts: {}, deletes: {}, inserts: {}, updates: {}",
54                  upsert, delete, insert, update);
55     }
56 }