aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Hill <aa1ronham@gmail.com>2017-05-20 21:22:57 -0400
committerJoe Wilm <jwilm@users.noreply.github.com>2017-05-24 10:56:50 -0700
commit7eff38d7b70c1aa4f44daba3f22df68007845b59 (patch)
tree3d8f161c611de425601566f55e26780b8cd0b6b8
parentbc8d86f97056478b411a7fea56fd47bc1f8fc58d (diff)
downloadalacritty-7eff38d7b70c1aa4f44daba3f22df68007845b59.tar.gz
alacritty-7eff38d7b70c1aa4f44daba3f22df68007845b59.zip
Add DrainResult enum
-rw-r--r--src/event_loop.rs55
1 files changed, 47 insertions, 8 deletions
diff --git a/src/event_loop.rs b/src/event_loop.rs
index a1fed336..ee5624c7 100644
--- a/src/event_loop.rs
+++ b/src/event_loop.rs
@@ -46,6 +46,44 @@ struct Writing {
written: usize,
}
+/// Indicates the result of draining the mio channel
+#[derive(Debug)]
+enum DrainResult {
+ /// At least one new item was received
+ ReceivedItem,
+ /// Nothing was available to receive
+ Empty,
+ /// A shutdown message was received
+ Shutdown
+}
+
+impl DrainResult {
+
+ pub fn is_shutdown(&self) -> bool {
+ match *self {
+ DrainResult::Shutdown => true,
+ _ => false
+ }
+ }
+
+ #[allow(dead_code)]
+ pub fn is_empty(&self) -> bool {
+ match *self {
+ DrainResult::Empty => true,
+ _ => false
+ }
+ }
+
+ #[allow(dead_code)]
+ pub fn is_item(&self) -> bool {
+ match *self {
+ DrainResult::ReceivedItem => true,
+ _ => false
+ }
+ }
+
+}
+
/// All of the mutable state needed to run the event loop
///
/// Contains list of items to write, current write state, etc. Anything that
@@ -172,11 +210,9 @@ impl<Io> EventLoop<Io>
// Drain the channel
//
- // Returns `Ok` if the `EventLoop` should continue running.
- // `Ok(true)`is returned if items were received
+ // Returns a `DrainResult` indicating the result of receiving from the channe;
//
- // An `Err` indicates that the event loop should shut down
- fn drain_recv_channel(&self, state: &mut State) -> Result<bool, ()> {
+ fn drain_recv_channel(&self, state: &mut State) -> DrainResult {
let mut received_item = false;
while let Ok(msg) = self.rx.try_recv() {
received_item = true;
@@ -185,18 +221,18 @@ impl<Io> EventLoop<Io>
state.write_list.push_back(input);
},
Msg::Shutdown => {
- return Err(())
+ return DrainResult::Shutdown;
}
}
}
- Ok(received_item)
+ return if received_item { DrainResult::ReceivedItem } else { DrainResult::Empty };
}
// Returns a `bool` indicating whether or not the event loop should continue running
#[inline]
fn channel_event(&mut self, state: &mut State) -> bool {
- if self.drain_recv_channel(state).is_err() {
+ if self.drain_recv_channel(state).is_shutdown() {
return false;
}
@@ -259,7 +295,10 @@ impl<Io> EventLoop<Io>
// (new items came in for writing) or `Err` (we need to shut down)
if state.writing.is_some()
|| !state.write_list.is_empty()
- || self.drain_recv_channel(state).unwrap_or_else(|_| true)
+ || match self.drain_recv_channel(state) {
+ DrainResult::Shutdown | DrainResult::ReceivedItem => true,
+ _ => false
+ }
{
break;
}