async_sink/ext/
drain.rs

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