1 // run-pass
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.
5 
6 // ignore-emscripten no threads support
7 
8 use std::sync::Arc;
9 use std::sync::mpsc::channel;
10 use std::thread;
11 
12 trait Pet {
name(&self, blk: Box<dyn FnMut(&str)>)13     fn name(&self, blk: Box<dyn FnMut(&str)>);
num_legs(&self) -> usize14     fn num_legs(&self) -> usize;
of_good_pedigree(&self) -> bool15     fn of_good_pedigree(&self) -> bool;
16 }
17 
18 struct Catte {
19     num_whiskers: usize,
20     name: String,
21 }
22 
23 struct Dogge {
24     bark_decibels: usize,
25     tricks_known: usize,
26     name: String,
27 }
28 
29 struct Goldfyshe {
30     swim_speed: usize,
31     name: String,
32 }
33 
34 impl Pet for Catte {
name(&self, mut blk: Box<dyn FnMut(&str)>)35     fn name(&self, mut blk: Box<dyn FnMut(&str)>) { blk(&self.name) }
num_legs(&self) -> usize36     fn num_legs(&self) -> usize { 4 }
of_good_pedigree(&self) -> bool37     fn of_good_pedigree(&self) -> bool { self.num_whiskers >= 4 }
38 }
39 impl Pet for Dogge {
name(&self, mut blk: Box<dyn FnMut(&str)>)40     fn name(&self, mut blk: Box<dyn FnMut(&str)>) { blk(&self.name) }
num_legs(&self) -> usize41     fn num_legs(&self) -> usize { 4 }
of_good_pedigree(&self) -> bool42     fn of_good_pedigree(&self) -> bool {
43         self.bark_decibels < 70 || self.tricks_known > 20
44     }
45 }
46 impl Pet for Goldfyshe {
name(&self, mut blk: Box<dyn FnMut(&str)>)47     fn name(&self, mut blk: Box<dyn FnMut(&str)>) { blk(&self.name) }
num_legs(&self) -> usize48     fn num_legs(&self) -> usize { 0 }
of_good_pedigree(&self) -> bool49     fn of_good_pedigree(&self) -> bool { self.swim_speed >= 500 }
50 }
51 
main()52 pub fn main() {
53     let catte = Catte { num_whiskers: 7, name: "alonzo_church".to_string() };
54     let dogge1 = Dogge {
55         bark_decibels: 100,
56         tricks_known: 42,
57         name: "alan_turing".to_string(),
58     };
59     let dogge2 = Dogge {
60         bark_decibels: 55,
61         tricks_known: 11,
62         name: "albert_einstein".to_string(),
63     };
64     let fishe = Goldfyshe {
65         swim_speed: 998,
66         name: "alec_guinness".to_string(),
67     };
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(()); });
82     rx1.recv();
83     rx2.recv();
84     rx3.recv();
85     t1.join();
86     t2.join();
87     t3.join();
88 }
89 
check_legs(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>)90 fn check_legs(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>) {
91     let mut legs = 0;
92     for pet in arc.iter() {
93         legs += pet.num_legs();
94     }
95     assert!(legs == 12);
96 }
check_names(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>)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);
101         }))
102     }
103 }
check_pedigree(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>)104 fn check_pedigree(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>) {
105     for pet in arc.iter() {
106         assert!(pet.of_good_pedigree());
107     }
108 }
109