Commit d65eb353 authored by Ralf's avatar Ralf
Browse files

turn State into a trait

parent a72d1fc0
......@@ -23,49 +23,46 @@ struct Actors {
lock_actor: Box<Actor<LockAction> + Send>,
}
struct State {
run: Box<FnOnce(Actors, &mut Environment) -> (Actors, Option<State>) + 'static>,
trait State {
fn run(&mut self, Actors, &mut Environment) -> (Actors, Option<Box<State>>);
}
fn user_command_reject_handler(ev: Event) {}
impl State {
fn new<F>(f: F) -> State
where F: FnOnce(Actors, &mut Environment) -> (Actors, Option<State>) + 'static
{
State { run: Box::new(f) }
}
struct UnLockingState {
locking: bool,
}
fn new_locking() -> Self {
Self::new(|mut actors, env| {
// TODO: abort if already locked
let mut lock_actor = actors.lock_actor; // move out
for i in 0..3 {
let locker = Wakeable::new(move |t| {
lock_actor.act(LockAction::PressLock);
t.sleep(Duration::from_millis(100));
lock_actor.act(LockAction::Release);
lock_actor
});
let r = env.handle_events(Duration::from_secs(1),
vec![
EventHandler::new_safe(user_command_reject_handler),
EventHandler::new(|ev| {
match ev {
Event::DoorLocked(true) => HandlerResult::QuitLoop,
_ => HandlerResult::Continue,
}
}),
]);
// take back lock_actor to feel complete again
lock_actor = locker.terminate();
if r != EventsResult::Timeout {
assert!(r == EventsResult::ShuttingDown || env.door_locked());
break;
}
impl State for UnLockingState {
fn run(&mut self, mut actors: Actors, env: &mut Environment) -> (Actors, Option<Box<State>>) {
// TODO: abort if already locked
let mut lock_actor = actors.lock_actor; // move out
let locking = self.locking;
for i in 0..3 {
let locker = Wakeable::new(move |t| {
lock_actor.act(if locking { LockAction::PressLock } else { LockAction::PressUnlock } );
t.sleep(Duration::from_millis(100));
lock_actor.act(LockAction::Release);
lock_actor
});
let r = env.handle_events(Duration::from_secs(1),
vec![
EventHandler::new_safe(user_command_reject_handler),
EventHandler::new(|ev| {
match ev {
Event::DoorLocked(b) if b == locking => HandlerResult::QuitLoop,
_ => HandlerResult::Continue,
}
}),
]);
// take back lock_actor to feel complete again
lock_actor = locker.terminate();
if r != EventsResult::Timeout {
assert!(r == EventsResult::ShuttingDown || env.door_locked() == locking);
break;
}
actors.lock_actor = lock_actor; // move back
(actors, None)
})
}
actors.lock_actor = lock_actor; // move back
(actors, None)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment