Sha256: ddebd58db9f6766efa3f50543fb51b138538533921e1ee1da4621fff9c64efe2

Contents?: true

Size: 1.24 KB

Versions: 27

Compression:

Stored size: 1.24 KB

Contents

use crate::io::AsyncRead;
use futures_core::future::Future;
use futures_core::ready;
use futures_core::task::{Context, Poll};
use std::io;
use std::mem;
use std::pin::Pin;

/// Future for the [`read_exact`](super::AsyncReadExt::read_exact) method.
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct ReadExact<'a, R: ?Sized> {
    reader: &'a mut R,
    buf: &'a mut [u8],
}

impl<R: ?Sized + Unpin> Unpin for ReadExact<'_, R> {}

impl<'a, R: AsyncRead + ?Sized + Unpin> ReadExact<'a, R> {
    pub(super) fn new(reader: &'a mut R, buf: &'a mut [u8]) -> Self {
        Self { reader, buf }
    }
}

impl<R: AsyncRead + ?Sized + Unpin> Future for ReadExact<'_, R> {
    type Output = io::Result<()>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = &mut *self;
        while !this.buf.is_empty() {
            let n = ready!(Pin::new(&mut this.reader).poll_read(cx, this.buf))?;
            {
                let (_, rest) = mem::take(&mut this.buf).split_at_mut(n);
                this.buf = rest;
            }
            if n == 0 {
                return Poll::Ready(Err(io::ErrorKind::UnexpectedEof.into()));
            }
        }
        Poll::Ready(Ok(()))
    }
}

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/read_exact.rs
wasmtime-29.0.0 ./ext/cargo-vendor/futures-util-0.3.31/src/io/read_exact.rs
wasmtime-28.0.0 ./ext/cargo-vendor/futures-util-0.3.31/src/io/read_exact.rs
wasmtime-27.0.0 ./ext/cargo-vendor/futures-util-0.3.31/src/io/read_exact.rs
wasmtime-26.0.0 ./ext/cargo-vendor/futures-util-0.3.31/src/io/read_exact.rs
wasmtime-25.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs
wasmtime-25.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs
wasmtime-25.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs
wasmtime-24.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs
wasmtime-23.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs
wasmtime-22.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs
wasmtime-21.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs
wasmtime-20.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs
wasmtime-20.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs
wasmtime-18.0.3 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs
wasmtime-17.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs
wasmtime-17.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs
wasmtime-16.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs
wasmtime-15.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs
wasmtime-15.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_exact.rs