@@ -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 ) ]
1819pub 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-
8696impl 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 ) ]
190248pub 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- }
0 commit comments