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 used to fully flush an I/O object.
12 ///
13 /// Created by the [`AsyncWriteExt::flush`][flush] function.
14 /// [flush]: crate::io::AsyncWriteExt::flush
15 #[derive(Debug)]
16 #[must_use = "futures do nothing unless you `.await` or poll them"]
17 pub struct Flush<'a, A: ?Sized> {
18 a: &'a mut A,
19 // Make this future `!Unpin` for compatibility with async trait methods.
20 #[pin]
21 _pin: PhantomPinned,
22 }
23 }
24
25 /// Creates a future which will entirely flush an I/O object.
flush<A>(a: &mut A) -> Flush<'_, A> where A: AsyncWrite + Unpin + ?Sized,26 pub(super) fn flush<A>(a: &mut A) -> Flush<'_, A>
27 where
28 A: AsyncWrite + Unpin + ?Sized,
29 {
30 Flush {
31 a,
32 _pin: PhantomPinned,
33 }
34 }
35
36 impl<A> Future for Flush<'_, A>
37 where
38 A: AsyncWrite + Unpin + ?Sized,
39 {
40 type Output = io::Result<()>;
41
poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>42 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
43 let me = self.project();
44 Pin::new(&mut *me.a).poll_flush(cx)
45 }
46 }
47