]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/trait-bounds-in-arc.rs
test: Automatically remove all `~[T]` from tests.
[rust.git] / src / test / run-pass / trait-bounds-in-arc.rs
1 // ignore-pretty
2
3 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
4 // file at the top-level directory of this distribution and at
5 // http://rust-lang.org/COPYRIGHT.
6 //
7 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 // option. This file may not be copied, modified, or distributed
11 // except according to those terms.
12
13 // Tests that a heterogeneous list of existential types can be put inside an Arc
14 // and shared between tasks as long as all types fulfill Freeze+Send.
15
16 // ignore-fast
17
18 extern crate sync;
19
20 use sync::Arc;
21 use std::task;
22
23 trait Pet {
24     fn name(&self, blk: |&str|);
25     fn num_legs(&self) -> uint;
26     fn of_good_pedigree(&self) -> bool;
27 }
28
29 struct Catte {
30     num_whiskers: uint,
31     name: ~str,
32 }
33
34 struct Dogge {
35     bark_decibels: uint,
36     tricks_known: uint,
37     name: ~str,
38 }
39
40 struct Goldfyshe {
41     swim_speed: uint,
42     name: ~str,
43 }
44
45 impl Pet for Catte {
46     fn name(&self, blk: |&str|) { blk(self.name) }
47     fn num_legs(&self) -> uint { 4 }
48     fn of_good_pedigree(&self) -> bool { self.num_whiskers >= 4 }
49 }
50 impl Pet for Dogge {
51     fn name(&self, blk: |&str|) { blk(self.name) }
52     fn num_legs(&self) -> uint { 4 }
53     fn of_good_pedigree(&self) -> bool {
54         self.bark_decibels < 70 || self.tricks_known > 20
55     }
56 }
57 impl Pet for Goldfyshe {
58     fn name(&self, blk: |&str|) { blk(self.name) }
59     fn num_legs(&self) -> uint { 0 }
60     fn of_good_pedigree(&self) -> bool { self.swim_speed >= 500 }
61 }
62
63 pub fn main() {
64     let catte = Catte { num_whiskers: 7, name: ~"alonzo_church" };
65     let dogge1 = Dogge { bark_decibels: 100, tricks_known: 42, name: ~"alan_turing" };
66     let dogge2 = Dogge { bark_decibels: 55,  tricks_known: 11, name: ~"albert_einstein" };
67     let fishe = Goldfyshe { swim_speed: 998, name: ~"alec_guinness" };
68     let arc = Arc::new(vec!(~catte  as ~Pet:Share+Send,
69                          ~dogge1 as ~Pet:Share+Send,
70                          ~fishe  as ~Pet:Share+Send,
71                          ~dogge2 as ~Pet:Share+Send));
72     let (tx1, rx1) = channel();
73     let arc1 = arc.clone();
74     task::spawn(proc() { check_legs(arc1); tx1.send(()); });
75     let (tx2, rx2) = channel();
76     let arc2 = arc.clone();
77     task::spawn(proc() { check_names(arc2); tx2.send(()); });
78     let (tx3, rx3) = channel();
79     let arc3 = arc.clone();
80     task::spawn(proc() { check_pedigree(arc3); tx3.send(()); });
81     rx1.recv();
82     rx2.recv();
83     rx3.recv();
84 }
85
86 fn check_legs(arc: Arc<Vec<~Pet:Share+Send>>) {
87     let mut legs = 0;
88     for pet in arc.get().iter() {
89         legs += pet.num_legs();
90     }
91     assert!(legs == 12);
92 }
93 <<<<<<< HEAD
94 fn check_names(arc: Arc<~[~Pet:Share+Send]>) {
95 =======
96 fn check_names(arc: Arc<Vec<~Pet:Freeze+Send> >) {
97 >>>>>>> test: Automatically remove all `~[T]` from tests.
98     for pet in arc.get().iter() {
99         pet.name(|name| {
100             assert!(name[0] == 'a' as u8 && name[1] == 'l' as u8);
101         })
102     }
103 }
104 <<<<<<< HEAD
105 fn check_pedigree(arc: Arc<~[~Pet:Share+Send]>) {
106 =======
107 fn check_pedigree(arc: Arc<Vec<~Pet:Freeze+Send> >) {
108 >>>>>>> test: Automatically remove all `~[T]` from tests.
109     for pet in arc.get().iter() {
110         assert!(pet.of_good_pedigree());
111     }
112 }