Commit 0aa78c01 authored by Ralf's avatar Ralf
Browse files

cargo fmt

parent 834190fc
......@@ -101,8 +101,13 @@ macro_rules! handle_events_timeout {
impl Environment {
pub fn new() -> (Environment, EventSender) {
let (tx, rx) = channel();
( Environment { events: rx, sender: tx.clone(), sensors: SensorStates::new(), cur_id: 0 },
EventSender { sender: tx } )
(Environment {
events: rx,
sender: tx.clone(),
sensors: SensorStates::new(),
cur_id: 0,
},
EventSender { sender: tx })
}
pub fn handle_events(&mut self, handlers: Vec<EventHandler>) -> EventsResult {
......@@ -113,11 +118,13 @@ impl Environment {
pub fn handle_events_timeout(&mut self,
d: Duration,
handlers: Vec<EventHandler>)
-> EventsResult
{
-> EventsResult {
let sender = self.sender.clone();
// make sure we ignore old timeouts
let id = { self.cur_id = u32::wrapping_add(self.cur_id, 1); self.cur_id };
let id = {
self.cur_id = u32::wrapping_add(self.cur_id, 1);
self.cur_id
};
// spawn a thread that sends a timeout event with this id in [d] time
thread::spawn(move || {
thread::sleep(d);
......@@ -128,8 +135,7 @@ impl Environment {
self.handle_events_cur_id(handlers)
}
fn handle_events_cur_id(&mut self, mut handlers: Vec<EventHandler>) -> EventsResult
{
fn handle_events_cur_id(&mut self, mut handlers: Vec<EventHandler>) -> EventsResult {
for ev in self.events.iter() {
match ev {
EventOrTimer::Timeout(id) => {
......@@ -137,7 +143,7 @@ impl Environment {
return EventsResult::Timeout;
}
// old timeout event, ignore
},
}
EventOrTimer::Ev(ev) => {
// a regular event
self.sensors.record_state(&ev);
......
......@@ -19,7 +19,10 @@ impl UnLocking {
}
impl Situation for UnLocking {
fn run(&mut self, mut actors: Actors, env: &mut Environment) -> (Actors, Option<Box<Situation>>) {
fn run(&mut self,
mut actors: Actors,
env: &mut Environment)
-> (Actors, Option<Box<Situation>>) {
// TODO: abort if already locked
let locking = self.locking;
for _ in 0..3 {
......@@ -46,7 +49,8 @@ impl Situation for UnLocking {
}));
actors.lock = locker.terminate();
if r != EventsResult::Timeout {
assert!(r == EventsResult::ShuttingDown || env.sensors().door_locked() == Some(locking));
assert!(r == EventsResult::ShuttingDown ||
env.sensors().door_locked() == Some(locking));
break; // the retries
}
}
......@@ -64,7 +68,10 @@ impl SpaceOpen {
}
impl Situation for SpaceOpen {
fn run(&mut self, mut actors: Actors, env: &mut Environment) -> (Actors, Option<Box<Situation>>) {
fn run(&mut self,
mut actors: Actors,
env: &mut Environment)
-> (Actors, Option<Box<Situation>>) {
let mut buzzer = Wakeable::new(actors.buzz, move |t, mut buzz_actor, d| {
buzz_actor.act(true);
t.sleep(d);
......@@ -79,7 +86,11 @@ impl Situation for SpaceOpen {
}));
actors.buzz = buzzer.terminate();
// hand over to next state
(actors, if r == EventsResult::ShuttingDown { None }
else { None /* FIXME */ })
(actors,
if r == EventsResult::ShuttingDown {
None
} else {
None /* FIXME */
})
}
}
......@@ -26,7 +26,7 @@ pub struct Wakeable<T, F> {
/// a way to talk to it (the ThreadState).
enum WakeableState<T> {
Lazy(T),
Running(thread::JoinHandle<T>, Arc<ThreadState>)
Running(thread::JoinHandle<T>, Arc<ThreadState>),
}
impl<T, F> Wakeable<T, F> {
......@@ -95,7 +95,7 @@ impl<T> WakeableState<T> {
WakeableState::Running(handle, state) => {
state.wakeup();
handle.join().unwrap() // panics only if the thread paniced
},
}
}
}
}
......@@ -162,6 +162,6 @@ impl ThreadState {
fn should_terminate(&self) -> bool {
// We panic if the lock is poisened, which only happens if someone paniced
let should_run = self.mutex.lock().unwrap();
return *should_run == ShouldThreadRun::No
return *should_run == ShouldThreadRun::No;
}
}
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