]> git.lizzy.rs Git - rust.git/blob - src/test/ui/traits/bound/in-arc.rs
Auto merge of #84959 - camsteffen:lint-suggest-group, r=estebank
[rust.git] / src / test / ui / traits / bound / in-arc.rs
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 #![feature(box_syntax)]
9
10 use std::sync::Arc;
11 use std::sync::mpsc::channel;
12 use std::thread;
13
14 trait Pet {
15     fn name(&self, blk: Box<dyn FnMut(&str)>);
16     fn num_legs(&self) -> usize;
17     fn of_good_pedigree(&self) -> bool;
18 }
19
20 struct Catte {
21     num_whiskers: usize,
22     name: String,
23 }
24
25 struct Dogge {
26     bark_decibels: usize,
27     tricks_known: usize,
28     name: String,
29 }
30
31 struct Goldfyshe {
32     swim_speed: usize,
33     name: String,
34 }
35
36 impl Pet for Catte {
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 }
40 }
41 impl Pet for Dogge {
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
46     }
47 }
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 }
52 }
53
54 pub fn main() {
55     let catte = Catte { num_whiskers: 7, name: "alonzo_church".to_string() };
56     let dogge1 = Dogge {
57         bark_decibels: 100,
58         tricks_known: 42,
59         name: "alan_turing".to_string(),
60     };
61     let dogge2 = Dogge {
62         bark_decibels: 55,
63         tricks_known: 11,
64         name: "albert_einstein".to_string(),
65     };
66     let fishe = Goldfyshe {
67         swim_speed: 998,
68         name: "alec_guinness".to_string(),
69     };
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(()); });
83     rx1.recv();
84     rx2.recv();
85     rx3.recv();
86     t1.join();
87     t2.join();
88     t3.join();
89 }
90
91 fn check_legs(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>) {
92     let mut legs = 0;
93     for pet in arc.iter() {
94         legs += pet.num_legs();
95     }
96     assert!(legs == 12);
97 }
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);
102         }))
103     }
104 }
105 fn check_pedigree(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>) {
106     for pet in arc.iter() {
107         assert!(pet.of_good_pedigree());
108     }
109 }