futures_util/io/
empty.rs

1use futures_core::task::{Context, Poll};
2use futures_io::{AsyncBufRead, AsyncRead};
3use std::fmt;
4use std::io;
5use std::pin::Pin;
6
7/// Reader for the [`empty()`] function.
8#[must_use = "readers do nothing unless polled"]
9pub struct Empty {
10    _priv: (),
11}
12
13/// Constructs a new handle to an empty reader.
14///
15/// All reads from the returned reader will return `Poll::Ready(Ok(0))`.
16///
17/// # Examples
18///
19/// A slightly sad example of not reading anything into a buffer:
20///
21/// ```
22/// # futures::executor::block_on(async {
23/// use futures::io::{self, AsyncReadExt};
24///
25/// let mut buffer = String::new();
26/// let mut reader = io::empty();
27/// reader.read_to_string(&mut buffer).await?;
28/// assert!(buffer.is_empty());
29/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
30/// ```
31pub fn empty() -> Empty {
32    Empty { _priv: () }
33}
34
35impl AsyncRead for Empty {
36    #[inline]
37    fn poll_read(
38        self: Pin<&mut Self>,
39        _: &mut Context<'_>,
40        _: &mut [u8],
41    ) -> Poll<io::Result<usize>> {
42        Poll::Ready(Ok(0))
43    }
44}
45
46impl AsyncBufRead for Empty {
47    #[inline]
48    fn poll_fill_buf(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
49        Poll::Ready(Ok(&[]))
50    }
51    #[inline]
52    fn consume(self: Pin<&mut Self>, _: usize) {}
53}
54
55impl fmt::Debug for Empty {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        f.pad("Empty { .. }")
58    }
59}