Skip to content

Commit 79a22f4

Browse files
committed
feat: merge functionality of io::Sink into io::Empty
1 parent dd9a7bf commit 79a22f4

File tree

2 files changed

+87
-26
lines changed

2 files changed

+87
-26
lines changed

library/std/src/io/util.rs

+76-24
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,41 @@ use crate::io::{
88
self, BorrowedCursor, BufRead, IoSlice, IoSliceMut, Read, Seek, SeekFrom, SizeHint, Write,
99
};
1010

11-
/// A reader which is always at EOF.
11+
/// `Empty` ignores any data written via [`Write`], and will always be empty
12+
/// (returning zero bytes) when read via [`Read`].
1213
///
13-
/// This struct is generally created by calling [`empty()`]. Please see
14-
/// the documentation of [`empty()`] for more details.
14+
/// This struct is generally created by calling [`empty()`]. Please
15+
/// see the documentation of [`empty()`] for more details.
1516
#[stable(feature = "rust1", since = "1.0.0")]
1617
#[non_exhaustive]
17-
#[derive(Copy, Clone, Default)]
18+
#[derive(Copy, Clone, Debug, Default)]
1819
pub struct Empty;
1920

20-
/// Constructs a new handle to an empty reader.
21+
/// Creates a value that is always at EOF for reads, and ignores all data written.
2122
///
22-
/// All reads from the returned reader will return <code>[Ok]\(0)</code>.
23+
/// All calls to [`write`] on the returned instance will return [`Ok(buf.len())`]
24+
/// and the contents of the buffer will not be inspected.
25+
///
26+
/// All calls to [`read`] from the returned reader will return [`Ok(0)`].
27+
///
28+
/// [`Ok(buf.len())`]: Ok
29+
/// [`Ok(0)`]: Ok
30+
///
31+
/// [`write`]: Write::write
32+
/// [`read`]: Read::read
2333
///
2434
/// # Examples
2535
///
26-
/// A slightly sad example of not reading anything into a buffer:
36+
/// ```rust
37+
/// use std::io::{self, Write};
2738
///
39+
/// let buffer = vec![1, 2, 3, 5, 8];
40+
/// let num_bytes = io::empty().write(&buffer).unwrap();
41+
/// assert_eq!(num_bytes, 5);
2842
/// ```
43+
///
44+
///
45+
/// ```rust
2946
/// use std::io::{self, Read};
3047
///
3148
/// let mut buffer = String::new();
@@ -76,20 +93,61 @@ impl Seek for Empty {
7693
}
7794
}
7895

79-
#[stable(feature = "std_debug", since = "1.16.0")]
80-
impl fmt::Debug for Empty {
81-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82-
f.debug_struct("Empty").finish_non_exhaustive()
83-
}
84-
}
85-
8696
impl SizeHint for Empty {
8797
#[inline]
8898
fn upper_bound(&self) -> Option<usize> {
8999
Some(0)
90100
}
91101
}
92102

103+
#[stable(feature = "empty_write", since = "1.64.0")]
104+
impl Write for Empty {
105+
#[inline]
106+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
107+
Ok(buf.len())
108+
}
109+
110+
#[inline]
111+
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
112+
let total_len = bufs.iter().map(|b| b.len()).sum();
113+
Ok(total_len)
114+
}
115+
116+
#[inline]
117+
fn is_write_vectored(&self) -> bool {
118+
true
119+
}
120+
121+
#[inline]
122+
fn flush(&mut self) -> io::Result<()> {
123+
Ok(())
124+
}
125+
}
126+
127+
#[stable(feature = "empty_write", since = "1.64.0")]
128+
impl Write for &Empty {
129+
#[inline]
130+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
131+
Ok(buf.len())
132+
}
133+
134+
#[inline]
135+
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
136+
let total_len = bufs.iter().map(|b| b.len()).sum();
137+
Ok(total_len)
138+
}
139+
140+
#[inline]
141+
fn is_write_vectored(&self) -> bool {
142+
true
143+
}
144+
145+
#[inline]
146+
fn flush(&mut self) -> io::Result<()> {
147+
Ok(())
148+
}
149+
}
150+
93151
/// A reader which yields one byte over and over and over and over and over and...
94152
///
95153
/// This struct is generally created by calling [`repeat()`]. Please
@@ -182,19 +240,20 @@ impl fmt::Debug for Repeat {
182240

183241
/// A writer which will move data into the void.
184242
///
185-
/// This struct is generally created by calling [`sink`]. Please
243+
/// This struct is generally created by calling [`sink()`]. Please
186244
/// see the documentation of [`sink()`] for more details.
187245
#[stable(feature = "rust1", since = "1.0.0")]
188246
#[non_exhaustive]
189-
#[derive(Copy, Clone, Default)]
247+
#[derive(Copy, Clone, Debug, Default)]
190248
pub struct Sink;
191249

192250
/// Creates an instance of a writer which will successfully consume all data.
193251
///
194-
/// All calls to [`write`] on the returned instance will return `Ok(buf.len())`
252+
/// All calls to [`write`] on the returned instance will return [`Ok(buf.len())`]
195253
/// and the contents of the buffer will not be inspected.
196254
///
197255
/// [`write`]: Write::write
256+
/// [`Ok(buf.len())`]: Ok
198257
///
199258
/// # Examples
200259
///
@@ -259,10 +318,3 @@ impl Write for &Sink {
259318
Ok(())
260319
}
261320
}
262-
263-
#[stable(feature = "std_debug", since = "1.16.0")]
264-
impl fmt::Debug for Sink {
265-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
266-
f.debug_struct("Sink").finish_non_exhaustive()
267-
}
268-
}

library/std/src/io/util/tests.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn empty_reads() {
7777
assert_eq!(e.read(&mut []).unwrap(), 0);
7878
assert_eq!(e.read(&mut [0]).unwrap(), 0);
7979
assert_eq!(e.read(&mut [0; 1024]).unwrap(), 0);
80-
assert_eq!(e.by_ref().read(&mut [0; 1024]).unwrap(), 0);
80+
assert_eq!(Read::by_ref(&mut e).read(&mut [0; 1024]).unwrap(), 0);
8181

8282
let buf: &mut [MaybeUninit<_>] = &mut [];
8383
let mut buf: BorrowedBuf<'_> = buf.into();
@@ -99,7 +99,7 @@ fn empty_reads() {
9999

100100
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 1024];
101101
let mut buf: BorrowedBuf<'_> = buf.into();
102-
e.by_ref().read_buf(buf.unfilled()).unwrap();
102+
Read::by_ref(&mut e).read_buf(buf.unfilled()).unwrap();
103103
assert_eq!(buf.len(), 0);
104104
assert_eq!(buf.init_len(), 0);
105105
}
@@ -124,6 +124,15 @@ fn empty_seeks() {
124124
assert!(matches!(e.seek(SeekFrom::Current(i64::MAX)), Ok(0)));
125125
}
126126

127+
#[test]
128+
fn empty_sinks() {
129+
let mut e = empty();
130+
assert_eq!(e.write(&[]).unwrap(), 0);
131+
assert_eq!(e.write(&[0]).unwrap(), 1);
132+
assert_eq!(e.write(&[0; 1024]).unwrap(), 1024);
133+
assert_eq!(Write::by_ref(&mut e).write(&[0; 1024]).unwrap(), 1024);
134+
}
135+
127136
#[test]
128137
fn repeat_repeats() {
129138
let mut r = repeat(4);

0 commit comments

Comments
 (0)