diff options
Diffstat (limited to 'src/component_future.rs')
-rw-r--r-- | src/component_future.rs | 61 |
1 files changed, 33 insertions, 28 deletions
diff --git a/src/component_future.rs b/src/component_future.rs index 2bc9ddc..4613b71 100644 --- a/src/component_future.rs +++ b/src/component_future.rs @@ -1,27 +1,34 @@ -pub enum Poll<T> { - // something happened that we want to report - Event(T), - // underlying future/stream returned NotReady, so it's safe for us to also - // return NotReady +pub type Poll<Item, Error> = Result<Async<Item>, Error>; + +pub enum Async<Item> { + // we have a value for the main loop to return immediately. + Ready(Item), + + // one of our inner futures returned futures::Async::NotReady. if all + // of our other components return either NothingToDo or NotReady, then our + // overall future should return NotReady and wait to be polled again. NotReady, - // didn't do any work, so we want to return NotReady assuming at least one - // other poll method returned NotReady (if every poll method returns - // NothingToDo, something is broken) - NothingToDo, - // did some work, so we want to loop + + // we did some work (moved our internal state closer to being ready to + // return a value), but we aren't ready to return a value yet. we should + // re-run all of the poll functions to see if the state modification made + // any of them also able to make progress. DidWork, - // the stream has ended - Done, + + // we didn't poll any inner futures or otherwise change our internal state + // at all, so rerunning is unlikely to make progress. if all components + // return either NothingToDo or NotReady (and at least one returned + // NotReady), then we should just return NotReady and wait to be polled + // again. it is an error (panic) for all component poll methods to return + // NothingToDo. + NothingToDo, } pub fn poll_future<T, Item, Error>( future: &mut T, poll_fns: &'static [&'static dyn for<'a> Fn( &'a mut T, - ) -> Result< - Poll<Item>, - Error, - >], + ) -> Poll<Item, Error>], ) -> futures::Poll<Item, Error> where T: futures::future::Future<Item = Item, Error = Error>, @@ -32,11 +39,10 @@ where for f in poll_fns { match f(future)? { - Poll::Event(e) => return Ok(futures::Async::Ready(e)), - Poll::NotReady => not_ready = true, - Poll::NothingToDo => {} - Poll::DidWork => did_work = true, - Poll::Done => unreachable!(), + Async::Ready(e) => return Ok(futures::Async::Ready(e)), + Async::NotReady => not_ready = true, + Async::NothingToDo => {} + Async::DidWork => did_work = true, } } @@ -54,8 +60,8 @@ pub fn poll_stream<T, Item, Error>( stream: &mut T, poll_fns: &'static [&'static dyn for<'a> Fn( &'a mut T, - ) -> Result< - Poll<Item>, + ) -> Poll< + Option<Item>, Error, >], ) -> futures::Poll<Option<Item>, Error> @@ -68,11 +74,10 @@ where for f in poll_fns { match f(stream)? { - Poll::Event(e) => return Ok(futures::Async::Ready(Some(e))), - Poll::NotReady => not_ready = true, - Poll::NothingToDo => {} - Poll::DidWork => did_work = true, - Poll::Done => return Ok(futures::Async::Ready(None)), + Async::Ready(e) => return Ok(futures::Async::Ready(e)), + Async::NotReady => not_ready = true, + Async::NothingToDo => {} + Async::DidWork => did_work = true, } } |