Function futures::io::copy_buf_abortable

source ·
pub fn copy_buf_abortable<R, W>(
    reader: R,
    writer: &mut W
) -> (CopyBufAbortable<'_, R, W>, AbortHandle)where
    R: AsyncBufRead,
    W: AsyncWrite + Unpin + ?Sized,
Expand description

Creates a future which copies all the bytes from one object to another, with its AbortHandle.

The returned future will copy all the bytes read from this AsyncBufRead into the writer specified. This future will only complete once abort has been requested or 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. If aborted, Aborted is returned. Otherwise, the underlying error is returned.

Examples

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

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

let (fut, abort_handle) = io::copy_buf_abortable(reader, &mut writer);
let bytes = fut.await;
abort_handle.abort();
writer.close().await.unwrap();
match bytes {
    Ok(Ok(n)) => {
        assert_eq!(n, 4);
        assert_eq!(writer.into_inner(), [1, 2, 3, 4, 0]);
        Ok(n)
    },
    Ok(Err(a)) => {
        Err::<u64, Aborted>(a)
    }
    Err(e) => panic!("{}", e)
}