tokio/io/util/
flush.rs

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    ///
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.
27pub(super) fn flush<A>(a: &mut A) -> Flush<'_, A>
28where
29    A: AsyncWrite + Unpin + ?Sized,
30{
31    Flush {
32        a,
33        _pin: PhantomPinned,
34    }
35}
36
37impl<A> Future for Flush<'_, A>
38where
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 = self.project();
45        Pin::new(&mut *me.a).poll_flush(cx)
46    }
47}