Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Allow polling stream again after returning a final error
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo157 committed Oct 5, 2017
1 parent c1a7a64 commit 44fd004
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ pub mod __rt {
struct GenFuture<T>(T);

/// Small shim to translate from a generator to a stream.
struct GenStream<T>(T);
struct GenStream<T> {
gen: T,
done: bool,
}

pub fn gen<T>(gen: T) -> impl MyFuture<T::Return>
where T: Generator<Yield = Async<!>>,
Expand All @@ -118,7 +121,7 @@ pub mod __rt {
Err = <<T::Yield as IsAsync>::Item as IsResult>::Err
>,
{
GenStream(gen)
GenStream { gen, done: false }
}

impl<T> Future for GenFuture<T>
Expand Down Expand Up @@ -151,15 +154,18 @@ pub mod __rt {
type Error = <<T::Yield as IsAsync>::Item as IsResult>::Err;

fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
match self.0.resume() {
if self.done { return Ok(Async::Ready(None)) }
match self.gen.resume() {
GeneratorState::Yielded(e) => match e.into_async() {
Async::NotReady
=> Ok(Async::NotReady),
Async::Ready(e)
=> e.into_result().map(|e| Async::Ready(Some(e))),
},
GeneratorState::Complete(e)
=> e.into_result().map(|()| Async::Ready(None)),
GeneratorState::Complete(e) => {
self.done = true;
e.into_result().map(|()| Async::Ready(None))
}
}
}
}
Expand Down

0 comments on commit 44fd004

Please sign in to comment.