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.
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.
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 Send.
23 fn name(&self, blk: |&str|);
24 fn num_legs(&self) -> uint;
25 fn of_good_pedigree(&self) -> bool;
45 fn name(&self, blk: |&str|) { blk(self.name) }
46 fn num_legs(&self) -> uint { 4 }
47 fn of_good_pedigree(&self) -> bool { self.num_whiskers >= 4 }
50 fn name(&self, blk: |&str|) { blk(self.name) }
51 fn num_legs(&self) -> uint { 4 }
52 fn of_good_pedigree(&self) -> bool {
53 self.bark_decibels < 70 || self.tricks_known > 20
56 impl Pet for Goldfyshe {
57 fn name(&self, blk: |&str|) { blk(self.name) }
58 fn num_legs(&self) -> uint { 0 }
59 fn of_good_pedigree(&self) -> bool { self.swim_speed >= 500 }
63 let catte = Catte { num_whiskers: 7, name: "alonzo_church".to_owned() };
64 let dogge1 = Dogge { bark_decibels: 100, tricks_known: 42, name: "alan_turing".to_owned() };
65 let dogge2 = Dogge { bark_decibels: 55, tricks_known: 11, name: "albert_einstein".to_owned() };
66 let fishe = Goldfyshe { swim_speed: 998, name: "alec_guinness".to_owned() };
67 let arc = Arc::new(vec!(box catte as Box<Pet:Share+Send>,
68 box dogge1 as Box<Pet:Share+Send>,
69 box fishe as Box<Pet:Share+Send>,
70 box dogge2 as Box<Pet:Share+Send>));
71 let (tx1, rx1) = channel();
72 let arc1 = arc.clone();
73 task::spawn(proc() { check_legs(arc1); tx1.send(()); });
74 let (tx2, rx2) = channel();
75 let arc2 = arc.clone();
76 task::spawn(proc() { check_names(arc2); tx2.send(()); });
77 let (tx3, rx3) = channel();
78 let arc3 = arc.clone();
79 task::spawn(proc() { check_pedigree(arc3); tx3.send(()); });
85 fn check_legs(arc: Arc<Vec<Box<Pet:Share+Send>>>) {
87 for pet in arc.iter() {
88 legs += pet.num_legs();
92 fn check_names(arc: Arc<Vec<Box<Pet:Share+Send>>>) {
93 for pet in arc.iter() {
95 assert!(name[0] == 'a' as u8 && name[1] == 'l' as u8);
99 fn check_pedigree(arc: Arc<Vec<Box<Pet:Share+Send>>>) {
100 for pet in arc.iter() {
101 assert!(pet.of_good_pedigree());