use std::future::Future;
use std::io::Result;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
use crate::AsyncOutput;
#[must_use = "futures do nothing unless polled"]
pub struct WriteFuture<'a, O>
where
O: AsyncOutput + ?Sized,
{
output: Pin<&'a mut O>,
input: &'a [O::Item],
completed: bool,
}
impl<'a, O> WriteFuture<'a, O>
where
O: AsyncOutput + ?Sized,
{
#[inline(always)]
pub const fn new(output: Pin<&'a mut O>, input: &'a [O::Item]) -> Self {
Self {
output,
input,
completed: false,
}
}
}
impl<O> Future for WriteFuture<'_, O>
where
O: AsyncOutput + ?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, "WriteFuture polled after completion");
let result = this.output.as_mut().poll_write(cx, this.input);
if result.is_ready() {
this.completed = true;
}
result
}
}