Add DrainResult enum

This commit is contained in:
Aaron Hill 2017-05-20 21:22:57 -04:00 committed by Joe Wilm
parent bc8d86f970
commit 7eff38d7b7
1 changed files with 47 additions and 8 deletions

View File

@ -46,6 +46,44 @@ struct Writing {
written: usize, 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 /// All of the mutable state needed to run the event loop
/// ///
/// Contains list of items to write, current write state, etc. Anything that /// Contains list of items to write, current write state, etc. Anything that
@ -172,11 +210,9 @@ impl<Io> EventLoop<Io>
// Drain the channel // Drain the channel
// //
// Returns `Ok` if the `EventLoop` should continue running. // Returns a `DrainResult` indicating the result of receiving from the channe;
// `Ok(true)`is returned if items were received
// //
// An `Err` indicates that the event loop should shut down fn drain_recv_channel(&self, state: &mut State) -> DrainResult {
fn drain_recv_channel(&self, state: &mut State) -> Result<bool, ()> {
let mut received_item = false; let mut received_item = false;
while let Ok(msg) = self.rx.try_recv() { while let Ok(msg) = self.rx.try_recv() {
received_item = true; received_item = true;
@ -185,18 +221,18 @@ impl<Io> EventLoop<Io>
state.write_list.push_back(input); state.write_list.push_back(input);
}, },
Msg::Shutdown => { 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 // Returns a `bool` indicating whether or not the event loop should continue running
#[inline] #[inline]
fn channel_event(&mut self, state: &mut State) -> bool { 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; return false;
} }
@ -259,7 +295,10 @@ impl<Io> EventLoop<Io>
// (new items came in for writing) or `Err` (we need to shut down) // (new items came in for writing) or `Err` (we need to shut down)
if state.writing.is_some() if state.writing.is_some()
|| !state.write_list.is_empty() || !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; break;
} }