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