1 use crate::io::AsyncWrite;
2 
3 use pin_project_lite::pin_project;
4 use std::future::Future;
5 use std::io;
6 use std::marker::PhantomPinned;
7 use std::pin::Pin;
8 use std::task::{Context, Poll};
9 
10 pin_project! {
11     /// A future to write some of the buffer to an `AsyncWrite`.
12     #[derive(Debug)]
13     #[must_use = "futures do nothing unless you `.await` or poll them"]
14     pub struct Write<'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 
23 /// Tries to write some bytes from the given `buf` to the writer in an
24 /// asynchronous manner, returning a future.
write<'a, W>(writer: &'a mut W, buf: &'a [u8]) -> Write<'a, W> where W: AsyncWrite + Unpin + ?Sized,25 pub(crate) fn write<'a, W>(writer: &'a mut W, buf: &'a [u8]) -> Write<'a, W>
26 where
27     W: AsyncWrite + Unpin + ?Sized,
28 {
29     Write {
30         writer,
31         buf,
32         _pin: PhantomPinned,
33     }
34 }
35 
36 impl<W> Future for Write<'_, W>
37 where
38     W: AsyncWrite + Unpin + ?Sized,
39 {
40     type Output = io::Result<usize>;
41 
poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>>42     fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
43         let me = self.project();
44         Pin::new(&mut *me.writer).poll_write(cx, me.buf)
45     }
46 }
47