Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions tokio/src/io/util/async_write_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use crate::io::util::write_int::{
WriteU128, WriteU128Le, WriteU16, WriteU16Le, WriteU32, WriteU32Le, WriteU64, WriteU64Le,
WriteU8,
};
use crate::io::util::write_vectored::{write_vectored, WriteVectored};
use crate::io::AsyncWrite;
use std::io::IoSlice;

use bytes::Buf;

Expand Down Expand Up @@ -116,6 +118,47 @@ cfg_io_util! {
write(self, src)
}

/// Like [`write`], except that it writes from a slice of buffers.
///
/// Equivalent to:
///
/// ```ignore
/// async fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize>;
/// ```
///
/// See [`AsyncWrite::poll_write_vectored`] for more details.
///
/// # Examples
///
/// ```no_run
/// use tokio::io::{self, AsyncWriteExt};
/// use tokio::fs::File;
/// use std::io::IoSlice;
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let mut file = File::create("foo.txt").await?;
///
/// let bufs: &[_] = &[
/// IoSlice::new(b"hello"),
/// IoSlice::new(b" "),
/// IoSlice::new(b"world"),
/// ];
///
/// file.write_vectored(&bufs).await?;
///
/// Ok(())
/// }
/// ```
///
/// [`write`]: AsyncWriteExt::write
fn write_vectored<'a, 'b>(&'a mut self, bufs: &'a [IoSlice<'b>]) -> WriteVectored<'a, 'b, Self>
where
Self: Unpin,
{
write_vectored(self, bufs)
}


/// Writes a buffer into this writer, advancing the buffer's internal
/// cursor.
Expand Down
1 change: 1 addition & 0 deletions tokio/src/io/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ cfg_io_util! {
pub use take::Take;

mod write;
mod write_vectored;
mod write_all;
mod write_buf;
mod write_int;
Expand Down
47 changes: 47 additions & 0 deletions tokio/src/io/util/write_vectored.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::io::AsyncWrite;

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

pin_project! {
/// A future to write a slice of buffers to an `AsyncWrite`.
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct WriteVectored<'a, 'b, W: ?Sized> {
writer: &'a mut W,
bufs: &'a [IoSlice<'b>],
// Make this future `!Unpin` for compatibility with async trait methods.
#[pin]
_pin: PhantomPinned,
Comment on lines +14 to +19
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh I'm not totally sure we need two lifetimes here. The test case in the docs works where both are 'a but not sure if that could break some use cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer having both lifetimes.

}
}

pub(crate) fn write_vectored<'a, 'b, W>(
writer: &'a mut W,
bufs: &'a [IoSlice<'b>],
) -> WriteVectored<'a, 'b, W>
where
W: AsyncWrite + Unpin + ?Sized,
{
WriteVectored {
writer,
bufs,
_pin: PhantomPinned,
}
}

impl<W> Future for WriteVectored<'_, '_, W>
where
W: AsyncWrite + Unpin + ?Sized,
{
type Output = io::Result<usize>;

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