| 1 |
use futures_core::task::{Context, Poll}; |
| 2 |
use futures_io::{AsyncWrite, IoSlice}; |
| 3 |
use std::fmt; |
| 4 |
use std::io; |
| 5 |
use std::pin::Pin; |
| 6 |
|
| 7 |
/// Writer for the [`sink()`] function. |
| 8 |
#[must_use = "writers do nothing unless polled"] |
| 9 |
pub struct Sink { |
| 10 |
_priv: (), |
| 11 |
} |
| 12 |
|
| 13 |
/// Creates an instance of a writer which will successfully consume all data. |
| 14 |
/// |
| 15 |
/// All calls to `poll_write` on the returned instance will return `Poll::Ready(Ok(buf.len()))` |
| 16 |
/// and the contents of the buffer will not be inspected. |
| 17 |
/// |
| 18 |
/// # Examples |
| 19 |
/// |
| 20 |
/// ```rust |
| 21 |
/// # futures::executor::block_on(async { |
| 22 |
/// use futures::io::{self, AsyncWriteExt}; |
| 23 |
/// |
| 24 |
/// let buffer = vec![1, 2, 3, 5, 8]; |
| 25 |
/// let mut writer = io::sink(); |
| 26 |
/// let num_bytes = writer.write(&buffer).await?; |
| 27 |
/// assert_eq!(num_bytes, 5); |
| 28 |
/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap(); |
| 29 |
/// ``` |
| 30 |
pub fn sink() -> Sink { |
| 31 |
Sink { _priv: () } |
| 32 |
} |
| 33 |
|
| 34 |
impl AsyncWrite for Sink { |
| 35 |
#[inline] |
| 36 |
fn poll_write( |
| 37 |
self: Pin<&mut Self>, |
| 38 |
_: &mut Context<'_>, |
| 39 |
buf: &[u8], |
| 40 |
) -> Poll<io::Result<usize>> { |
| 41 |
Poll::Ready(Ok(buf.len())) |
| 42 |
} |
| 43 |
|
| 44 |
#[inline] |
| 45 |
fn poll_write_vectored( |
| 46 |
self: Pin<&mut Self>, |
| 47 |
_: &mut Context<'_>, |
| 48 |
bufs: &[IoSlice<'_>], |
| 49 |
) -> Poll<io::Result<usize>> { |
| 50 |
Poll::Ready(Ok(bufs.iter().map(|b| b.len()).sum())) |
| 51 |
} |
| 52 |
|
| 53 |
#[inline] |
| 54 |
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 55 |
Poll::Ready(Ok(())) |
| 56 |
} |
| 57 |
#[inline] |
| 58 |
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 59 |
Poll::Ready(Ok(())) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
impl fmt::Debug for Sink { |
| 64 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 65 |
f.pad("Sink { .. }") |
| 66 |
} |
| 67 |
} |