]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/trait-bounds-in-arc.rs
Rollup merge of #53110 - Xanewok:save-analysis-remap-path, r=nrc
[rust.git] / src / test / run-pass / trait-bounds-in-arc.rs
1 // Copyright 2013-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 // Tests that a heterogeneous list of existential types can be put inside an Arc
12 // and shared between threads as long as all types fulfill Send.
13
14 // ignore-emscripten no threads support
15
16 #![feature(box_syntax)]
17
18 use std::sync::Arc;
19 use std::sync::mpsc::channel;
20 use std::thread;
21
22 trait Pet {
23     fn name(&self, blk: Box<FnMut(&str)>);
24     fn num_legs(&self) -> usize;
25     fn of_good_pedigree(&self) -> bool;
26 }
27
28 struct Catte {
29     num_whiskers: usize,
30     name: String,
31 }
32
33 struct Dogge {
34     bark_decibels: usize,
35     tricks_known: usize,
36     name: String,
37 }
38
39 struct Goldfyshe {
40     swim_speed: usize,
41     name: String,
42 }
43
44 impl Pet for Catte {
45     fn name(&self, mut blk: Box<FnMut(&str)>) { blk(&self.name) }
46     fn num_legs(&self) -> usize { 4 }
47     fn of_good_pedigree(&self) -> bool { self.num_whiskers >= 4 }
48 }
49 impl Pet for Dogge {
50     fn name(&self, mut blk: Box<FnMut(&str)>) { blk(&self.name) }
51     fn num_legs(&self) -> usize { 4 }
52     fn of_good_pedigree(&self) -> bool {
53         self.bark_decibels < 70 || self.tricks_known > 20
54     }
55 }
56 impl Pet for Goldfyshe {
57     fn name(&self, mut blk: Box<FnMut(&str)>) { blk(&self.name) }
58     fn num_legs(&self) -> usize { 0 }
59     fn of_good_pedigree(&self) -> bool { self.swim_speed >= 500 }
60 }
61
62 pub fn main() {
63     let catte = Catte { num_whiskers: 7, name: "alonzo_church".to_string() };
64     let dogge1 = Dogge {
65         bark_decibels: 100,
66         tricks_known: 42,
67         name: "alan_turing".to_string(),
68     };
69     let dogge2 = Dogge {
70         bark_decibels: 55,
71         tricks_known: 11,
72         name: "albert_einstein".to_string(),
73     };
74     let fishe = Goldfyshe {
75         swim_speed: 998,
76         name: "alec_guinness".to_string(),
77     };
78     let arc = Arc::new(vec![box catte  as Box<Pet+Sync+Send>,
79                             box dogge1 as Box<Pet+Sync+Send>,
80                             box fishe  as Box<Pet+Sync+Send>,
81                             box dogge2 as Box<Pet+Sync+Send>]);
82     let (tx1, rx1) = channel();
83     let arc1 = arc.clone();
84     let t1 = thread::spawn(move|| { check_legs(arc1); tx1.send(()); });
85     let (tx2, rx2) = channel();
86     let arc2 = arc.clone();
87     let t2 = thread::spawn(move|| { check_names(arc2); tx2.send(()); });
88     let (tx3, rx3) = channel();
89     let arc3 = arc.clone();
90     let t3 = thread::spawn(move|| { check_pedigree(arc3); tx3.send(()); });
91     rx1.recv();
92     rx2.recv();
93     rx3.recv();
94     t1.join();
95     t2.join();
96     t3.join();
97 }
98
99 fn check_legs(arc: Arc<Vec<Box<Pet+Sync+Send>>>) {
100     let mut legs = 0;
101     for pet in arc.iter() {
102         legs += pet.num_legs();
103     }
104     assert!(legs == 12);
105 }
106 fn check_names(arc: Arc<Vec<Box<Pet+Sync+Send>>>) {
107     for pet in arc.iter() {
108         pet.name(Box::new(|name| {
109             assert!(name.as_bytes()[0] == 'a' as u8 && name.as_bytes()[1] == 'l' as u8);
110         }))
111     }
112 }
113 fn check_pedigree(arc: Arc<Vec<Box<Pet+Sync+Send>>>) {
114     for pet in arc.iter() {
115         assert!(pet.of_good_pedigree());
116     }
117 }