1 use crate::io::AsyncWrite;
2 
3 use bytes::Buf;
4 use std::future::Future;
5 use std::io;
6 use std::pin::Pin;
7 use std::task::{Context, Poll};
8 
9 cfg_io_util! {
10     /// A future to write some of the buffer to an `AsyncWrite`.
11     #[derive(Debug)]
12     #[must_use = "futures do nothing unless you `.await` or poll them"]
13     pub struct WriteBuf<'a, W, B> {
14         writer: &'a mut W,
15         buf: &'a mut B,
16     }
17 }
18 
19 /// Tries to write some bytes from the given `buf` to the writer in an
20 /// asynchronous manner, returning a future.
write_buf<'a, W, B>(writer: &'a mut W, buf: &'a mut B) -> WriteBuf<'a, W, B> where W: AsyncWrite + Unpin, B: Buf,21 pub(crate) fn write_buf<'a, W, B>(writer: &'a mut W, buf: &'a mut B) -> WriteBuf<'a, W, B>
22 where
23     W: AsyncWrite + Unpin,
24     B: Buf,
25 {
26     WriteBuf { writer, buf }
27 }
28 
29 impl<W, B> Future for WriteBuf<'_, W, B>
30 where
31     W: AsyncWrite + Unpin,
32     B: Buf,
33 {
34     type Output = io::Result<usize>;
35 
poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>>36     fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
37         let me = &mut *self;
38         Pin::new(&mut *me.writer).poll_write_buf(cx, me.buf)
39     }
40 }
41