| 1 | use std::mem; |
| 2 | use std::pin::Pin; |
| 3 | use std::future::Future; |
| 4 | |
| 5 | use crate::io::{self, Write}; |
| 6 | use crate::task::{Context, Poll}; |
| 7 | |
| 8 | #[doc (hidden)] |
| 9 | #[allow (missing_debug_implementations)] |
| 10 | pub struct WriteAllFuture<'a, T: Unpin + ?Sized> { |
| 11 | pub(crate) writer: &'a mut T, |
| 12 | pub(crate) buf: &'a [u8], |
| 13 | } |
| 14 | |
| 15 | impl<T: Write + Unpin + ?Sized> Future for WriteAllFuture<'_, T> { |
| 16 | type Output = io::Result<()>; |
| 17 | |
| 18 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 19 | let Self { writer: &mut &mut T, buf: &mut &[u8] } = &mut *self; |
| 20 | |
| 21 | while !buf.is_empty() { |
| 22 | let n: usize = futures_core::ready!(Pin::new(&mut **writer).poll_write(cx, buf))?; |
| 23 | let (_, rest: &[u8]) = mem::replace(buf, &[]).split_at(mid:n); |
| 24 | *buf = rest; |
| 25 | |
| 26 | if n == 0 { |
| 27 | return Poll::Ready(Err(io::ErrorKind::WriteZero.into())); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | Poll::Ready(Ok(())) |
| 32 | } |
| 33 | } |
| 34 | |