1use crate::io::AsyncWrite;
2
3use pin_project_lite::pin_project;
4use std::future::Future;
5use std::io;
6use std::marker::PhantomPinned;
7use std::pin::Pin;
8use std::task::{Context, Poll};
9
10pin_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.
25pub(crate) fn write<'a, W>(writer: &'a mut W, buf: &'a [u8]) -> Write<'a, W>
26where
27 W: AsyncWrite + Unpin + ?Sized,
28{
29 Write {
30 writer,
31 buf,
32 _pin: PhantomPinned,
33 }
34}
35
36impl<W> Future for Write<'_, W>
37where
38 W: AsyncWrite + Unpin + ?Sized,
39{
40 type Output = io::Result<usize>;
41
42 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
43 let me: Projection<'_, '_, W> = self.project();
44 Pin::new(&mut *me.writer).poll_write(cx, me.buf)
45 }
46}
47