summaryrefslogtreecommitdiff
path: root/src/eventloop.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/eventloop.rs')
-rw-r--r--src/eventloop.rs18
1 files changed, 9 insertions, 9 deletions
diff --git a/src/eventloop.rs b/src/eventloop.rs
index 93343b1..befd104 100644
--- a/src/eventloop.rs
+++ b/src/eventloop.rs
@@ -67,7 +67,6 @@ const TIMER_TOKEN: Token = Token(std::usize::MAX-1);
pub trait Machine {
- fn init(&mut self, &mut Context);
fn handle(&mut self, &mut Context, mio::Event);
fn timeout(&mut self, &mut Context, TToken);
}
@@ -88,7 +87,7 @@ pub struct EventLoop {
timer: Timer<TToken>,
timers: Slab<Option<(MToken,Timeout)>, TToken>,
// A machine entry is set to None during a method call on the Machine object.
- machines: Slab<Option<Box<Machine + Sync>>, MToken>,
+ machines: Slab<Option<Box<Machine>>, MToken>,
}
pub struct Context<'a> {
@@ -158,8 +157,8 @@ impl<'a> Context<'a> {
self.removed
}
- pub fn spawn(&mut self, machine: Box<Machine + Sync>) {
- self.parent.spawn(machine);
+ pub fn spawn<F>(&mut self, f: F) where F: Send + 'static + FnOnce(&mut Context) -> Box<Machine> {
+ self.parent.spawn(f);
}
}
@@ -189,20 +188,21 @@ impl EventLoop {
}
}
- pub fn spawn(&mut self, mut machine: Box<Machine + Sync>) {
+ pub fn spawn<F>(&mut self, f: F) where F: Send + 'static + FnOnce(&mut Context) -> Box<Machine> {
let mtoken = slab_insert(&mut self.machines, None);
trace!("[{}] Spawning machine", mtoken.0);
- {
+ let machine = {
let mut ctx = Context{ parent: self, machine: mtoken, removed: false };
- machine.init(&mut ctx);
+ let m = f(&mut ctx);
assert!(!ctx.removed);
- }
+ m
+ };
self.machines[mtoken] = Some(machine);
}
// XXX: I don't get why "&mut Machine" doesn't work in the FnOnce.
fn dispatch<F>(&mut self, mtoken: MToken, f: F)
- where F: FnOnce(&mut Box<Machine + Sync>, &mut Context) {
+ where F: FnOnce(&mut Box<Machine>, &mut Context) {
let mut machine = match self.machines.entry(mtoken) {
None => { return; },
Some(mut x) => { x.replace(None).unwrap() },