@@ -55,6 +55,27 @@ pub trait AsyncReadExt: AsyncRead {
55
55
/// provided.
56
56
///
57
57
/// On success the number of bytes is returned.
58
+ ///
59
+ /// # Examples
60
+ ///
61
+ /// ```
62
+ /// #![feature(async_await, await_macro, futures_api)]
63
+ /// # futures::executor::block_on(async {
64
+ /// use futures::io::AsyncReadExt;
65
+ /// use std::io::Cursor;
66
+ ///
67
+ /// let mut reader = Cursor::new([1, 2, 3, 4]);
68
+ /// let mut output = [0u8; 5];
69
+ ///
70
+ /// let bytes = {
71
+ /// let mut writer = Cursor::new(&mut output[..]);
72
+ /// await!(reader.copy_into(&mut writer))?
73
+ /// };
74
+ ///
75
+ /// assert_eq!(bytes, 4);
76
+ /// assert_eq!(output, [1, 2, 3, 4, 0]);
77
+ /// # Ok::<(), Box<std::error::Error>>(()) }).unwrap();
78
+ /// ```
58
79
fn copy_into < ' a , W > (
59
80
& ' a mut self ,
60
81
writer : & ' a mut W ,
@@ -69,17 +90,72 @@ pub trait AsyncReadExt: AsyncRead {
69
90
///
70
91
/// The returned future will resolve to the number of bytes read once the read
71
92
/// operation is completed.
93
+ ///
94
+ /// # Examples
95
+ ///
96
+ /// ```
97
+ /// #![feature(async_await, await_macro, futures_api)]
98
+ /// # futures::executor::block_on(async {
99
+ /// use futures::io::AsyncReadExt;
100
+ /// use std::io::Cursor;
101
+ ///
102
+ /// let mut reader = Cursor::new([1, 2, 3, 4]);
103
+ /// let mut output = [0u8; 5];
104
+ ///
105
+ /// let bytes = await!(reader.read(&mut output[..]))?;
106
+ ///
107
+ /// // This is only guaranteed to be 4 because `&[u8]` is a synchronous
108
+ /// // reader. In a real system you could get anywhere from 1 to
109
+ /// // `output.len()` bytes in a single read.
110
+ /// assert_eq!(bytes, 4);
111
+ /// assert_eq!(output, [1, 2, 3, 4, 0]);
112
+ /// # Ok::<(), Box<std::error::Error>>(()) }).unwrap();
113
+ /// ```
72
114
fn read < ' a > ( & ' a mut self , buf : & ' a mut [ u8 ] ) -> Read < ' a , Self > {
73
115
Read :: new ( self , buf)
74
116
}
75
117
76
118
/// Creates a future which will read exactly enough bytes to fill `buf`,
77
- /// returning an error if EOF is hit sooner.
119
+ /// returning an error if end of file ( EOF) is hit sooner.
78
120
///
79
121
/// The returned future will resolve once the read operation is completed.
80
122
///
81
123
/// In the case of an error the buffer and the object will be discarded, with
82
124
/// the error yielded.
125
+ ///
126
+ /// # Examples
127
+ ///
128
+ /// ```
129
+ /// #![feature(async_await, await_macro, futures_api)]
130
+ /// # futures::executor::block_on(async {
131
+ /// use futures::io::AsyncReadExt;
132
+ /// use std::io::Cursor;
133
+ ///
134
+ /// let mut reader = Cursor::new([1, 2, 3, 4]);
135
+ /// let mut output = [0u8; 4];
136
+ ///
137
+ /// await!(reader.read_exact(&mut output))?;
138
+ ///
139
+ /// assert_eq!(output, [1, 2, 3, 4]);
140
+ /// # Ok::<(), Box<std::error::Error>>(()) }).unwrap();
141
+ /// ```
142
+ ///
143
+ /// ## EOF is hit before `buf` is filled
144
+ ///
145
+ /// ```
146
+ /// #![feature(async_await, await_macro, futures_api)]
147
+ /// # futures::executor::block_on(async {
148
+ /// use futures::io::AsyncReadExt;
149
+ /// use std::io::{self, Cursor};
150
+ ///
151
+ /// let mut reader = Cursor::new([1, 2, 3, 4]);
152
+ /// let mut output = [0u8; 5];
153
+ ///
154
+ /// let result = await!(reader.read_exact(&mut output));
155
+ ///
156
+ /// assert_eq!(result.unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
157
+ /// # });
158
+ /// ```
83
159
fn read_exact < ' a > (
84
160
& ' a mut self ,
85
161
buf : & ' a mut [ u8 ] ,
@@ -88,6 +164,23 @@ pub trait AsyncReadExt: AsyncRead {
88
164
}
89
165
90
166
/// Creates a future which will read all the bytes from this `AsyncRead`.
167
+ ///
168
+ /// # Examples
169
+ ///
170
+ /// ```
171
+ /// #![feature(async_await, await_macro, futures_api)]
172
+ /// # futures::executor::block_on(async {
173
+ /// use futures::io::AsyncReadExt;
174
+ /// use std::io::Cursor;
175
+ ///
176
+ /// let mut reader = Cursor::new([1, 2, 3, 4]);
177
+ /// let mut output = Vec::with_capacity(4);
178
+ ///
179
+ /// await!(reader.read_to_end(&mut output))?;
180
+ ///
181
+ /// assert_eq!(output, vec![1, 2, 3, 4]);
182
+ /// # Ok::<(), Box<std::error::Error>>(()) }).unwrap();
183
+ /// ```
91
184
fn read_to_end < ' a > (
92
185
& ' a mut self ,
93
186
buf : & ' a mut Vec < u8 > ,
@@ -97,8 +190,36 @@ pub trait AsyncReadExt: AsyncRead {
97
190
98
191
/// Helper method for splitting this read/write object into two halves.
99
192
///
100
- /// The two halves returned implement the `Read` and `Write` traits,
101
- /// respectively.
193
+ /// The two halves returned implement the `AsyncRead` and `AsyncWrite`
194
+ /// traits, respectively.
195
+ ///
196
+ /// # Examples
197
+ ///
198
+ /// ```
199
+ /// #![feature(async_await, await_macro, futures_api)]
200
+ /// # futures::executor::block_on(async {
201
+ /// use futures::io::AsyncReadExt;
202
+ /// use std::io::Cursor;
203
+ ///
204
+ /// let mut reader = Cursor::new([1, 2, 3, 4]);
205
+ /// let mut buffer = [0, 0, 0, 0, 5, 6, 7, 8];
206
+ /// let mut output = [0u8; 5];
207
+ ///
208
+ /// {
209
+ /// let mut writer = Cursor::new(&mut output[..]);
210
+ /// // Note that for `Cursor` the read and write halves share a single
211
+ /// // seek position. This may or may not be true for other types that
212
+ /// // implement both `AsyncRead` and `AsyncWrite`.
213
+ /// let buffer_cursor = Cursor::new(&mut buffer[..]);
214
+ /// let (mut buffer_reader, mut buffer_writer) = buffer_cursor.split();
215
+ /// await!(reader.copy_into(&mut buffer_writer))?;
216
+ /// await!(buffer_reader.copy_into(&mut writer))?;
217
+ /// }
218
+ ///
219
+ /// assert_eq!(buffer, [1, 2, 3, 4, 5, 6, 7, 8]);
220
+ /// assert_eq!(output, [5, 6, 7, 8, 0]);
221
+ /// # Ok::<(), Box<std::error::Error>>(()) }).unwrap();
222
+ /// ```
102
223
fn split ( self ) -> ( ReadHalf < Self > , WriteHalf < Self > )
103
224
where Self : AsyncWrite + Sized ,
104
225
{
@@ -111,6 +232,28 @@ impl<R: AsyncRead + ?Sized> AsyncReadExt for R {}
111
232
/// An extension trait which adds utility methods to `AsyncWrite` types.
112
233
pub trait AsyncWriteExt : AsyncWrite {
113
234
/// Creates a future which will entirely flush this `AsyncWrite`.
235
+ ///
236
+ /// # Examples
237
+ ///
238
+ /// ```
239
+ /// #![feature(async_await, await_macro, futures_api)]
240
+ /// # futures::executor::block_on(async {
241
+ /// use futures::io::{AllowStdIo, AsyncWriteExt};
242
+ /// use std::io::{BufWriter, Cursor};
243
+ ///
244
+ /// let mut output = [0u8; 5];
245
+ ///
246
+ /// {
247
+ /// let mut writer = Cursor::new(&mut output[..]);
248
+ /// let mut buffered = AllowStdIo::new(BufWriter::new(writer));
249
+ /// await!(buffered.write_all(&[1, 2]))?;
250
+ /// await!(buffered.write_all(&[3, 4]))?;
251
+ /// await!(buffered.flush())?;
252
+ /// }
253
+ ///
254
+ /// assert_eq!(output, [1, 2, 3, 4, 0]);
255
+ /// # Ok::<(), Box<std::error::Error>>(()) }).unwrap();
256
+ /// ```
114
257
fn flush < ' a > ( & ' a mut self ) -> Flush < ' a , Self > {
115
258
Flush :: new ( self )
116
259
}
@@ -126,6 +269,25 @@ pub trait AsyncWriteExt: AsyncWrite {
126
269
/// this `AsyncWrite`.
127
270
///
128
271
/// The returned future will not complete until all the data has been written.
272
+ ///
273
+ /// # Examples
274
+ ///
275
+ /// ```
276
+ /// #![feature(async_await, await_macro, futures_api)]
277
+ /// # futures::executor::block_on(async {
278
+ /// use futures::io::AsyncWriteExt;
279
+ /// use std::io::Cursor;
280
+ ///
281
+ /// let mut output = [0u8; 5];
282
+ ///
283
+ /// {
284
+ /// let mut writer = Cursor::new(&mut output[..]);
285
+ /// await!(writer.write_all(&[1, 2, 3, 4]))?;
286
+ /// }
287
+ ///
288
+ /// assert_eq!(output, [1, 2, 3, 4, 0]);
289
+ /// # Ok::<(), Box<std::error::Error>>(()) }).unwrap();
290
+ /// ```
129
291
fn write_all < ' a > ( & ' a mut self , buf : & ' a [ u8 ] ) -> WriteAll < ' a , Self > {
130
292
WriteAll :: new ( self , buf)
131
293
}
0 commit comments