]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/trait-bounds-in-arc.rs
Merge pull request #20510 from tshepang/patch-6
[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 tasks as long as all types fulfill Send.
13
14 use std::sync::Arc;
15 use std::sync::mpsc::channel;
16 use std::thread::Thread;
17
18 trait Pet {
19     fn name(&self, blk: |&str|);
20     fn num_legs(&self) -> uint;
21     fn of_good_pedigree(&self) -> bool;
22 }
23
24 struct Catte {
25     num_whiskers: uint,
26     name: String,
27 }
28
29 struct Dogge {
30     bark_decibels: uint,
31     tricks_known: uint,
32     name: String,
33 }
34
35 struct Goldfyshe {
36     swim_speed: uint,
37     name: String,
38 }
39
40 impl Pet for Catte {
41     fn name(&self, blk: |&str|) { blk(self.name.as_slice()) }
42     fn num_legs(&self) -> uint { 4 }
43     fn of_good_pedigree(&self) -> bool { self.num_whiskers >= 4 }
44 }
45 impl Pet for Dogge {
46     fn name(&self, blk: |&str|) { blk(self.name.as_slice()) }
47     fn num_legs(&self) -> uint { 4 }
48     fn of_good_pedigree(&self) -> bool {
49         self.bark_decibels < 70 || self.tricks_known > 20
50     }
51 }
52 impl Pet for Goldfyshe {
53     fn name(&self, blk: |&str|) { blk(self.name.as_slice()) }
54     fn num_legs(&self) -> uint { 0 }
55     fn of_good_pedigree(&self) -> bool { self.swim_speed >= 500 }
56 }
57
58 pub fn main() {
59     let catte = Catte { num_whiskers: 7, name: "alonzo_church".to_string() };
60     let dogge1 = Dogge {
61         bark_decibels: 100,
62         tricks_known: 42,
63         name: "alan_turing".to_string(),
64     };
65     let dogge2 = Dogge {
66         bark_decibels: 55,
67         tricks_known: 11,
68         name: "albert_einstein".to_string(),
69     };
70     let fishe = Goldfyshe {
71         swim_speed: 998,
72         name: "alec_guinness".to_string(),
73     };
74     let arc = Arc::new(vec!(box catte  as Box<Pet+Sync+Send>,
75                             box dogge1 as Box<Pet+Sync+Send>,
76                             box fishe  as Box<Pet+Sync+Send>,
77                             box dogge2 as Box<Pet+Sync+Send>));
78     let (tx1, rx1) = channel();
79     let arc1 = arc.clone();
80     let _t1 = Thread::spawn(move|| { check_legs(arc1); tx1.send(()); });
81     let (tx2, rx2) = channel();
82     let arc2 = arc.clone();
83     let _t2 = Thread::spawn(move|| { check_names(arc2); tx2.send(()); });
84     let (tx3, rx3) = channel();
85     let arc3 = arc.clone();
86     let _t3 = Thread::spawn(move|| { check_pedigree(arc3); tx3.send(()); });
87     rx1.recv();
88     rx2.recv();
89     rx3.recv();
90 }
91
92 fn check_legs(arc: Arc<Vec<Box<Pet+Sync+Send>>>) {
93     let mut legs = 0;
94     for pet in arc.iter() {
95         legs += pet.num_legs();
96     }
97     assert!(legs == 12);
98 }
99 fn check_names(arc: Arc<Vec<Box<Pet+Sync+Send>>>) {
100     for pet in arc.iter() {
101         pet.name(|name| {
102             assert!(name.as_bytes()[0] == 'a' as u8 && name.as_bytes()[1] == 'l' as u8);
103         })
104     }
105 }
106 fn check_pedigree(arc: Arc<Vec<Box<Pet+Sync+Send>>>) {
107     for pet in arc.iter() {
108         assert!(pet.of_good_pedigree());
109     }
110 }