1#[cfg(feature = "alloc")]
2use alloc::{string::String, vec::Vec};
3use core::{fmt, io::BorrowedCursor};
4
5use crate::{BufRead, Error, IoBuf, IoBufMut, Read, Result, Seek, SeekFrom, Write};
6
7#[non_exhaustive]
13#[derive(Copy, Clone, Debug, Default)]
14pub struct Empty;
15
16#[must_use]
20pub const fn empty() -> Empty {
21 Empty
22}
23
24impl Read for Empty {
25 #[inline]
26 fn read(&mut self, _buf: &mut [u8]) -> Result<usize> {
27 Ok(0)
28 }
29
30 #[inline]
31 fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
32 if !buf.is_empty() {
33 Err(Error::UnexpectedEof)
34 } else {
35 Ok(())
36 }
37 }
38
39 #[inline]
40 fn read_buf(&mut self, _cursor: BorrowedCursor<'_>) -> Result<()> {
41 Ok(())
42 }
43
44 #[inline]
45 fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<()> {
46 if cursor.capacity() != 0 {
47 Err(Error::UnexpectedEof)
48 } else {
49 Ok(())
50 }
51 }
52
53 #[inline]
54 #[cfg(feature = "alloc")]
55 fn read_to_end(&mut self, _buf: &mut Vec<u8>) -> Result<usize> {
56 Ok(0)
57 }
58
59 #[inline]
60 #[cfg(feature = "alloc")]
61 fn read_to_string(&mut self, _buf: &mut String) -> Result<usize> {
62 Ok(0)
63 }
64}
65
66impl BufRead for Empty {
67 #[inline]
68 fn fill_buf(&mut self) -> Result<&[u8]> {
69 Ok(&[])
70 }
71
72 #[inline]
73 fn consume(&mut self, _n: usize) {}
74
75 #[inline]
76 fn has_data_left(&mut self) -> Result<bool> {
77 Ok(false)
78 }
79
80 #[inline]
81 fn skip_until(&mut self, _byte: u8) -> Result<usize> {
82 Ok(0)
83 }
84
85 #[inline]
86 #[cfg(feature = "alloc")]
87 fn read_until(&mut self, _byte: u8, _buf: &mut Vec<u8>) -> Result<usize> {
88 Ok(0)
89 }
90
91 #[inline]
92 #[cfg(feature = "alloc")]
93 fn read_line(&mut self, _buf: &mut String) -> Result<usize> {
94 Ok(0)
95 }
96}
97
98impl Seek for Empty {
99 #[inline]
100 fn seek(&mut self, _pos: SeekFrom) -> Result<u64> {
101 Ok(0)
102 }
103
104 #[inline]
105 fn stream_len(&mut self) -> Result<u64> {
106 Ok(0)
107 }
108
109 #[inline]
110 fn stream_position(&mut self) -> Result<u64> {
111 Ok(0)
112 }
113}
114
115impl Write for Empty {
116 #[inline]
117 fn write(&mut self, buf: &[u8]) -> Result<usize> {
118 Ok(buf.len())
119 }
120
121 #[inline]
122 fn write_all(&mut self, _buf: &[u8]) -> Result<()> {
123 Ok(())
124 }
125
126 #[inline]
127 fn write_fmt(&mut self, _args: fmt::Arguments<'_>) -> Result<()> {
128 Ok(())
129 }
130
131 #[inline]
132 fn flush(&mut self) -> Result<()> {
133 Ok(())
134 }
135}
136
137impl Write for &Empty {
138 #[inline]
139 fn write(&mut self, buf: &[u8]) -> Result<usize> {
140 Ok(buf.len())
141 }
142
143 #[inline]
144 fn write_all(&mut self, _buf: &[u8]) -> Result<()> {
145 Ok(())
146 }
147
148 #[inline]
149 fn write_fmt(&mut self, _args: fmt::Arguments<'_>) -> Result<()> {
150 Ok(())
151 }
152
153 #[inline]
154 fn flush(&mut self) -> Result<()> {
155 Ok(())
156 }
157}
158
159impl IoBuf for Empty {
160 #[inline]
161 fn remaining(&self) -> usize {
162 0
163 }
164}
165
166impl IoBufMut for Empty {
167 #[inline]
168 fn remaining_mut(&self) -> usize {
169 usize::MAX
170 }
171}