use std::{
pin::Pin,
task::{Context, Poll},
};
use futures_core::Stream;
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
pub enum GeneratorState<Y, R> {
Yielded(Y),
Complete(R),
}
pub trait AsyncGenerator {
type Yield;
type Return;
fn poll_resume(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<GeneratorState<Self::Yield, Self::Return>>;
}
impl<S: Stream> AsyncGenerator for S {
type Yield = S::Item;
type Return = ();
#[inline]
fn poll_resume(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<GeneratorState<Self::Yield, Self::Return>> {
self.poll_next(cx).map(|val| match val {
Some(val) => GeneratorState::Yielded(val),
None => GeneratorState::Complete(()),
})
}
}