@@ -34,34 +34,40 @@ const DEFAULT_CAPACITY: usize = 8 * 1024;
34
34
///
35
35
/// Let's write the numbers one through ten to a [`TcpStream`]:
36
36
///
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
+ /// ```
47
52
///
48
53
/// Because we're not buffering, we write each one in turn, incurring the
49
54
/// overhead of a system call per byte written. We can fix this with a
50
55
/// `BufWriter`:
51
56
///
52
57
/// ```no_run
53
- /// use async_std::io::prelude::*;
58
+ /// # #![feature(async_await)]
59
+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
54
60
/// use async_std::io::BufWriter;
55
61
/// use async_std::net::TcpStream;
56
- /// use futures::AsyncWrite;
57
62
/// use async_std::io::Write;
58
63
///
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(()) }) }
65
71
/// ```
66
72
///
67
73
/// By wrapping the stream with a `BufWriter`, these ten writes are all grouped
@@ -88,10 +94,15 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
88
94
/// # Examples
89
95
///
90
96
/// ```no_run
97
+ /// # #![feature(async_await)]
98
+ /// # #![allow(unused_mut)]
99
+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
91
100
/// use async_std::io::BufWriter;
92
101
/// use async_std::net::TcpStream;
93
102
///
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(()) }) }
95
106
/// ```
96
107
pub fn new ( inner : W ) -> BufWriter < W > {
97
108
BufWriter :: with_capacity ( DEFAULT_CAPACITY , inner)
@@ -104,11 +115,16 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
104
115
/// Creating a buffer with a buffer of a hundred bytes.
105
116
///
106
117
/// ```no_run
118
+ /// # #![feature(async_await)]
119
+ /// # #![allow(unused_mut)]
120
+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
107
121
/// use async_std::io::BufWriter;
108
122
/// use async_std::net::TcpStream;
109
123
///
110
- /// let stream = TcpStream::connect("127.0.0.1:34254").unwrap() ;
124
+ /// let stream = TcpStream::connect("127.0.0.1:34254").await? ;
111
125
/// let mut buffer = BufWriter::with_capacity(100, stream);
126
+ /// #
127
+ /// # Ok(()) }) }
112
128
/// ```
113
129
pub fn with_capacity ( capacity : usize , inner : W ) -> BufWriter < W > {
114
130
BufWriter {
@@ -123,13 +139,18 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
123
139
/// # Examples
124
140
///
125
141
/// ```no_run
142
+ /// # #![feature(async_await)]
143
+ /// # #![allow(unused_mut)]
144
+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
126
145
/// use async_std::io::BufWriter;
127
146
/// use async_std::net::TcpStream;
128
147
///
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? );
130
149
///
131
- /// // we can use reference just like buffer
150
+ /// // We can use reference just like buffer
132
151
/// let reference = buffer.get_ref();
152
+ /// #
153
+ /// # Ok(()) }) }
133
154
/// ```
134
155
pub fn get_ref ( & self ) -> & W {
135
156
& self . inner
@@ -142,13 +163,17 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
142
163
/// # Examples
143
164
///
144
165
/// ```no_run
166
+ /// # #![feature(async_await)]
167
+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
145
168
/// use async_std::io::BufWriter;
146
169
/// use async_std::net::TcpStream;
147
170
///
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? );
149
172
///
150
- /// // we can use reference just like buffer
173
+ /// // We can use reference just like buffer
151
174
/// let reference = buffer.get_mut();
175
+ /// #
176
+ /// # Ok(()) }) }
152
177
/// ```
153
178
pub fn get_mut ( & mut self ) -> & mut W {
154
179
& mut self . inner
@@ -177,13 +202,17 @@ impl<W: AsyncWrite + Unpin> BufWriter<W> {
177
202
/// # Examples
178
203
///
179
204
/// ```no_run
205
+ /// # #![feature(async_await)]
206
+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
180
207
/// use async_std::io::BufWriter;
181
208
/// use async_std::net::TcpStream;
182
209
///
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? );
184
211
///
185
212
/// // See how many bytes are currently buffered
186
213
/// let bytes_buffered = buf_writer.buffer().len();
214
+ /// #
215
+ /// # Ok(()) }) }
187
216
/// ```
188
217
pub fn buffer ( & self ) -> & [ u8 ] {
189
218
& self . buf
@@ -276,16 +305,15 @@ impl<W: AsyncWrite + Unpin> LineWriter<W> {
276
305
/// # Examples
277
306
///
278
307
/// ```no_run
308
+ /// # #![feature(async_await)]
309
+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
279
310
/// use async_std::fs::File;
280
311
/// use async_std::io::LineWriter;
281
312
///
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(()) }) }
289
317
/// ```
290
318
pub fn new ( inner : W ) -> LineWriter < W > {
291
319
// Lines typically aren't that long, don't use a giant buffer
@@ -298,16 +326,15 @@ impl<W: AsyncWrite + Unpin> LineWriter<W> {
298
326
/// # Examples
299
327
///
300
328
/// ```no_run
329
+ /// # #![feature(async_await)]
330
+ /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
301
331
/// use async_std::fs::File;
302
332
/// use async_std::io::LineWriter;
303
333
///
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(()) }) }
311
338
/// ```
312
339
pub fn with_capacity ( capacity : usize , inner : W ) -> LineWriter < W > {
313
340
LineWriter {
@@ -342,7 +369,7 @@ impl<W: AsyncWrite + Unpin> AsyncWrite for LineWriter<W> {
342
369
343
370
let n = ready ! ( self . as_mut( ) . inner( ) . as_mut( ) . poll_write( cx, & buf[ ..=i] ) ?) ;
344
371
* 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 {
346
373
return Poll :: Ready ( Ok ( n) )
347
374
}
348
375
match ready ! ( self . inner( ) . poll_write( cx, & buf[ i + 1 ..] ) ) {
@@ -389,13 +416,11 @@ mod tests {
389
416
assert_eq ! ( * writer. get_ref( ) , [ ] ) ;
390
417
writer. flush ( ) . await . unwrap ( ) ;
391
418
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 ( ) ;
393
420
assert_eq ! ( * writer. get_ref( ) , [ 0 , 1 , 0 , b'\n' , 1 , b'\n' ] ) ;
394
421
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 ] ) ;
397
423
writer. write ( & [ 3 , b'\n' ] ) . await . unwrap ( ) ;
398
- println ! ( "{:?}" , * writer. get_ref( ) ) ;
399
424
assert_eq ! ( * writer. get_ref( ) , [ 0 , 1 , 0 , b'\n' , 1 , b'\n' , 2 , 3 , b'\n' ] ) ;
400
425
} )
401
426
}
0 commit comments