use std::future::Future;
use std::io::Result;
use std::pin::Pin;
use std::task::{
Context,
Poll,
};
use crate::AsyncInput;
#[must_use = "futures do nothing unless polled"]
pub struct ReadFuture<'a, I>
where
I: AsyncInput + ?Sized,
{
input: Pin<&'a mut I>,
output: &'a mut [I::Item],
completed: bool,
}
impl<'a, I> ReadFuture<'a, I>
where
I: AsyncInput + ?Sized,
{
#[inline(always)]
pub const fn new(input: Pin<&'a mut I>, output: &'a mut [I::Item]) -> Self {
Self {
input,
output,
completed: false,
}
}
}
impl<I> Future for ReadFuture<'_, I>
where
I: AsyncInput + ?Sized,
{
type Output = Result<usize>;
#[inline]
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
assert!(!this.completed, "ReadFuture polled after completion");
let result = this.input.as_mut().poll_read(cx, this.output);
if result.is_ready() {
this.completed = true;
}
result
}
}