Sha256: a87be2d115c09a6782ec8cadeafc92fb1fbe534580e71540087c3298a03bfca2

Contents?: true

Size: 1.82 KB

Versions: 19

Compression:

Stored size: 1.82 KB

Contents

use crate::io::AsyncRead;

use bytes::BufMut;
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};

pub(crate) fn read_buf<'a, R, B>(reader: &'a mut R, buf: &'a mut B) -> ReadBuf<'a, R, B>
where
    R: AsyncRead + Unpin,
    B: BufMut,
{
    ReadBuf {
        reader,
        buf,
        _pin: PhantomPinned,
    }
}

pin_project! {
    /// Future returned by [`read_buf`](crate::io::AsyncReadExt::read_buf).
    #[derive(Debug)]
    #[must_use = "futures do nothing unless you `.await` or poll them"]
    pub struct ReadBuf<'a, R, B> {
        reader: &'a mut R,
        buf: &'a mut B,
        #[pin]
        _pin: PhantomPinned,
    }
}

impl<R, B> Future for ReadBuf<'_, R, B>
where
    R: AsyncRead + Unpin,
    B: BufMut,
{
    type Output = io::Result<usize>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
        use crate::io::ReadBuf;
        use std::mem::MaybeUninit;

        let me = self.project();

        if !me.buf.has_remaining_mut() {
            return Poll::Ready(Ok(0));
        }

        let n = {
            let dst = me.buf.chunk_mut();
            let dst = unsafe { &mut *(dst as *mut _ as *mut [MaybeUninit<u8>]) };
            let mut buf = ReadBuf::uninit(dst);
            let ptr = buf.filled().as_ptr();
            ready!(Pin::new(me.reader).poll_read(cx, &mut buf)?);

            // Ensure the pointer does not change from under us
            assert_eq!(ptr, buf.filled().as_ptr());
            buf.filled().len()
        };

        // Safety: This is guaranteed to be the number of initialized (and read)
        // bytes due to the invariants provided by `ReadBuf::filled`.
        unsafe {
            me.buf.advance_mut(n);
        }

        Poll::Ready(Ok(n))
    }
}

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
wasmtime-14.0.4 ./ext/cargo-vendor/tokio-1.33.0/src/io/util/read_buf.rs
wasmtime-14.0.3 ./ext/cargo-vendor/tokio-1.33.0/src/io/util/read_buf.rs
wasmtime-14.0.1 ./ext/cargo-vendor/tokio-1.33.0/src/io/util/read_buf.rs
wasmtime-14.0.0 ./ext/cargo-vendor/tokio-1.33.0/src/io/util/read_buf.rs
wasmtime-13.0.0 ./ext/cargo-vendor/tokio-1.32.0/src/io/util/read_buf.rs
wasmtime-12.0.1 ./ext/cargo-vendor/tokio-1.32.0/src/io/util/read_buf.rs
wasmtime-12.0.0 ./ext/cargo-vendor/tokio-1.32.0/src/io/util/read_buf.rs
wasmtime-11.0.0 ./ext/cargo-vendor/tokio-1.32.0/src/io/util/read_buf.rs
wasmtime-10.0.1 ./ext/cargo-vendor/tokio-1.30.0/src/io/util/read_buf.rs
wasmtime-10.0.0 ./ext/cargo-vendor/tokio-1.30.0/src/io/util/read_buf.rs
wasmtime-9.0.4 ./ext/cargo-vendor/tokio-1.30.0/src/io/util/read_buf.rs
wasmtime-9.0.1 ./ext/cargo-vendor/tokio-1.28.1/src/io/util/read_buf.rs
wasmtime-8.0.0 ./ext/cargo-vendor/tokio-1.27.0/src/io/util/read_buf.rs
wasmtime-7.0.0 ./ext/cargo-vendor/tokio-1.27.0/src/io/util/read_buf.rs
wasmtime-6.0.1 ./ext/cargo-vendor/tokio-1.25.0/src/io/util/read_buf.rs
wasmtime-6.0.0 ./ext/cargo-vendor/tokio-1.25.0/src/io/util/read_buf.rs
wasmtime-5.0.0 ./ext/cargo-vendor/tokio-1.24.2/src/io/util/read_buf.rs
wasmtime-0.4.1 ./ext/cargo-vendor/tokio-1.23.0/src/io/util/read_buf.rs
wasmtime-0.4.0 ./ext/cargo-vendor/tokio-1.23.0/src/io/util/read_buf.rs