use core::{
cell::Cell,
fmt,
pin::Pin,
task::{Context, Poll},
};
use crate::EventIterator;
pub struct Ready<F>(Cell<Option<F>>);
impl<F> fmt::Debug for Ready<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Ready").field(&format_args!("_")).finish()
}
}
impl<F, E> EventIterator for Ready<F>
where
F: FnMut() -> E + Unpin,
{
type Event<'me> = E where Self: 'me;
fn poll_next<'a>(
self: Pin<&'a Self>,
_cx: &mut Context<'_>,
) -> Poll<Option<Self::Event<'a>>> {
self.0
.take()
.map(|mut f| {
let poll = f();
self.0.set(Some(f));
Poll::Ready(Some(poll))
})
.unwrap()
}
}
#[doc = include_str!("../examples/ready.rs")]
pub fn ready<E, F>(f: F) -> Ready<F>
where
F: FnMut() -> E,
{
Ready(Cell::new(Some(f)))
}