Sha256: 223725d828071e923f25d2d49a0f6e470c411a6d9ba225700f2dd8d5793601bb

Contents?: true

Size: 1.96 KB

Versions: 39

Compression:

Stored size: 1.96 KB

Contents

use crate::io::AsyncBufRead;

use pin_project_lite::pin_project;
use std::future::Future;
use std::io;
use std::marker::PhantomPinned;
use std::pin::Pin;
use std::task::{Context, Poll};

pin_project! {
    /// Future for the [`fill_buf`](crate::io::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>,
        #[pin]
        _pin: PhantomPinned,
    }
}

pub(crate) fn fill_buf<R>(reader: &mut R) -> FillBuf<'_, R>
where
    R: AsyncBufRead + ?Sized + Unpin,
{
    FillBuf {
        reader: Some(reader),
        _pin: PhantomPinned,
    }
}

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

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let me = self.project();

        let reader = me.reader.take().expect("Polled after completion.");
        match Pin::new(&mut *reader).poll_fill_buf(cx) {
            Poll::Ready(Ok(slice)) => unsafe {
                // Safety: This is necessary only due to a limitation in the
                // borrow checker. Once Rust starts using the polonius borrow
                // checker, this can be simplified.
                //
                // The safety of this transmute relies on the fact that the
                // value of `reader` is `None` when we return in this branch.
                // Otherwise the caller could poll us again after
                // completion, and access the mutable reference while the
                // returned immutable reference still exists.
                let slice = std::mem::transmute::<&[u8], &'a [u8]>(slice);
                Poll::Ready(Ok(slice))
            },
            Poll::Ready(Err(err)) => Poll::Ready(Err(err)),
            Poll::Pending => {
                *me.reader = Some(reader);
                Poll::Pending
            }
        }
    }
}

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
wasmtime-30.0.2 ./ext/cargo-vendor/tokio-1.43.0/src/io/util/fill_buf.rs
wasmtime-29.0.0 ./ext/cargo-vendor/tokio-1.43.0/src/io/util/fill_buf.rs
wasmtime-28.0.0 ./ext/cargo-vendor/tokio-1.43.0/src/io/util/fill_buf.rs
wasmtime-27.0.0 ./ext/cargo-vendor/tokio-1.41.1/src/io/util/fill_buf.rs
wasmtime-26.0.0 ./ext/cargo-vendor/tokio-1.41.0/src/io/util/fill_buf.rs
wasmtime-25.0.2 ./ext/cargo-vendor/tokio-1.40.0/src/io/util/fill_buf.rs
wasmtime-25.0.1 ./ext/cargo-vendor/tokio-1.39.3/src/io/util/fill_buf.rs
wasmtime-25.0.0 ./ext/cargo-vendor/tokio-1.39.3/src/io/util/fill_buf.rs
wasmtime-24.0.0 ./ext/cargo-vendor/tokio-1.39.3/src/io/util/fill_buf.rs
wasmtime-23.0.2 ./ext/cargo-vendor/tokio-1.36.0/src/io/util/fill_buf.rs
wasmtime-22.0.0 ./ext/cargo-vendor/tokio-1.36.0/src/io/util/fill_buf.rs
wasmtime-21.0.1 ./ext/cargo-vendor/tokio-1.36.0/src/io/util/fill_buf.rs
wasmtime-20.0.2 ./ext/cargo-vendor/tokio-1.36.0/src/io/util/fill_buf.rs
wasmtime-20.0.0 ./ext/cargo-vendor/tokio-1.36.0/src/io/util/fill_buf.rs
wasmtime-18.0.3 ./ext/cargo-vendor/tokio-1.36.0/src/io/util/fill_buf.rs
wasmtime-17.0.1 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/fill_buf.rs
wasmtime-17.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/fill_buf.rs
wasmtime-16.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/fill_buf.rs
wasmtime-15.0.1 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/fill_buf.rs
wasmtime-15.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/fill_buf.rs