| 1 | use core::marker::PhantomData; |
| 2 | use core::pin::Pin; |
| 3 | use futures_core::future::Future; |
| 4 | use futures_core::task::{Context, Poll}; |
| 5 | use futures_sink::Sink; |
| 6 | |
| 7 | /// Future for the [`flush`](super::SinkExt::flush) method. |
| 8 | #[derive (Debug)] |
| 9 | #[must_use = "futures do nothing unless you `.await` or poll them" ] |
| 10 | pub struct Flush<'a, Si: ?Sized, Item> { |
| 11 | sink: &'a mut Si, |
| 12 | _phantom: PhantomData<fn(Item)>, |
| 13 | } |
| 14 | |
| 15 | // Pin is never projected to a field. |
| 16 | impl<Si: Unpin + ?Sized, Item> Unpin for Flush<'_, Si, Item> {} |
| 17 | |
| 18 | /// A future that completes when the sink has finished processing all |
| 19 | /// pending requests. |
| 20 | /// |
| 21 | /// The sink itself is returned after flushing is complete; this adapter is |
| 22 | /// intended to be used when you want to stop sending to the sink until |
| 23 | /// all current requests are processed. |
| 24 | impl<'a, Si: Sink<Item> + Unpin + ?Sized, Item> Flush<'a, Si, Item> { |
| 25 | pub(super) fn new(sink: &'a mut Si) -> Self { |
| 26 | Self { sink, _phantom: PhantomData } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl<Si: Sink<Item> + Unpin + ?Sized, Item> Future for Flush<'_, Si, Item> { |
| 31 | type Output = Result<(), Si::Error>; |
| 32 | |
| 33 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 34 | Pin::new(&mut self.sink).poll_flush(cx) |
| 35 | } |
| 36 | } |
| 37 | |