/rust/registry/src/index.crates.io-6f17d22bba15001f/futures-util-0.3.28/src/io/sink.rs
Line | Count | Source (jump to first uncovered line) |
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 | 0 | pub fn sink() -> Sink { |
31 | 0 | Sink { _priv: () } |
32 | 0 | } |
33 | | |
34 | | impl AsyncWrite for Sink { |
35 | | #[inline] |
36 | 0 | fn poll_write( |
37 | 0 | self: Pin<&mut Self>, |
38 | 0 | _: &mut Context<'_>, |
39 | 0 | buf: &[u8], |
40 | 0 | ) -> Poll<io::Result<usize>> { |
41 | 0 | Poll::Ready(Ok(buf.len())) |
42 | 0 | } |
43 | | |
44 | | #[inline] |
45 | 0 | fn poll_write_vectored( |
46 | 0 | self: Pin<&mut Self>, |
47 | 0 | _: &mut Context<'_>, |
48 | 0 | bufs: &[IoSlice<'_>], |
49 | 0 | ) -> Poll<io::Result<usize>> { |
50 | 0 | Poll::Ready(Ok(bufs.iter().map(|b| b.len()).sum())) |
51 | 0 | } |
52 | | |
53 | | #[inline] |
54 | 0 | fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> { |
55 | 0 | Poll::Ready(Ok(())) |
56 | 0 | } |
57 | | #[inline] |
58 | 0 | fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> { |
59 | 0 | Poll::Ready(Ok(())) |
60 | 0 | } |
61 | | } |
62 | | |
63 | | impl fmt::Debug for Sink { |
64 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
65 | 0 | f.pad("Sink { .. }") |
66 | 0 | } |
67 | | } |