@@ -49,17 +49,20 @@ const DEFAULT_CAPACITY: usize = 8 * 1024;
49
49
/// overhead of a system call per byte written. We can fix this with a
50
50
/// `BufWriter`:
51
51
///
52
- /*/ ```no_run
53
- / use std::io::prelude::*;
54
- / use std::io::BufWriter;
55
- / use std::net::TcpStream;
56
- /
57
- / let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
58
- /
59
- / for i in 0..10 {
60
- / stream.write(&[i+1]).unwrap();
61
- / }
62
- / ```*/
52
+ /// ```no_run
53
+ /// use async_std::io::prelude::*;
54
+ /// use async_std::io::BufWriter;
55
+ /// use async_std::net::TcpStream;
56
+ /// use futures::AsyncWrite;
57
+ /// use async_std::io::Write;
58
+ ///
59
+ /// async_std::task::block_on(async {
60
+ /// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
61
+ /// for i in 0..10 {
62
+ /// stream.write(&[i+1]).await;
63
+ /// }
64
+ /// });
65
+ /// ```
63
66
///
64
67
/// By wrapping the stream with a `BufWriter`, these ten writes are all grouped
65
68
/// together by the buffer, and will all be written out in one system call when
@@ -115,10 +118,38 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
115
118
}
116
119
}
117
120
121
+ /// Gets a reference to the underlying writer.
122
+ ///
123
+ /// # Examples
124
+ ///
125
+ /// ```no_run
126
+ /// use async_std::io::BufWriter;
127
+ /// use async_std::net::TcpStream;
128
+ ///
129
+ /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
130
+ ///
131
+ /// // we can use reference just like buffer
132
+ /// let reference = buffer.get_ref();
133
+ /// ```
118
134
pub fn get_ref ( & self ) -> & W {
119
135
& self . inner
120
136
}
121
137
138
+ /// Gets a mutable reference to the underlying writer.
139
+ ///
140
+ /// It is inadvisable to directly write to the underlying writer.
141
+ ///
142
+ /// # Examples
143
+ ///
144
+ /// ```no_run
145
+ /// use async_std::io::BufWriter;
146
+ /// use async_std::net::TcpStream;
147
+ ///
148
+ /// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
149
+ ///
150
+ /// // we can use reference just like buffer
151
+ /// let reference = buffer.get_mut();
152
+ /// ```
122
153
pub fn get_mut ( & mut self ) -> & mut W {
123
154
& mut self . inner
124
155
}
@@ -141,6 +172,19 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
141
172
unimplemented ! ( "poll into inner method" )
142
173
}
143
174
175
+ /// Returns a reference to the internally buffered data.
176
+ ///
177
+ /// # Examples
178
+ ///
179
+ /// ```no_run
180
+ /// use async_std::io::BufWriter;
181
+ /// use async_std::net::TcpStream;
182
+ ///
183
+ /// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
184
+ ///
185
+ /// // See how many bytes are currently buffered
186
+ /// let bytes_buffered = buf_writer.buffer().len();
187
+ /// ```
144
188
pub fn buffer ( & self ) -> & [ u8 ] {
145
189
& self . buf
146
190
}
@@ -192,7 +236,7 @@ impl<W: AsyncWrite + Unpin> AsyncWrite for BufWriter<W> {
192
236
if buf. len ( ) >= self . buf . capacity ( ) {
193
237
self . inner ( ) . poll_write ( cx, buf)
194
238
} else {
195
- self . buf ( ) . write ( buf) . poll ( )
239
+ self . buf ( ) . write ( buf) . poll ( cx )
196
240
}
197
241
}
198
242
0 commit comments