Created
July 23, 2017 18:28
-
-
Save x37v/7fd563a15afbdffe071e6ffd634ea19a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::rc::Rc; | |
type TimePoint = u64; | |
trait Schedule { | |
fn schedule(&mut self, t: TimePoint, f: Rc<Fn(&mut Schedule)>) -> (); | |
fn now(&self) -> TimePoint; | |
} | |
type SeqFun = Rc<Fn(&mut Schedule)>; | |
struct RuntimeSeq { | |
items: Vec<SeqFun>, | |
now: TimePoint | |
} | |
struct Seq { | |
items: Vec<SeqFun>, | |
runtime: RuntimeSeq, | |
now: TimePoint | |
} | |
impl Schedule for Seq { | |
fn schedule(&mut self, t: TimePoint, f: SeqFun) -> () { | |
self.items.push(f); | |
} | |
fn now(&self) -> TimePoint { | |
self.now | |
} | |
} | |
impl Schedule for RuntimeSeq { | |
fn schedule(&mut self, t: TimePoint, f: SeqFun) -> () { | |
self.items.push(f); | |
} | |
fn now(&self) -> TimePoint { | |
self.now | |
} | |
} | |
impl RuntimeSeq { | |
fn new() -> RuntimeSeq { | |
RuntimeSeq{items:Vec::new(), now: 0} | |
} | |
fn exec(&mut self) { | |
let mut v = self.items.clone(); | |
let mut iter = v.iter_mut(); | |
while let Some(f) = iter.next() { | |
f(self); | |
} | |
} | |
} | |
impl Seq { | |
fn new() -> Seq { | |
Seq{items:Vec::new(), runtime: RuntimeSeq::new(), now: 0} | |
} | |
fn exec(&mut self) { | |
for f in &self.items { | |
f(&mut self.runtime); | |
} | |
self.runtime.exec(); | |
} | |
} | |
fn doit(context: &mut Schedule) { | |
println!("doit: {}", context.now()); | |
} | |
fn doit2(context: &mut Schedule) { | |
println!("doit2: {}", context.now()); | |
context.schedule(2000, Rc::new(doit)); | |
} | |
fn main() { | |
let mut c = Seq::new(); | |
c.schedule(23, Rc::new(doit)); | |
c.schedule(34, Rc::new(doit2)); | |
c.schedule(10, Rc::new(|context: &mut Schedule| { | |
println!("outer dude: {}", context.now()); | |
context.schedule(43, Rc::new(|context: &mut Schedule| { | |
println!("inner dude"); | |
})); | |
})); | |
c.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment