2 #![allow(unused_must_use)]
3 // Tests that a heterogeneous list of existential `dyn` types can be put inside an Arc
4 // and shared between threads as long as all types fulfill Send.
6 // ignore-emscripten no threads support
9 use std::sync::mpsc::channel;
13 fn name(&self, blk: Box<dyn FnMut(&str)>);
14 fn num_legs(&self) -> usize;
15 fn of_good_pedigree(&self) -> bool;
35 fn name(&self, mut blk: Box<dyn FnMut(&str)>) { blk(&self.name) }
36 fn num_legs(&self) -> usize { 4 }
37 fn of_good_pedigree(&self) -> bool { self.num_whiskers >= 4 }
40 fn name(&self, mut blk: Box<dyn FnMut(&str)>) { blk(&self.name) }
41 fn num_legs(&self) -> usize { 4 }
42 fn of_good_pedigree(&self) -> bool {
43 self.bark_decibels < 70 || self.tricks_known > 20
46 impl Pet for Goldfyshe {
47 fn name(&self, mut blk: Box<dyn FnMut(&str)>) { blk(&self.name) }
48 fn num_legs(&self) -> usize { 0 }
49 fn of_good_pedigree(&self) -> bool { self.swim_speed >= 500 }
53 let catte = Catte { num_whiskers: 7, name: "alonzo_church".to_string() };
57 name: "alan_turing".to_string(),
62 name: "albert_einstein".to_string(),
64 let fishe = Goldfyshe {
66 name: "alec_guinness".to_string(),
68 let arc = Arc::new(vec![
69 Box::new(catte) as Box<dyn Pet+Sync+Send>,
70 Box::new(dogge1) as Box<dyn Pet+Sync+Send>,
71 Box::new(fishe) as Box<dyn Pet+Sync+Send>,
72 Box::new(dogge2) as Box<dyn Pet+Sync+Send>]);
73 let (tx1, rx1) = channel();
74 let arc1 = arc.clone();
75 let t1 = thread::spawn(move|| { check_legs(arc1); tx1.send(()); });
76 let (tx2, rx2) = channel();
77 let arc2 = arc.clone();
78 let t2 = thread::spawn(move|| { check_names(arc2); tx2.send(()); });
79 let (tx3, rx3) = channel();
80 let arc3 = arc.clone();
81 let t3 = thread::spawn(move|| { check_pedigree(arc3); tx3.send(()); });
90 fn check_legs(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>) {
92 for pet in arc.iter() {
93 legs += pet.num_legs();
97 fn check_names(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>) {
98 for pet in arc.iter() {
99 pet.name(Box::new(|name| {
100 assert!(name.as_bytes()[0] == 'a' as u8 && name.as_bytes()[1] == 'l' as u8);
104 fn check_pedigree(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>) {
105 for pet in arc.iter() {
106 assert!(pet.of_good_pedigree());