broker_tokio/io/util/empty.rs
1use crate::io::{AsyncBufRead, AsyncRead};
2
3use std::fmt;
4use std::io;
5use std::pin::Pin;
6use std::task::{Context, Poll};
7
8cfg_io_util! {
9 /// An async reader which is always at EOF.
10 ///
11 /// This struct is generally created by calling [`empty`]. Please see
12 /// the documentation of [`empty()`][`empty`] for more details.
13 ///
14 /// This is an asynchronous version of [`std::io::empty`][std].
15 ///
16 /// [`empty`]: fn.empty.html
17 /// [std]: std::io::empty
18 pub struct Empty {
19 _p: (),
20 }
21
22 /// Creates a new empty async reader.
23 ///
24 /// All reads from the returned reader will return `Poll::Ready(Ok(0))`.
25 ///
26 /// This is an asynchronous version of [`std::io::empty`][std].
27 ///
28 /// [std]: std::io::empty
29 ///
30 /// # Examples
31 ///
32 /// A slightly sad example of not reading anything into a buffer:
33 ///
34 /// ```
35 /// use tokio::io::{self, AsyncReadExt};
36 ///
37 /// #[tokio::main]
38 /// async fn main() {
39 /// let mut buffer = String::new();
40 /// io::empty().read_to_string(&mut buffer).await.unwrap();
41 /// assert!(buffer.is_empty());
42 /// }
43 /// ```
44 pub fn empty() -> Empty {
45 Empty { _p: () }
46 }
47}
48
49impl AsyncRead for Empty {
50 #[inline]
51 fn poll_read(
52 self: Pin<&mut Self>,
53 _: &mut Context<'_>,
54 _: &mut [u8],
55 ) -> Poll<io::Result<usize>> {
56 Poll::Ready(Ok(0))
57 }
58}
59
60impl AsyncBufRead for Empty {
61 #[inline]
62 fn poll_fill_buf(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
63 Poll::Ready(Ok(&[]))
64 }
65
66 #[inline]
67 fn consume(self: Pin<&mut Self>, _: usize) {}
68}
69
70impl fmt::Debug for Empty {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 f.pad("Empty { .. }")
73 }
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79
80 #[test]
81 fn assert_unpin() {
82 crate::is_unpin::<Empty>();
83 }
84}