1 use futures_core::task::{Context, Poll};
2 use futures_io::{AsyncBufRead, AsyncRead};
3 use std::fmt;
4 use std::io;
5 use std::pin::Pin;
6 
7 /// Reader for the [`empty()`] function.
8 #[must_use = "readers do nothing unless polled"]
9 pub 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 /// ```
empty() -> Empty31 pub fn empty() -> Empty {
32     Empty { _priv: () }
33 }
34 
35 impl AsyncRead for Empty {
36     #[inline]
poll_read( self: Pin<&mut Self>, _: &mut Context<'_>, _: &mut [u8], ) -> Poll<io::Result<usize>>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 
46 impl AsyncBufRead for Empty {
47     #[inline]
poll_fill_buf(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<&[u8]>>48     fn poll_fill_buf(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
49         Poll::Ready(Ok(&[]))
50     }
51     #[inline]
consume(self: Pin<&mut Self>, _: usize)52     fn consume(self: Pin<&mut Self>, _: usize) {}
53 }
54 
55 impl fmt::Debug for Empty {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result56     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57         f.pad("Empty { .. }")
58     }
59 }
60