Skip to content

Commit

Permalink
io: delegate WriteHalf::poll_write_vectored (#5914)
Browse files Browse the repository at this point in the history
  • Loading branch information
M0dEx authored Aug 10, 2023
1 parent 4c220af commit 5d29bdf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
17 changes: 17 additions & 0 deletions tokio/src/io/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ cfg_io_util! {
where
T: AsyncRead + AsyncWrite,
{
let is_write_vectored = stream.is_write_vectored();

let inner = Arc::new(Inner {
locked: AtomicBool::new(false),
stream: UnsafeCell::new(stream),
is_write_vectored,
});

let rd = ReadHalf {
Expand All @@ -53,6 +56,7 @@ cfg_io_util! {
struct Inner<T> {
locked: AtomicBool,
stream: UnsafeCell<T>,
is_write_vectored: bool,
}

struct Guard<'a, T> {
Expand Down Expand Up @@ -131,6 +135,19 @@ impl<T: AsyncWrite> AsyncWrite for WriteHalf<T> {
let mut inner = ready!(self.inner.poll_lock(cx));
inner.stream_pin().poll_shutdown(cx)
}

fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[io::IoSlice<'_>],
) -> Poll<Result<usize, io::Error>> {
let mut inner = ready!(self.inner.poll_lock(cx));
inner.stream_pin().poll_write_vectored(cx, bufs)
}

fn is_write_vectored(&self) -> bool {
self.inner.is_write_vectored
}
}

impl<T> Inner<T> {
Expand Down
39 changes: 38 additions & 1 deletion tokio/tests/io_split.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![warn(rust_2018_idioms)]
#![cfg(all(feature = "full", not(target_os = "wasi")))] // Wasi does not support panic recovery

use tokio::io::{split, AsyncRead, AsyncWrite, ReadBuf, ReadHalf, WriteHalf};
use tokio::io::{
split, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf, ReadHalf, WriteHalf,
};

use std::io;
use std::pin::Pin;
Expand Down Expand Up @@ -36,6 +38,18 @@ impl AsyncWrite for RW {
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Poll::Ready(Ok(()))
}

fn poll_write_vectored(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
_bufs: &[io::IoSlice<'_>],
) -> Poll<Result<usize, io::Error>> {
Poll::Ready(Ok(2))
}

fn is_write_vectored(&self) -> bool {
true
}
}

#[test]
Expand Down Expand Up @@ -77,3 +91,26 @@ fn unsplit_err2() {
let (r, _) = split(RW);
r.unsplit(w);
}

#[test]
fn method_delegation() {
let (mut r, mut w) = split(RW);
let mut buf = [0; 1];

tokio_test::block_on(async move {
assert_eq!(1, r.read(&mut buf).await.unwrap());
assert_eq!(b'z', buf[0]);

assert_eq!(1, w.write(&[b'x']).await.unwrap());
assert_eq!(
2,
w.write_vectored(&[io::IoSlice::new(&[b'x'])])
.await
.unwrap()
);
assert!(w.is_write_vectored());

assert!(w.flush().await.is_ok());
assert!(w.shutdown().await.is_ok());
});
}

0 comments on commit 5d29bdf

Please sign in to comment.