@@ -31,6 +31,9 @@ pub use self::buf_writer::BufWriter;
31
31
mod copy_into;
32
32
pub use self :: copy_into:: CopyInto ;
33
33
34
+ mod copy_buf_into;
35
+ pub use self :: copy_buf_into:: CopyBufInto ;
36
+
34
37
mod flush;
35
38
pub use self :: flush:: Flush ;
36
39
@@ -404,6 +407,36 @@ impl<S: AsyncSeek + ?Sized> AsyncSeekExt for S {}
404
407
405
408
/// An extension trait which adds utility methods to `AsyncBufRead` types.
406
409
pub trait AsyncBufReadExt : AsyncBufRead {
410
+ /// Creates a future which copies all the bytes from one object to another.
411
+ ///
412
+ /// The returned future will copy all the bytes read from this `AsyncBufRead` into the
413
+ /// `writer` specified. This future will only complete once the `reader` has hit
414
+ /// EOF and all bytes have been written to and flushed from the `writer`
415
+ /// provided.
416
+ ///
417
+ /// On success the number of bytes is returned.
418
+ ///
419
+ /// # Examples
420
+ ///
421
+ /// ```
422
+ /// #![feature(async_await)]
423
+ /// # futures::executor::block_on(async {
424
+ /// use futures::io::AsyncBufReadExt;
425
+ /// use std::io::Cursor;
426
+ ///
427
+ /// let mut reader = Cursor::new([1, 2, 3, 4]);
428
+ /// let mut writer = Cursor::new([0u8; 5]);
429
+ ///
430
+ /// let bytes = reader.copy_buf_into(&mut writer).await?;
431
+ ///
432
+ /// assert_eq!(bytes, 4);
433
+ /// assert_eq!(writer.into_inner(), [1, 2, 3, 4, 0]);
434
+ /// # Ok::<(), Box<std::error::Error>>(()) }).unwrap();
435
+ /// ```
436
+ fn copy_buf_into < W : AsyncWrite > ( self , writer : W ) -> CopyBufInto < Self , W > where Self : Sized {
437
+ CopyBufInto :: new ( self , writer)
438
+ }
439
+
407
440
/// Creates a future which will read all the bytes associated with this I/O
408
441
/// object into `buf` until the delimiter `byte` or EOF is reached.
409
442
/// This method is the async equivalent to [`BufRead::read_until`](std::io::BufRead::read_until).
0 commit comments