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/// # #[tokio::main]
22/// # async fn main() -> Result<(), core::convert::Infallible> {
23/// use crate::sink::{self, SinkExt};
24///
25/// let mut drain = sink::drain();
26/// drain.send(5).await?;
27/// # Ok(())
28/// # }
29/// ```
30pub fn drain<T>() -> Drain<T> {
31    Drain {
32        marker: PhantomData,
33    }
34}
35
36impl<T> Unpin for Drain<T> {}
37
38impl<T> Clone for Drain<T> {
39    fn clone(&self) -> Self {
40        drain()
41    }
42}
43
44impl<T> Sink<T> for Drain<T> {
45    type Error = Infallible;
46
47    fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
48        Poll::Ready(Ok(()))
49    }
50
51    fn start_send(self: Pin<&mut Self>, _item: T) -> Result<(), Self::Error> {
52        Ok(())
53    }
54
55    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
56        Poll::Ready(Ok(()))
57    }
58
59    fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
60        Poll::Ready(Ok(()))
61    }
62}