2013-08-27 18:45:13 -07:00
|
|
|
// xfail-pretty
|
|
|
|
|
2013-06-24 15:35:02 -04:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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
|
2013-06-28 16:00:30 +12:00
|
|
|
// and shared between tasks as long as all types fulfill Freeze+Send.
|
2013-06-24 15:35:02 -04:00
|
|
|
|
|
|
|
// xfail-fast
|
|
|
|
|
|
|
|
extern mod extra;
|
2013-12-03 16:44:16 -08:00
|
|
|
|
2013-06-24 15:35:02 -04:00
|
|
|
use extra::arc;
|
|
|
|
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() {
|
2013-06-24 15:35:02 -04:00
|
|
|
let catte = Catte { num_whiskers: 7, name: ~"alonzo_church" };
|
|
|
|
let dogge1 = Dogge { bark_decibels: 100, tricks_known: 42, name: ~"alan_turing" };
|
|
|
|
let dogge2 = Dogge { bark_decibels: 55, tricks_known: 11, name: ~"albert_einstein" };
|
|
|
|
let fishe = Goldfyshe { swim_speed: 998, name: ~"alec_guinness" };
|
2013-07-22 13:57:40 -07:00
|
|
|
let arc = arc::Arc::new(~[~catte as ~Pet:Freeze+Send,
|
2013-06-28 16:00:30 +12:00
|
|
|
~dogge1 as ~Pet:Freeze+Send,
|
|
|
|
~fishe as ~Pet:Freeze+Send,
|
|
|
|
~dogge2 as ~Pet:Freeze+Send]);
|
2013-12-05 18:19:06 -08:00
|
|
|
let (p1,c1) = Chan::new();
|
2013-12-03 16:44:16 -08:00
|
|
|
let arc1 = arc.clone();
|
2014-01-27 18:29:50 -05:00
|
|
|
task::spawn(proc() { check_legs(arc1); c1.send(()); });
|
2013-12-05 18:19:06 -08:00
|
|
|
let (p2,c2) = Chan::new();
|
2013-12-03 16:44:16 -08:00
|
|
|
let arc2 = arc.clone();
|
2014-01-27 18:29:50 -05:00
|
|
|
task::spawn(proc() { check_names(arc2); c2.send(()); });
|
2013-12-05 18:19:06 -08:00
|
|
|
let (p3,c3) = Chan::new();
|
2013-12-03 16:44:16 -08:00
|
|
|
let arc3 = arc.clone();
|
2014-01-27 18:29:50 -05:00
|
|
|
task::spawn(proc() { check_pedigree(arc3); c3.send(()); });
|
2013-06-24 15:35:02 -04:00
|
|
|
p1.recv();
|
|
|
|
p2.recv();
|
|
|
|
p3.recv();
|
|
|
|
}
|
|
|
|
|
2013-07-22 13:57:40 -07:00
|
|
|
fn check_legs(arc: arc::Arc<~[~Pet:Freeze+Send]>) {
|
2013-06-24 15:35:02 -04:00
|
|
|
let mut legs = 0;
|
2013-08-03 12:45:23 -04:00
|
|
|
for pet in arc.get().iter() {
|
2013-06-24 15:35:02 -04:00
|
|
|
legs += pet.num_legs();
|
|
|
|
}
|
|
|
|
assert!(legs == 12);
|
|
|
|
}
|
2013-07-22 13:57:40 -07:00
|
|
|
fn check_names(arc: arc::Arc<~[~Pet:Freeze+Send]>) {
|
2013-08-03 12:45:23 -04:00
|
|
|
for pet in arc.get().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
|
|
|
}
|
|
|
|
}
|
2013-07-22 13:57:40 -07:00
|
|
|
fn check_pedigree(arc: arc::Arc<~[~Pet:Freeze+Send]>) {
|
2013-08-03 12:45:23 -04:00
|
|
|
for pet in arc.get().iter() {
|
2013-06-24 15:35:02 -04:00
|
|
|
assert!(pet.of_good_pedigree());
|
|
|
|
}
|
|
|
|
}
|