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