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 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.
26pub(super) fn flush<A>(a: &mut A) -> Flush<'_, A>
27where
28 A: AsyncWrite + Unpin + ?Sized,
29{
30 Flush {
31 a,
32 _pin: PhantomPinned,
33 }
34}
35
36impl<A> Future for Flush<'_, A>
37where
38 A: AsyncWrite + Unpin + ?Sized,
39{
40 type Output = io::Result<()>;
41
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