@@ -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
@@ -441,6 +444,40 @@ impl<S: AsyncSeek + ?Sized> AsyncSeekExt for S {}
441
444
442
445
/// An extension trait which adds utility methods to `AsyncBufRead` types.
443
446
pub trait AsyncBufReadExt : AsyncBufRead {
447
+ /// Creates a future which copies all the bytes from one object to another.
448
+ ///
449
+ /// The returned future will copy all the bytes read from this `AsyncBufRead` into the
450
+ /// `writer` specified. This future will only complete once the `reader` has hit
451
+ /// EOF and all bytes have been written to and flushed from the `writer`
452
+ /// provided.
453
+ ///
454
+ /// On success the number of bytes is returned.
455
+ ///
456
+ /// Note that this method consumes `writer` but does not close it, you will likely want to pass
457
+ /// it by reference as shown in the example.
458
+ ///
459
+ /// # Examples
460
+ ///
461
+ /// ```
462
+ /// #![feature(async_await)]
463
+ /// # futures::executor::block_on(async {
464
+ /// use futures::io::{AsyncBufReadExt, AsyncWriteExt};
465
+ /// use std::io::Cursor;
466
+ ///
467
+ /// let reader = Cursor::new([1, 2, 3, 4]);
468
+ /// let mut writer = Cursor::new([0u8; 5]);
469
+ ///
470
+ /// let bytes = reader.copy_buf_into(&mut writer).await?;
471
+ /// writer.close().await?;
472
+ ///
473
+ /// assert_eq!(bytes, 4);
474
+ /// assert_eq!(writer.into_inner(), [1, 2, 3, 4, 0]);
475
+ /// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
476
+ /// ```
477
+ fn copy_buf_into < W : AsyncWrite > ( self , writer : W ) -> CopyBufInto < Self , W > where Self : Sized {
478
+ CopyBufInto :: new ( self , writer)
479
+ }
480
+
444
481
/// Creates a future which will read all the bytes associated with this I/O
445
482
/// object into `buf` until the delimiter `byte` or EOF is reached.
446
483
/// This method is the async equivalent to [`BufRead::read_until`](std::io::BufRead::read_until).
0 commit comments