@@ -8,24 +8,41 @@ use crate::io::{
8
8
self , BorrowedCursor , BufRead , IoSlice , IoSliceMut , Read , Seek , SeekFrom , SizeHint , Write ,
9
9
} ;
10
10
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`].
12
13
///
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.
15
16
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
16
17
#[ non_exhaustive]
17
- #[ derive( Copy , Clone , Default ) ]
18
+ #[ derive( Copy , Clone , Debug , Default ) ]
18
19
pub struct Empty ;
19
20
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 .
21
22
///
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
23
33
///
24
34
/// # Examples
25
35
///
26
- /// A slightly sad example of not reading anything into a buffer:
36
+ /// ```rust
37
+ /// use std::io::{self, Write};
27
38
///
39
+ /// let buffer = vec![1, 2, 3, 5, 8];
40
+ /// let num_bytes = io::empty().write(&buffer).unwrap();
41
+ /// assert_eq!(num_bytes, 5);
28
42
/// ```
43
+ ///
44
+ ///
45
+ /// ```rust
29
46
/// use std::io::{self, Read};
30
47
///
31
48
/// let mut buffer = String::new();
@@ -76,20 +93,61 @@ impl Seek for Empty {
76
93
}
77
94
}
78
95
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
-
86
96
impl SizeHint for Empty {
87
97
#[ inline]
88
98
fn upper_bound ( & self ) -> Option < usize > {
89
99
Some ( 0 )
90
100
}
91
101
}
92
102
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
+
93
151
/// A reader which yields one byte over and over and over and over and over and...
94
152
///
95
153
/// This struct is generally created by calling [`repeat()`]. Please
@@ -182,19 +240,20 @@ impl fmt::Debug for Repeat {
182
240
183
241
/// A writer which will move data into the void.
184
242
///
185
- /// This struct is generally created by calling [`sink`]. Please
243
+ /// This struct is generally created by calling [`sink() `]. Please
186
244
/// see the documentation of [`sink()`] for more details.
187
245
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
188
246
#[ non_exhaustive]
189
- #[ derive( Copy , Clone , Default ) ]
247
+ #[ derive( Copy , Clone , Debug , Default ) ]
190
248
pub struct Sink ;
191
249
192
250
/// Creates an instance of a writer which will successfully consume all data.
193
251
///
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())`]
195
253
/// and the contents of the buffer will not be inspected.
196
254
///
197
255
/// [`write`]: Write::write
256
+ /// [`Ok(buf.len())`]: Ok
198
257
///
199
258
/// # Examples
200
259
///
@@ -259,10 +318,3 @@ impl Write for &Sink {
259
318
Ok ( ( ) )
260
319
}
261
320
}
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
- }
0 commit comments