Skip to content

Instantly share code, notes, and snippets.

@Thiez
Forked from saosebastiao/gist:67dcf4ea695338270936
Last active August 29, 2015 14:06
Show Gist options
  • Save Thiez/a139bfaefbd44865872a to your computer and use it in GitHub Desktop.
Save Thiez/a139bfaefbd44865872a to your computer and use it in GitHub Desktop.
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(0));
println!("{} is {}" , self . name , res);
res
}
}
impl Constraint for Bar {
fn is_satisfied(&self) -> bool {
let res = self.a;
println!("{} 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 par : Vec<Future<bool>> = cs.into_iter().map(|constraint| Future::spawn(proc(){ constraint.is_satisfied() })).collect();
let result = par.into_iter().all(|future|future.unwrap());
if result {
println!("All conditions satisfied.");
} else {
println!("Not all conditions satisfied.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment