Skip to content

Instantly share code, notes, and snippets.

@saosebastiao
Created September 24, 2014 22:38
Show Gist options
  • Save saosebastiao/e31e96917c386a055c74 to your computer and use it in GitHub Desktop.
Save saosebastiao/e31e96917c386a055c74 to your computer and use it in GitHub Desktop.
tmp
use std::io::timer;
use std::time::Duration;
use std::sync::Future;
trait Constraint {
fn is_satisfied(&self) -> bool;
}
#[deriving(Send)]
struct Foo {
name: &'static str,
a: bool,
b: bool,
}
#[deriving(Send)]
struct Bar {
name: &'static str,
a: bool,
}
impl Constraint for Foo {
fn is_satisfied(&self) -> bool {
let res = self.a && self.b;
timer::sleep(Duration::seconds(1));
println!("Foo {} is {}" , self . name , res);
res
}
}
impl Constraint for Bar {
fn is_satisfied(&self) -> bool {
let res = self.a;
println!("Bar {} is {}" , self . name , res);
res
}
}
fn main() {
let mut cs: Vec<Box<Constraint+Send>> = Vec::with_capacity(10);
cs.push(box Foo{name: "c1", a: true, b: true,} as Box<Constraint+Send>);
cs.push(box Foo{name: "c2", a: true, b: true,} as Box<Constraint+Send>);
cs.push(box Foo{name: "c3", a: true, b: true,} as Box<Constraint+Send>);
cs.push(box Bar{name: "c4", a: true,} as Box<Constraint+Send>);
cs.push(box Bar{name: "c5", a: false,} as Box<Constraint+Send>);
cs.push(box Foo{name: "c6", a: true, b: true,} as Box<Constraint+Send>);
cs.push(box Foo{name: "c7", a: true, b: true,} as Box<Constraint+Send>);
cs.push(box Bar{name: "c8", a: true,} as Box<Constraint+Send>);
cs.push(box Bar{name: "c9", a: true,} as Box<Constraint+Send>);
cs.push(box Foo{name: "c10", a: true, b: true,} as Box<Constraint+Send>);
let mut results : Vec<Receiver<bool>> = Vec::with_capacity(10);
for c in cs.iter() {
let (tx,rx) = channel();
spawn(proc(){
let res = c.is_satisfied();
tx.send(res);
});
results.push(rx);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment