Skip to content

Commit ae66973

Browse files
committed
Fix the LineWriter bug and documentation tests
Signed-off-by: Kirill Mironov <[email protected]>
1 parent f046d19 commit ae66973

File tree

1 file changed

+69
-44
lines changed

1 file changed

+69
-44
lines changed

src/io/buf_writer.rs

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,40 @@ const DEFAULT_CAPACITY: usize = 8 * 1024;
3434
///
3535
/// Let's write the numbers one through ten to a [`TcpStream`]:
3636
///
37-
/*/ ```no_run
38-
/ use std::io::prelude::*;
39-
/ use std::net::TcpStream;
40-
/
41-
/ let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap();
42-
/
43-
/ for i in 0..10 {
44-
/ stream.write(&[i+1]).unwrap();
45-
/ }
46-
/ ```*/
37+
/// ```no_run
38+
/// # #![feature(async_await)]
39+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
40+
/// use async_std::net::TcpStream;
41+
/// use async_std::io::Write;
42+
///
43+
/// let mut stream = TcpStream::connect("127.0.0.1:34254").await?;
44+
///
45+
/// for i in 0..10 {
46+
/// let arr = [i+1];
47+
/// stream.write(&arr).await?;
48+
/// }
49+
/// #
50+
/// # Ok(()) }) }
51+
/// ```
4752
///
4853
/// Because we're not buffering, we write each one in turn, incurring the
4954
/// overhead of a system call per byte written. We can fix this with a
5055
/// `BufWriter`:
5156
///
5257
/// ```no_run
53-
/// use async_std::io::prelude::*;
58+
/// # #![feature(async_await)]
59+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
5460
/// use async_std::io::BufWriter;
5561
/// use async_std::net::TcpStream;
56-
/// use futures::AsyncWrite;
5762
/// use async_std::io::Write;
5863
///
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-
/// });
64+
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").await?);
65+
/// for i in 0..10 {
66+
/// let arr = [i+1];
67+
/// stream.write(&arr).await?;
68+
/// };
69+
/// #
70+
/// # Ok(()) }) }
6571
/// ```
6672
///
6773
/// By wrapping the stream with a `BufWriter`, these ten writes are all grouped
@@ -88,10 +94,15 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
8894
/// # Examples
8995
///
9096
/// ```no_run
97+
/// # #![feature(async_await)]
98+
/// # #![allow(unused_mut)]
99+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
91100
/// use async_std::io::BufWriter;
92101
/// use async_std::net::TcpStream;
93102
///
94-
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
103+
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").await?);
104+
/// #
105+
/// # Ok(()) }) }
95106
/// ```
96107
pub fn new(inner: W) -> BufWriter<W> {
97108
BufWriter::with_capacity(DEFAULT_CAPACITY, inner)
@@ -104,11 +115,16 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
104115
/// Creating a buffer with a buffer of a hundred bytes.
105116
///
106117
/// ```no_run
118+
/// # #![feature(async_await)]
119+
/// # #![allow(unused_mut)]
120+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
107121
/// use async_std::io::BufWriter;
108122
/// use async_std::net::TcpStream;
109123
///
110-
/// let stream = TcpStream::connect("127.0.0.1:34254").unwrap();
124+
/// let stream = TcpStream::connect("127.0.0.1:34254").await?;
111125
/// let mut buffer = BufWriter::with_capacity(100, stream);
126+
/// #
127+
/// # Ok(()) }) }
112128
/// ```
113129
pub fn with_capacity(capacity: usize, inner: W) -> BufWriter<W> {
114130
BufWriter {
@@ -123,13 +139,18 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
123139
/// # Examples
124140
///
125141
/// ```no_run
142+
/// # #![feature(async_await)]
143+
/// # #![allow(unused_mut)]
144+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
126145
/// use async_std::io::BufWriter;
127146
/// use async_std::net::TcpStream;
128147
///
129-
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
148+
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").await?);
130149
///
131-
/// // we can use reference just like buffer
150+
/// // We can use reference just like buffer
132151
/// let reference = buffer.get_ref();
152+
/// #
153+
/// # Ok(()) }) }
133154
/// ```
134155
pub fn get_ref(&self) -> &W {
135156
&self.inner
@@ -142,13 +163,17 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
142163
/// # Examples
143164
///
144165
/// ```no_run
166+
/// # #![feature(async_await)]
167+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
145168
/// use async_std::io::BufWriter;
146169
/// use async_std::net::TcpStream;
147170
///
148-
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
171+
/// let mut buffer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").await?);
149172
///
150-
/// // we can use reference just like buffer
173+
/// // We can use reference just like buffer
151174
/// let reference = buffer.get_mut();
175+
/// #
176+
/// # Ok(()) }) }
152177
/// ```
153178
pub fn get_mut(&mut self) -> &mut W {
154179
&mut self.inner
@@ -177,13 +202,17 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
177202
/// # Examples
178203
///
179204
/// ```no_run
205+
/// # #![feature(async_await)]
206+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
180207
/// use async_std::io::BufWriter;
181208
/// use async_std::net::TcpStream;
182209
///
183-
/// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());
210+
/// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34251").await?);
184211
///
185212
/// // See how many bytes are currently buffered
186213
/// let bytes_buffered = buf_writer.buffer().len();
214+
/// #
215+
/// # Ok(()) }) }
187216
/// ```
188217
pub fn buffer(&self) -> &[u8] {
189218
&self.buf
@@ -276,16 +305,15 @@ impl<W: AsyncWrite + Unpin> LineWriter<W> {
276305
/// # Examples
277306
///
278307
/// ```no_run
308+
/// # #![feature(async_await)]
309+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
279310
/// use async_std::fs::File;
280311
/// use async_std::io::LineWriter;
281312
///
282-
/// fn main() -> std::io::Result<()> {
283-
/// async_std::task::block_on(async {
284-
/// let file = File::create("poem.txt").await?;
285-
/// let file = LineWriter::new(file);
286-
/// Ok(())
287-
/// })
288-
/// }
313+
/// let file = File::create("poem.txt").await?;
314+
/// let file = LineWriter::new(file);
315+
/// #
316+
/// # Ok(()) }) }
289317
/// ```
290318
pub fn new(inner: W) -> LineWriter<W> {
291319
// Lines typically aren't that long, don't use a giant buffer
@@ -298,16 +326,15 @@ impl<W: AsyncWrite + Unpin> LineWriter<W> {
298326
/// # Examples
299327
///
300328
/// ```no_run
329+
/// # #![feature(async_await)]
330+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
301331
/// use async_std::fs::File;
302332
/// use async_std::io::LineWriter;
303333
///
304-
/// fn main() -> std::io::Result<()> {
305-
/// async_std::task::block_on(async {
306-
/// let file = File::create("poem.txt").await?;
307-
/// let file = LineWriter::with_capacity(100, file);
308-
/// Ok(())
309-
/// })
310-
/// }
334+
/// let file = File::create("poem.txt").await?;
335+
/// let file = LineWriter::with_capacity(100, file);
336+
/// #
337+
/// # Ok(()) }) }
311338
/// ```
312339
pub fn with_capacity(capacity: usize, inner: W) -> LineWriter<W> {
313340
LineWriter {
@@ -342,7 +369,7 @@ impl<W: AsyncWrite + Unpin> AsyncWrite for LineWriter<W> {
342369

343370
let n = ready!(self.as_mut().inner().as_mut().poll_write(cx, &buf[..=i])?);
344371
*self.as_mut().need_flush() = true;
345-
if ready!(self.as_mut().poll_flush(cx)).is_err() || n != 1 + 1 {
372+
if ready!(self.as_mut().poll_flush(cx)).is_err() || n != i + 1 {
346373
return Poll::Ready(Ok(n))
347374
}
348375
match ready!(self.inner().poll_write(cx, &buf[i + 1..])) {
@@ -389,13 +416,11 @@ mod tests {
389416
assert_eq!(*writer.get_ref(), []);
390417
writer.flush().await.unwrap();
391418
assert_eq!(*writer.get_ref(), [0, 1]);
392-
writer.write(&[0, b'\n', 1, b'\n']).await.unwrap();
419+
writer.write(&[0, b'\n', 1, b'\n', 2]).await.unwrap();
393420
assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n']);
394421
writer.flush().await.unwrap();
395-
//assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2]);
396-
println!("{:?}", *writer.get_ref());
422+
assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2]);
397423
writer.write(&[3, b'\n']).await.unwrap();
398-
println!("{:?}", *writer.get_ref());
399424
assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2, 3, b'\n']);
400425
})
401426
}

0 commit comments

Comments
 (0)