mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-10-01 12:20:39 +00:00
parent
618d2bfc71
commit
eabb7ce61c
@ -11,7 +11,9 @@ use crate::io::util::write_int::{
|
|||||||
WriteU128, WriteU128Le, WriteU16, WriteU16Le, WriteU32, WriteU32Le, WriteU64, WriteU64Le,
|
WriteU128, WriteU128Le, WriteU16, WriteU16Le, WriteU32, WriteU32Le, WriteU64, WriteU64Le,
|
||||||
WriteU8,
|
WriteU8,
|
||||||
};
|
};
|
||||||
|
use crate::io::util::write_vectored::{write_vectored, WriteVectored};
|
||||||
use crate::io::AsyncWrite;
|
use crate::io::AsyncWrite;
|
||||||
|
use std::io::IoSlice;
|
||||||
|
|
||||||
use bytes::Buf;
|
use bytes::Buf;
|
||||||
|
|
||||||
@ -116,6 +118,47 @@ cfg_io_util! {
|
|||||||
write(self, src)
|
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
|
/// Writes a buffer into this writer, advancing the buffer's internal
|
||||||
/// cursor.
|
/// cursor.
|
||||||
|
@ -71,6 +71,7 @@ cfg_io_util! {
|
|||||||
pub use take::Take;
|
pub use take::Take;
|
||||||
|
|
||||||
mod write;
|
mod write;
|
||||||
|
mod write_vectored;
|
||||||
mod write_all;
|
mod write_all;
|
||||||
mod write_buf;
|
mod write_buf;
|
||||||
mod write_int;
|
mod write_int;
|
||||||
|
47
tokio/src/io/util/write_vectored.rs
Normal file
47
tokio/src/io/util/write_vectored.rs
Normal file
@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user