| 1 | use super::assert_sink; |
| 2 | use crate::never::Never; |
| 3 | use core::marker::PhantomData; |
| 4 | use core::pin::Pin; |
| 5 | use futures_core::task::{Context, Poll}; |
| 6 | use futures_sink::Sink; |
| 7 | |
| 8 | /// Sink for the [`drain`] function. |
| 9 | #[derive (Debug)] |
| 10 | #[must_use = "sinks do nothing unless polled" ] |
| 11 | pub struct Drain<T> { |
| 12 | marker: PhantomData<T>, |
| 13 | } |
| 14 | |
| 15 | /// Create a sink that will just discard all items given to it. |
| 16 | /// |
| 17 | /// Similar to [`io::Sink`](::std::io::Sink). |
| 18 | /// |
| 19 | /// # Examples |
| 20 | /// |
| 21 | /// ``` |
| 22 | /// # futures::executor::block_on(async { |
| 23 | /// use futures::sink::{self, SinkExt}; |
| 24 | /// |
| 25 | /// let mut drain = sink::drain(); |
| 26 | /// drain.send(5).await?; |
| 27 | /// # Ok::<(), futures::never::Never>(()) }).unwrap(); |
| 28 | /// ``` |
| 29 | pub fn drain<T>() -> Drain<T> { |
| 30 | assert_sink::<T, Never, _>(Drain { marker: PhantomData }) |
| 31 | } |
| 32 | |
| 33 | impl<T> Unpin for Drain<T> {} |
| 34 | |
| 35 | impl<T> Clone for Drain<T> { |
| 36 | fn clone(&self) -> Self { |
| 37 | drain() |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl<T> Sink<T> for Drain<T> { |
| 42 | type Error = Never; |
| 43 | |
| 44 | fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 45 | Poll::Ready(Ok(())) |
| 46 | } |
| 47 | |
| 48 | fn start_send(self: Pin<&mut Self>, _item: T) -> Result<(), Self::Error> { |
| 49 | Ok(()) |
| 50 | } |
| 51 | |
| 52 | fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 53 | Poll::Ready(Ok(())) |
| 54 | } |
| 55 | |
| 56 | fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 57 | Poll::Ready(Ok(())) |
| 58 | } |
| 59 | } |
| 60 | |