async_hal/io/
write_all.rs

1use crate::io::AsyncWrite;
2use core::{
3    marker::PhantomPinned,
4    mem,
5    pin::Pin,
6    task::{Context, Poll},
7};
8use futures::{ready, Future};
9use pin_project_lite::pin_project;
10
11pin_project! {
12    #[derive(Debug)]
13    #[must_use = "futures do nothing unless you `.await` or poll them"]
14    pub struct WriteAll<'a, W: ?Sized> {
15        writer: &'a mut W,
16        buf: &'a [u8],
17        // Make this future `!Unpin` for compatibility with async trait methods.
18        #[pin]
19        _pin: PhantomPinned,
20    }
21}
22
23pub(crate) fn write_all<'a, W>(writer: &'a mut W, buf: &'a [u8]) -> WriteAll<'a, W>
24where
25    W: AsyncWrite + Unpin + ?Sized,
26{
27    WriteAll {
28        writer,
29        buf,
30        _pin: PhantomPinned,
31    }
32}
33
34impl<W> Future for WriteAll<'_, W>
35where
36    W: AsyncWrite + Unpin + ?Sized,
37{
38    type Output = Result<(), W::Error>;
39
40    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
41        let me = self.project();
42        while !me.buf.is_empty() {
43            let n = ready!(Pin::new(&mut *me.writer).poll_write(cx, me.buf))?;
44            {
45                let (_, rest) = mem::take(&mut *me.buf).split_at(n);
46                *me.buf = rest;
47            }
48            if n == 0 {
49                todo!()
50            }
51        }
52
53        Poll::Ready(Ok(()))
54    }
55}