2018-08-30 14:18:55 +02:00
|
|
|
// run-pass
|
2018-09-25 23:51:35 +02:00
|
|
|
#![allow(unused_must_use)]
|
2019-08-02 01:14:42 +01:00
|
|
|
// Tests that a heterogeneous list of existential `dyn` types can be put inside an Arc
|
2015-05-09 00:12:29 +09:00
|
|
|
// and shared between threads as long as all types fulfill Send.
|
2013-06-24 15:35:02 -04:00
|
|
|
|
2016-02-11 12:34:41 +01:00
|
|
|
// ignore-emscripten no threads support
|
2018-07-23 21:05:39 +01:00
|
|
|
|
2014-06-07 11:13:26 -07:00
|
|
|
use std::sync::Arc;
|
2014-12-23 11:53:35 -08:00
|
|
|
use std::sync::mpsc::channel;
|
2015-03-30 11:00:05 -07:00
|
|
|
use std::thread;
|
2013-06-24 15:35:02 -04:00
|
|
|
|
|
|
|
trait Pet {
|
2019-05-28 14:47:21 -04:00
|
|
|
fn name(&self, blk: Box<dyn FnMut(&str)>);
|
2015-03-25 17:06:52 -07:00
|
|
|
fn num_legs(&self) -> usize;
|
2013-06-24 15:35:02 -04:00
|
|
|
fn of_good_pedigree(&self) -> bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Catte {
|
2015-03-25 17:06:52 -07:00
|
|
|
num_whiskers: usize,
|
2014-05-22 16:57:53 -07:00
|
|
|
name: String,
|
2013-06-24 15:35:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Dogge {
|
2015-03-25 17:06:52 -07:00
|
|
|
bark_decibels: usize,
|
|
|
|
tricks_known: usize,
|
2014-05-22 16:57:53 -07:00
|
|
|
name: String,
|
2013-06-24 15:35:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Goldfyshe {
|
2015-03-25 17:06:52 -07:00
|
|
|
swim_speed: usize,
|
2014-05-22 16:57:53 -07:00
|
|
|
name: String,
|
2013-06-24 15:35:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Pet for Catte {
|
2019-05-28 14:47:21 -04:00
|
|
|
fn name(&self, mut blk: Box<dyn FnMut(&str)>) { blk(&self.name) }
|
2015-03-25 17:06:52 -07:00
|
|
|
fn num_legs(&self) -> usize { 4 }
|
2013-06-24 15:35:02 -04:00
|
|
|
fn of_good_pedigree(&self) -> bool { self.num_whiskers >= 4 }
|
|
|
|
}
|
|
|
|
impl Pet for Dogge {
|
2019-05-28 14:47:21 -04:00
|
|
|
fn name(&self, mut blk: Box<dyn FnMut(&str)>) { blk(&self.name) }
|
2015-03-25 17:06:52 -07:00
|
|
|
fn num_legs(&self) -> usize { 4 }
|
2013-06-24 15:35:02 -04:00
|
|
|
fn of_good_pedigree(&self) -> bool {
|
|
|
|
self.bark_decibels < 70 || self.tricks_known > 20
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Pet for Goldfyshe {
|
2019-05-28 14:47:21 -04:00
|
|
|
fn name(&self, mut blk: Box<dyn FnMut(&str)>) { blk(&self.name) }
|
2015-03-25 17:06:52 -07:00
|
|
|
fn num_legs(&self) -> usize { 0 }
|
2013-06-24 15:35:02 -04:00
|
|
|
fn of_good_pedigree(&self) -> bool { self.swim_speed >= 500 }
|
|
|
|
}
|
|
|
|
|
2014-01-03 15:30:54 -08:00
|
|
|
pub fn main() {
|
2014-05-25 03:17:19 -07:00
|
|
|
let catte = Catte { num_whiskers: 7, name: "alonzo_church".to_string() };
|
2014-05-12 17:56:43 -07:00
|
|
|
let dogge1 = Dogge {
|
|
|
|
bark_decibels: 100,
|
|
|
|
tricks_known: 42,
|
2014-05-25 03:17:19 -07:00
|
|
|
name: "alan_turing".to_string(),
|
2014-05-12 17:56:43 -07:00
|
|
|
};
|
|
|
|
let dogge2 = Dogge {
|
|
|
|
bark_decibels: 55,
|
|
|
|
tricks_known: 11,
|
2014-05-25 03:17:19 -07:00
|
|
|
name: "albert_einstein".to_string(),
|
2014-05-12 17:56:43 -07:00
|
|
|
};
|
|
|
|
let fishe = Goldfyshe {
|
|
|
|
swim_speed: 998,
|
2014-05-25 03:17:19 -07:00
|
|
|
name: "alec_guinness".to_string(),
|
2014-05-12 17:56:43 -07:00
|
|
|
};
|
2021-08-25 02:39:40 +02:00
|
|
|
let arc = Arc::new(vec![
|
|
|
|
Box::new(catte) as Box<dyn Pet+Sync+Send>,
|
|
|
|
Box::new(dogge1) as Box<dyn Pet+Sync+Send>,
|
|
|
|
Box::new(fishe) as Box<dyn Pet+Sync+Send>,
|
|
|
|
Box::new(dogge2) as Box<dyn Pet+Sync+Send>]);
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx1, rx1) = channel();
|
2013-12-03 16:44:16 -08:00
|
|
|
let arc1 = arc.clone();
|
2015-04-13 15:15:32 -07:00
|
|
|
let t1 = thread::spawn(move|| { check_legs(arc1); tx1.send(()); });
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx2, rx2) = channel();
|
2013-12-03 16:44:16 -08:00
|
|
|
let arc2 = arc.clone();
|
2015-04-13 15:15:32 -07:00
|
|
|
let t2 = thread::spawn(move|| { check_names(arc2); tx2.send(()); });
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx3, rx3) = channel();
|
2013-12-03 16:44:16 -08:00
|
|
|
let arc3 = arc.clone();
|
2015-04-13 15:15:32 -07:00
|
|
|
let t3 = thread::spawn(move|| { check_pedigree(arc3); tx3.send(()); });
|
2014-03-09 14:58:32 -07:00
|
|
|
rx1.recv();
|
|
|
|
rx2.recv();
|
|
|
|
rx3.recv();
|
2015-04-13 15:15:32 -07:00
|
|
|
t1.join();
|
|
|
|
t2.join();
|
|
|
|
t3.join();
|
2013-06-24 15:35:02 -04:00
|
|
|
}
|
|
|
|
|
2019-05-28 14:47:21 -04:00
|
|
|
fn check_legs(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>) {
|
2013-06-24 15:35:02 -04:00
|
|
|
let mut legs = 0;
|
2015-06-11 13:56:07 +01:00
|
|
|
for pet in arc.iter() {
|
2013-06-24 15:35:02 -04:00
|
|
|
legs += pet.num_legs();
|
|
|
|
}
|
|
|
|
assert!(legs == 12);
|
|
|
|
}
|
2019-05-28 14:47:21 -04:00
|
|
|
fn check_names(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>) {
|
2015-06-11 13:56:07 +01:00
|
|
|
for pet in arc.iter() {
|
2015-02-15 09:52:21 +01:00
|
|
|
pet.name(Box::new(|name| {
|
2014-06-19 18:22:33 -07:00
|
|
|
assert!(name.as_bytes()[0] == 'a' as u8 && name.as_bytes()[1] == 'l' as u8);
|
2015-02-15 09:52:21 +01:00
|
|
|
}))
|
2013-06-24 15:35:02 -04:00
|
|
|
}
|
|
|
|
}
|
2019-05-28 14:47:21 -04:00
|
|
|
fn check_pedigree(arc: Arc<Vec<Box<dyn Pet+Sync+Send>>>) {
|
2015-06-11 13:56:07 +01:00
|
|
|
for pet in arc.iter() {
|
2013-06-24 15:35:02 -04:00
|
|
|
assert!(pet.of_good_pedigree());
|
|
|
|
}
|
|
|
|
}
|