1#[cfg(test)]
2mod tests;
3
4use crate::io;
5use crate::io::{BorrowedCursor, BufRead, Read, Seek, SeekFrom, SizeHint, Write};
6use alloc::fmt;
7use alloc::string::String;
8use alloc::vec::Vec;
9
10#[non_exhaustive]
16#[derive(Copy, Clone, Debug, Default)]
17pub struct Empty;
18
19#[must_use]
51pub const fn empty() -> Empty {
52 Empty
53}
54impl Read for Empty {
55 #[inline]
56 fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
57 Ok(0)
58 }
59
60 #[inline]
61 fn read_buf(&mut self, _cursor: BorrowedCursor<'_>) -> io::Result<()> {
62 Ok(())
63 }
64}
65impl BufRead for Empty {
66 #[inline]
67 fn fill_buf(&mut self) -> io::Result<&[u8]> {
68 Ok(&[])
69 }
70 #[inline]
71 fn consume(&mut self, _n: usize) {}
72}
73impl Seek for Empty {
74 fn seek(&mut self, _pos: SeekFrom) -> io::Result<u64> {
75 Ok(0)
76 }
77
78 fn stream_len(&mut self) -> io::Result<u64> {
79 Ok(0)
80 }
81
82 fn stream_position(&mut self) -> io::Result<u64> {
83 Ok(0)
84 }
85}
86
87impl SizeHint for Empty {
88 #[inline]
89 fn lower_bound(&self) -> usize {
90 0
91 }
92 #[inline]
93 fn upper_bound(&self) -> Option<usize> {
94 Some(0)
95 }
96}
97impl Write for Empty {
98 #[inline]
99 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
100 Ok(buf.len())
101 }
102 #[inline]
103 fn is_write_vectored(&self) -> bool {
104 true
105 }
106
107 #[inline]
108 fn flush(&mut self) -> io::Result<()> {
109 Ok(())
110 }
111}
112impl Write for &Empty {
113 #[inline]
114 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
115 Ok(buf.len())
116 }
117 #[inline]
118 fn is_write_vectored(&self) -> bool {
119 true
120 }
121
122 #[inline]
123 fn flush(&mut self) -> io::Result<()> {
124 Ok(())
125 }
126}
127
128pub struct Repeat {
133 byte: u8,
134}
135
136#[must_use]
151pub const fn repeat(byte: u8) -> Repeat {
152 Repeat { byte }
153}
154impl Read for Repeat {
155 #[inline]
156 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
157 for slot in &mut *buf {
158 *slot = self.byte;
159 }
160 Ok(buf.len())
161 }
162
163 fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> io::Result<()> {
164 for slot in unsafe { buf.as_mut() } {
166 slot.write(self.byte);
167 }
168
169 let remaining = buf.capacity();
170
171 unsafe {
173 buf.advance_unchecked(remaining);
174 }
175
176 Ok(())
177 }
178
179 fn read_to_end(&mut self, _: &mut Vec<u8>) -> io::Result<usize> {
181 Err(io::Error::from(io::ErrorKind::OutOfMemory))
182 }
183
184 fn read_to_string(&mut self, _: &mut String) -> io::Result<usize> {
186 Err(io::Error::from(io::ErrorKind::OutOfMemory))
187 }
188}
189
190impl SizeHint for Repeat {
191 #[inline]
192 fn lower_bound(&self) -> usize {
193 usize::MAX
194 }
195
196 #[inline]
197 fn upper_bound(&self) -> Option<usize> {
198 None
199 }
200}
201impl fmt::Debug for Repeat {
202 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
203 f.debug_struct("Repeat").finish_non_exhaustive()
204 }
205}
206
207#[non_exhaustive]
212#[derive(Copy, Clone, Debug, Default)]
213pub struct Sink;
214
215#[must_use]
233pub const fn sink() -> Sink {
234 Sink
235}
236impl Write for Sink {
237 #[inline]
238 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
239 Ok(buf.len())
240 }
241
242 #[inline]
243 fn is_write_vectored(&self) -> bool {
244 true
245 }
246
247 #[inline]
248 fn flush(&mut self) -> io::Result<()> {
249 Ok(())
250 }
251}
252impl Write for &Sink {
253 #[inline]
254 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
255 Ok(buf.len())
256 }
257 #[inline]
258 fn is_write_vectored(&self) -> bool {
259 true
260 }
261
262 #[inline]
263 fn flush(&mut self) -> io::Result<()> {
264 Ok(())
265 }
266}