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
8 #![feature(box_syntax)]
11 use std::sync::mpsc::channel;
15 fn name(&self, blk: Box<dyn FnMut(&str)>);
16 fn num_legs(&self) -> usize;
17 fn of_good_pedigree(&self) -> bool;
37 fn name(&self, mut blk: Box<dyn FnMut(&str)>) { blk(&self.name) }
38 fn num_legs(&self) -> usize { 4 }
39 fn of_good_pedigree(&self) -> bool { self.num_whiskers >= 4 }
42 fn name(&self, mut blk: Box<dyn FnMut(&str)>) { blk(&self.name) }
43 fn num_legs(&self) -> usize { 4 }
44 fn of_good_pedigree(&self) -> bool {
45 self.bark_decibels < 70 || self.tricks_known > 20
48 impl Pet for Goldfyshe {
49 fn name(&self, mut blk: Box<dyn FnMut(&str)>) { blk(&self.name) }
50 fn num_legs(&self) -> usize { 0 }
51 fn of_good_pedigree(&self) -> bool { self.swim_speed >= 500 }
55 let catte = Catte { num_whiskers: 7, name: "alonzo_church".to_string() };
59 name: "alan_turing".to_string(),
64 name: "albert_einstein".to_string(),
66 let fishe = Goldfyshe {
68 name: "alec_guinness".to_string(),
70 let arc = Arc::new(vec![box catte as Box<dyn Pet+Sync+Send>,
71 box dogge1 as Box<dyn Pet+Sync+Send>,
72 box fishe as Box<dyn Pet+Sync+Send>,
73 box dogge2 as Box<dyn Pet+Sync+Send>]);
74 let (tx1, rx1) = channel();
75 let arc1 = arc.clone();
76 let t1 = thread::spawn(move|| { check_legs(arc1); tx1.send(()); });
77 let (tx2, rx2) = channel();
78 let arc2 = arc.clone();
79 let t2 = thread::spawn(move|| { check_names(arc2); tx2.send(()); });
80 let (tx3, rx3) = channel();
81 let arc3 = arc.clone();
82 let t3 = thread::spawn(move|| { check_pedigree(arc3); tx3.send(()); });
91 fn check_legs(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>) {
93 for pet in arc.iter() {
94 legs += pet.num_legs();
98 fn check_names(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>) {
99 for pet in arc.iter() {
100 pet.name(Box::new(|name| {
101 assert!(name.as_bytes()[0] == 'a' as u8 && name.as_bytes()[1] == 'l' as u8);
105 fn check_pedigree(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>) {
106 for pet in arc.iter() {
107 assert!(pet.of_good_pedigree());