| 1 | use std::fmt; |
| 2 | use std::pin::Pin; |
| 3 | |
| 4 | use crate::io::{self, Write}; |
| 5 | use crate::task::{Context, Poll}; |
| 6 | |
| 7 | /// Creates a writer that consumes and drops all data. |
| 8 | /// |
| 9 | /// # Examples |
| 10 | /// |
| 11 | /// ```rust |
| 12 | /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 13 | /// # |
| 14 | /// use async_std::io; |
| 15 | /// use async_std::prelude::*; |
| 16 | /// |
| 17 | /// let mut writer = io::sink(); |
| 18 | /// writer.write(b"hello world" ).await?; |
| 19 | /// # |
| 20 | /// # Ok(()) }) } |
| 21 | /// ``` |
| 22 | pub fn sink() -> Sink { |
| 23 | Sink { _private: () } |
| 24 | } |
| 25 | |
| 26 | /// A writer that consumes and drops all data. |
| 27 | /// |
| 28 | /// This writer is constructed by the [`sink`] function. See its documentation |
| 29 | /// for more. |
| 30 | /// |
| 31 | /// [`sink`]: fn.sink.html |
| 32 | pub struct Sink { |
| 33 | _private: (), |
| 34 | } |
| 35 | |
| 36 | impl fmt::Debug for Sink { |
| 37 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 38 | f.pad("Sink { .. }" ) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl Write for Sink { |
| 43 | #[inline ] |
| 44 | fn poll_write( |
| 45 | self: Pin<&mut Self>, |
| 46 | _: &mut Context<'_>, |
| 47 | buf: &[u8], |
| 48 | ) -> Poll<io::Result<usize>> { |
| 49 | Poll::Ready(Ok(buf.len())) |
| 50 | } |
| 51 | |
| 52 | #[inline ] |
| 53 | fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 54 | Poll::Ready(Ok(())) |
| 55 | } |
| 56 | |
| 57 | #[inline ] |
| 58 | fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 59 | Poll::Ready(Ok(())) |
| 60 | } |
| 61 | } |
| 62 | |