Sha256: a3623e037db59efab227a769698098597f1b531a75b6c045e75f068c0137c738

Contents?: true

Size: 1.52 KB

Versions: 20

Compression:

Stored size: 1.52 KB

Contents

use futures_core::future::Future;
use futures_core::task::{Context, Poll};
use futures_io::AsyncBufRead;
use std::io;
use std::pin::Pin;
use std::slice;

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

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

impl<'a, R: AsyncBufRead + ?Sized + Unpin> FillBuf<'a, R> {
    pub(super) fn new(reader: &'a mut R) -> Self {
        Self { reader: Some(reader) }
    }
}

impl<'a, R> Future for FillBuf<'a, R>
where
    R: AsyncBufRead + ?Sized + Unpin,
{
    type Output = io::Result<&'a [u8]>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = &mut *self;
        let reader = this.reader.take().expect("Polled FillBuf after completion");

        match Pin::new(&mut *reader).poll_fill_buf(cx) {
            Poll::Ready(Ok(slice)) => {
                // With polonius it is possible to remove this lifetime transmutation and just have
                // the correct lifetime of the reference inferred based on which branch is taken
                let slice: &'a [u8] = unsafe { slice::from_raw_parts(slice.as_ptr(), slice.len()) };
                Poll::Ready(Ok(slice))
            }
            Poll::Ready(Err(err)) => Poll::Ready(Err(err)),
            Poll::Pending => {
                this.reader = Some(reader);
                Poll::Pending
            }
        }
    }
}

Version data entries

20 entries across 20 versions & 1 rubygems

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