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.
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.
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.
21 fn name(&self, blk: |&str|);
22 fn num_legs(&self) -> uint;
23 fn of_good_pedigree(&self) -> bool;
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 }
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
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 }
61 let catte = Catte { num_whiskers: 7, name: "alonzo_church".to_strbuf() };
65 name: "alan_turing".to_strbuf(),
70 name: "albert_einstein".to_strbuf(),
72 let fishe = Goldfyshe {
74 name: "alec_guinness".to_strbuf(),
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(()); });
94 fn check_legs(arc: Arc<Vec<Box<Pet:Share+Send>>>) {
96 for pet in arc.iter() {
97 legs += pet.num_legs();
101 fn check_names(arc: Arc<Vec<Box<Pet:Share+Send>>>) {
102 for pet in arc.iter() {
104 assert!(name[0] == 'a' as u8 && name[1] == 'l' as u8);
108 fn check_pedigree(arc: Arc<Vec<Box<Pet:Share+Send>>>) {
109 for pet in arc.iter() {
110 assert!(pet.of_good_pedigree());