1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/// A generator-based future.
#[must_use]
pub struct AsyncFuture<G, R, E>(G)
where
  G: Generator<Yield = (), Return = Result<R, E>>;

impl<G, R, E> AsyncFuture<G, R, E>
where
  G: Generator<Yield = (), Return = Result<R, E>>,
{
  /// Creates a new `AsyncFuture`.
  #[inline(always)]
  pub fn new(generator: G) -> Self {
    AsyncFuture(generator)
  }
}

impl<G, R, E> Future for AsyncFuture<G, R, E>
where
  G: Generator<Yield = (), Return = Result<R, E>>,
{
  type Item = R;
  type Error = E;

  #[inline(always)]
  fn poll(&mut self) -> Poll<R, E> {
    match self.0.resume() {
      Yielded(()) => Ok(Async::NotReady),
      Complete(complete) => complete.map(Async::Ready),
    }
  }
}