futures::io

Function copy

Source
pub fn copy<R, W>(reader: R, writer: &mut W) -> Copy<'_, R, W> 
where R: AsyncRead, W: AsyncWrite + Unpin + ?Sized,
Expand description

Creates a future which copies all the bytes from one object to another.

The returned future will copy all the bytes read from this AsyncRead into the writer specified. This future will only complete once the reader has hit EOF and all bytes have been written to and flushed from the writer provided.

On success the number of bytes is returned.

§Examples

use futures::io::{self, AsyncWriteExt, Cursor};

let reader = Cursor::new([1, 2, 3, 4]);
let mut writer = Cursor::new(vec![0u8; 5]);

let bytes = io::copy(reader, &mut writer).await?;
writer.close().await?;

assert_eq!(bytes, 4);
assert_eq!(writer.into_inner(), [1, 2, 3, 4, 0]);