use crate::LendingStream;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Next<'a, S: ?Sized + Unpin> {
stream: &'a mut S,
done: bool,
}
impl<'a, S: ?Sized + Unpin> Next<'a, S> {
pub(crate) fn new(stream: &'a mut S) -> Self {
Self {
stream,
done: false,
}
}
}
impl<S: Unpin + ?Sized> Unpin for Next<'_, S> {}
impl<'a, S: LendingStream + Unpin + ?Sized> Future for Next<'a, S> {
type Output = Option<S::Item<'a>>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let Self {
stream,
done: is_done,
} = self.get_mut();
assert!(!*is_done, "Cannot poll future after it has been `.await`ed");
match unsafe { std::ptr::read(stream) }.poll_next(cx) {
Poll::Ready(ready) => {
*is_done = true;
Poll::Ready(ready)
}
Poll::Pending => todo!(),
}
}
}