Sha256: cb2466dcd7ea8bb1f07d00c03e66ed55abf71fe4be6937adc9f533ef9d99fb2d

Contents?: true

Size: 1.72 KB

Versions: 27

Compression:

Stored size: 1.72 KB

Contents

use super::{copy_buf, BufReader, CopyBuf};
use futures_core::future::Future;
use futures_core::task::{Context, Poll};
use futures_io::{AsyncRead, AsyncWrite};
use pin_project_lite::pin_project;
use std::io;
use std::pin::Pin;

/// 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
///
/// ```
/// # futures::executor::block_on(async {
/// 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]);
/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
/// ```
pub fn copy<R, W>(reader: R, writer: &mut W) -> Copy<'_, R, W>
where
    R: AsyncRead,
    W: AsyncWrite + Unpin + ?Sized,
{
    Copy { inner: copy_buf(BufReader::new(reader), writer) }
}

pin_project! {
    /// Future for the [`copy()`] function.
    #[derive(Debug)]
    #[must_use = "futures do nothing unless you `.await` or poll them"]
    pub struct Copy<'a, R, W: ?Sized> {
        #[pin]
        inner: CopyBuf<'a, BufReader<R>, W>,
    }
}

impl<R: AsyncRead, W: AsyncWrite + Unpin + ?Sized> Future for Copy<'_, R, W> {
    type Output = io::Result<u64>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        self.project().inner.poll(cx)
    }
}

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
wasmtime-30.0.2 ./ext/cargo-vendor/futures-util-0.3.31/src/io/copy.rs
wasmtime-29.0.0 ./ext/cargo-vendor/futures-util-0.3.31/src/io/copy.rs
wasmtime-28.0.0 ./ext/cargo-vendor/futures-util-0.3.31/src/io/copy.rs
wasmtime-27.0.0 ./ext/cargo-vendor/futures-util-0.3.31/src/io/copy.rs
wasmtime-26.0.0 ./ext/cargo-vendor/futures-util-0.3.31/src/io/copy.rs
wasmtime-25.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs
wasmtime-25.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs
wasmtime-25.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs
wasmtime-24.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs
wasmtime-23.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs
wasmtime-22.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs
wasmtime-21.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs
wasmtime-20.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs
wasmtime-20.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs
wasmtime-18.0.3 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs
wasmtime-17.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs
wasmtime-17.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs
wasmtime-16.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs
wasmtime-15.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs
wasmtime-15.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/copy.rs