async_std/io/
empty.rs

1use std::fmt;
2use std::pin::Pin;
3
4use crate::io::{self, BufRead, Read};
5use crate::task::{Context, Poll};
6
7/// Creates a reader that contains no 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 buf = Vec::new();
18/// let mut reader = io::empty();
19/// reader.read_to_end(&mut buf).await?;
20///
21/// assert!(buf.is_empty());
22/// #
23/// # Ok(()) }) }
24/// ```
25pub fn empty() -> Empty {
26    Empty { _private: () }
27}
28
29/// A reader that contains no data.
30///
31/// This reader is created by the [`empty`] function. See its
32/// documentation for more.
33///
34/// [`empty`]: fn.empty.html
35pub struct Empty {
36    _private: (),
37}
38
39impl fmt::Debug for Empty {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        f.pad("Empty { .. }")
42    }
43}
44
45impl Read for Empty {
46    #[inline]
47    fn poll_read(
48        self: Pin<&mut Self>,
49        _: &mut Context<'_>,
50        _: &mut [u8],
51    ) -> Poll<io::Result<usize>> {
52        Poll::Ready(Ok(0))
53    }
54}
55
56impl BufRead for Empty {
57    #[inline]
58    fn poll_fill_buf<'a>(
59        self: Pin<&'a mut Self>,
60        _: &mut Context<'_>,
61    ) -> Poll<io::Result<&'a [u8]>> {
62        Poll::Ready(Ok(&[]))
63    }
64
65    #[inline]
66    fn consume(self: Pin<&mut Self>, _: usize) {}
67}