Sha256: ab51d6174de24cbb729ce77dbaeea27e16059b8253e4830d8243ec5f08a08a8c

Contents?: true

Size: 1.29 KB

Versions: 33

Compression:

Stored size: 1.29 KB

Contents

use crate::io::AsyncWrite;

use bytes::Buf;
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! {
    /// A future to write some of the buffer to an `AsyncWrite`.
    #[derive(Debug)]
    #[must_use = "futures do nothing unless you `.await` or poll them"]
    pub struct WriteBuf<'a, W, B> {
        writer: &'a mut W,
        buf: &'a mut B,
        #[pin]
        _pin: PhantomPinned,
    }
}

/// Tries to write some bytes from the given `buf` to the writer in an
/// asynchronous manner, returning a future.
pub(crate) fn write_buf<'a, W, B>(writer: &'a mut W, buf: &'a mut B) -> WriteBuf<'a, W, B>
where
    W: AsyncWrite + Unpin,
    B: Buf,
{
    WriteBuf {
        writer,
        buf,
        _pin: PhantomPinned,
    }
}

impl<W, B> Future for WriteBuf<'_, W, B>
where
    W: AsyncWrite + Unpin,
    B: Buf,
{
    type Output = io::Result<usize>;

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

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

        let n = ready!(Pin::new(me.writer).poll_write(cx, me.buf.chunk()))?;
        me.buf.advance(n);
        Poll::Ready(Ok(n))
    }
}

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
wasmtime-25.0.1 ./ext/cargo-vendor/tokio-1.39.3/src/io/util/write_buf.rs
wasmtime-25.0.0 ./ext/cargo-vendor/tokio-1.39.3/src/io/util/write_buf.rs
wasmtime-24.0.0 ./ext/cargo-vendor/tokio-1.39.3/src/io/util/write_buf.rs
wasmtime-23.0.2 ./ext/cargo-vendor/tokio-1.36.0/src/io/util/write_buf.rs
wasmtime-22.0.0 ./ext/cargo-vendor/tokio-1.36.0/src/io/util/write_buf.rs
wasmtime-21.0.1 ./ext/cargo-vendor/tokio-1.36.0/src/io/util/write_buf.rs
wasmtime-20.0.2 ./ext/cargo-vendor/tokio-1.36.0/src/io/util/write_buf.rs
wasmtime-20.0.0 ./ext/cargo-vendor/tokio-1.36.0/src/io/util/write_buf.rs
wasmtime-18.0.3 ./ext/cargo-vendor/tokio-1.36.0/src/io/util/write_buf.rs
wasmtime-17.0.1 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/write_buf.rs
wasmtime-17.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/write_buf.rs
wasmtime-16.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/write_buf.rs
wasmtime-15.0.1 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/write_buf.rs
wasmtime-15.0.0 ./ext/cargo-vendor/tokio-1.35.1/src/io/util/write_buf.rs
wasmtime-14.0.4 ./ext/cargo-vendor/tokio-1.33.0/src/io/util/write_buf.rs
wasmtime-14.0.3 ./ext/cargo-vendor/tokio-1.33.0/src/io/util/write_buf.rs
wasmtime-14.0.1 ./ext/cargo-vendor/tokio-1.33.0/src/io/util/write_buf.rs
wasmtime-14.0.0 ./ext/cargo-vendor/tokio-1.33.0/src/io/util/write_buf.rs
wasmtime-13.0.0 ./ext/cargo-vendor/tokio-1.32.0/src/io/util/write_buf.rs
wasmtime-12.0.1 ./ext/cargo-vendor/tokio-1.32.0/src/io/util/write_buf.rs