2014-02-07 20:08:32 +01:00
|
|
|
// ignore-pretty
|
2013-08-27 18:45:13 -07:00
|
|
|
|
2014-02-07 20:08:32 +01:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-06-24 15:35:02 -04:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-07-22 13:57:40 -07:00
|
|
|
// Tests that a heterogeneous list of existential types can be put inside an Arc
|
2014-03-22 00:44:26 +01:00
|
|
|
// and shared between tasks as long as all types fulfill Send.
|
2013-06-24 15:35:02 -04:00
|
|
|
|
|
|
|
|
2014-02-14 10:10:06 -08:00
|
|
|
extern crate sync;
|
2013-12-03 16:44:16 -08:00
|
|
|
|
2014-01-30 15:04:47 -05:00
|
|
|
use sync::Arc;
|
2013-06-24 15:35:02 -04:00
|
|
|
use std::task;
|
|
|
|
|
|
|
|
trait Pet {
|
2013-11-19 16:34:19 -08:00
|
|
|
fn name(&self, blk: |&str|);
|
2013-06-24 15:35:02 -04:00
|
|
|
fn num_legs(&self) -> uint;
|
|
|
|
fn of_good_pedigree(&self) -> bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Catte {
|
|
|
|
num_whiskers: uint,
|
|
|
|
name: ~str,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Dogge {
|
|
|
|
bark_decibels: uint,
|
|
|
|
tricks_known: uint,
|
|
|
|
name: ~str,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Goldfyshe {
|
|
|
|
swim_speed: uint,
|
|
|
|
name: ~str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Pet for Catte {
|
2013-11-19 16:34:19 -08:00
|
|
|
fn name(&self, blk: |&str|) { blk(self.name) }
|
2013-06-24 15:35:02 -04:00
|
|
|
fn num_legs(&self) -> uint { 4 }
|
|
|
|
fn of_good_pedigree(&self) -> bool { self.num_whiskers >= 4 }
|
|
|
|
}
|
|
|
|
impl Pet for Dogge {
|
2013-11-19 16:34:19 -08:00
|
|
|
fn name(&self, blk: |&str|) { blk(self.name) }
|
2013-06-24 15:35:02 -04:00
|
|
|
fn num_legs(&self) -> uint { 4 }
|
|
|
|
fn of_good_pedigree(&self) -> bool {
|
|
|
|
self.bark_decibels < 70 || self.tricks_known > 20
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Pet for Goldfyshe {
|
2013-11-19 16:34:19 -08:00
|
|
|
fn name(&self, blk: |&str|) { blk(self.name) }
|
2013-06-24 15:35:02 -04:00
|
|
|
fn num_legs(&self) -> uint { 0 }
|
|
|
|
fn of_good_pedigree(&self) -> bool { self.swim_speed >= 500 }
|
|
|
|
}
|
|
|
|
|
2014-01-03 15:30:54 -08:00
|
|
|
pub fn main() {
|
2014-04-15 18:17:48 -07:00
|
|
|
let catte = Catte { num_whiskers: 7, name: "alonzo_church".to_owned() };
|
|
|
|
let dogge1 = Dogge { bark_decibels: 100, tricks_known: 42, name: "alan_turing".to_owned() };
|
|
|
|
let dogge2 = Dogge { bark_decibels: 55, tricks_known: 11, name: "albert_einstein".to_owned() };
|
|
|
|
let fishe = Goldfyshe { swim_speed: 998, name: "alec_guinness".to_owned() };
|
2014-03-05 14:02:44 -08:00
|
|
|
let arc = Arc::new(vec!(~catte as ~Pet:Share+Send,
|
2014-03-07 18:57:35 +01:00
|
|
|
~dogge1 as ~Pet:Share+Send,
|
|
|
|
~fishe as ~Pet:Share+Send,
|
2014-03-05 14:02:44 -08:00
|
|
|
~dogge2 as ~Pet:Share+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();
|
2014-03-09 14:58:32 -07:00
|
|
|
task::spawn(proc() { check_legs(arc1); tx1.send(()); });
|
|
|
|
let (tx2, rx2) = channel();
|
2013-12-03 16:44:16 -08:00
|
|
|
let arc2 = arc.clone();
|
2014-03-09 14:58:32 -07:00
|
|
|
task::spawn(proc() { check_names(arc2); tx2.send(()); });
|
|
|
|
let (tx3, rx3) = channel();
|
2013-12-03 16:44:16 -08:00
|
|
|
let arc3 = arc.clone();
|
2014-03-09 14:58:32 -07:00
|
|
|
task::spawn(proc() { check_pedigree(arc3); tx3.send(()); });
|
|
|
|
rx1.recv();
|
|
|
|
rx2.recv();
|
|
|
|
rx3.recv();
|
2013-06-24 15:35:02 -04:00
|
|
|
}
|
|
|
|
|
2014-03-05 14:02:44 -08:00
|
|
|
fn check_legs(arc: Arc<Vec<~Pet:Share+Send>>) {
|
2013-06-24 15:35:02 -04:00
|
|
|
let mut legs = 0;
|
2014-03-22 00:55:50 -07:00
|
|
|
for pet in arc.iter() {
|
2013-06-24 15:35:02 -04:00
|
|
|
legs += pet.num_legs();
|
|
|
|
}
|
|
|
|
assert!(legs == 12);
|
|
|
|
}
|
2014-03-05 15:28:08 -08:00
|
|
|
fn check_names(arc: Arc<Vec<~Pet:Share+Send>>) {
|
2014-03-22 00:55:50 -07:00
|
|
|
for pet in arc.iter() {
|
2013-11-21 17:23:21 -08:00
|
|
|
pet.name(|name| {
|
2013-06-24 15:35:02 -04:00
|
|
|
assert!(name[0] == 'a' as u8 && name[1] == 'l' as u8);
|
2013-11-21 17:23:21 -08:00
|
|
|
})
|
2013-06-24 15:35:02 -04:00
|
|
|
}
|
|
|
|
}
|
2014-03-05 15:28:08 -08:00
|
|
|
fn check_pedigree(arc: Arc<Vec<~Pet:Share+Send>>) {
|
2014-03-22 00:55:50 -07:00
|
|
|
for pet in arc.iter() {
|
2013-06-24 15:35:02 -04:00
|
|
|
assert!(pet.of_good_pedigree());
|
|
|
|
}
|
|
|
|
}
|