]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/trait-bounds-in-arc.rs
250ed58a8ef4c5040cd0d1389b68521695ff0193
[rustc.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 threads as long as all types fulfill Send.
13
14 // ignore-pretty
15
16 #![allow(unknown_features)]
17 #![feature(box_syntax, std_misc)]
18 #![feature(unboxed_closures)]
19
20 use std::sync::Arc;
21 use std::sync::mpsc::channel;
22 use std::thread;
23
24 trait Pet {
25 fn name(&self, blk: Box<FnMut(&str)>);
26 fn num_legs(&self) -> usize;
27 fn of_good_pedigree(&self) -> bool;
28 }
29
30 struct Catte {
31 num_whiskers: usize,
32 name: String,
33 }
34
35 struct Dogge {
36 bark_decibels: usize,
37 tricks_known: usize,
38 name: String,
39 }
40
41 struct Goldfyshe {
42 swim_speed: usize,
43 name: String,
44 }
45
46 impl Pet for Catte {
47 fn name(&self, mut blk: Box<FnMut(&str)>) { blk(&self.name) }
48 fn num_legs(&self) -> usize { 4 }
49 fn of_good_pedigree(&self) -> bool { self.num_whiskers >= 4 }
50 }
51 impl Pet for Dogge {
52 fn name(&self, mut blk: Box<FnMut(&str)>) { blk(&self.name) }
53 fn num_legs(&self) -> usize { 4 }
54 fn of_good_pedigree(&self) -> bool {
55 self.bark_decibels < 70 || self.tricks_known > 20
56 }
57 }
58 impl Pet for Goldfyshe {
59 fn name(&self, mut blk: Box<FnMut(&str)>) { blk(&self.name) }
60 fn num_legs(&self) -> usize { 0 }
61 fn of_good_pedigree(&self) -> bool { self.swim_speed >= 500 }
62 }
63
64 pub fn main() {
65 let catte = Catte { num_whiskers: 7, name: "alonzo_church".to_string() };
66 let dogge1 = Dogge {
67 bark_decibels: 100,
68 tricks_known: 42,
69 name: "alan_turing".to_string(),
70 };
71 let dogge2 = Dogge {
72 bark_decibels: 55,
73 tricks_known: 11,
74 name: "albert_einstein".to_string(),
75 };
76 let fishe = Goldfyshe {
77 swim_speed: 998,
78 name: "alec_guinness".to_string(),
79 };
80 let arc = Arc::new(vec!(box catte as Box<Pet+Sync+Send>,
81 box dogge1 as Box<Pet+Sync+Send>,
82 box fishe as Box<Pet+Sync+Send>,
83 box dogge2 as Box<Pet+Sync+Send>));
84 let (tx1, rx1) = channel();
85 let arc1 = arc.clone();
86 let t1 = thread::spawn(move|| { check_legs(arc1); tx1.send(()); });
87 let (tx2, rx2) = channel();
88 let arc2 = arc.clone();
89 let t2 = thread::spawn(move|| { check_names(arc2); tx2.send(()); });
90 let (tx3, rx3) = channel();
91 let arc3 = arc.clone();
92 let t3 = thread::spawn(move|| { check_pedigree(arc3); tx3.send(()); });
93 rx1.recv();
94 rx2.recv();
95 rx3.recv();
96 t1.join();
97 t2.join();
98 t3.join();
99 }
100
101 fn check_legs(arc: Arc<Vec<Box<Pet+Sync+Send>>>) {
102 let mut legs = 0;
103 for pet in &*arc {
104 legs += pet.num_legs();
105 }
106 assert!(legs == 12);
107 }
108 fn check_names(arc: Arc<Vec<Box<Pet+Sync+Send>>>) {
109 for pet in &*arc {
110 // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
111 pet.name(Box::new(|name| {
112 assert!(name.as_bytes()[0] == 'a' as u8 && name.as_bytes()[1] == 'l' as u8);
113 }))
114 }
115 }
116 fn check_pedigree(arc: Arc<Vec<Box<Pet+Sync+Send>>>) {
117 for pet in &*arc {
118 assert!(pet.of_good_pedigree());
119 }
120 }