Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io: delegate poll_write_vectored to inner #5914

Merged
merged 3 commits into from
Aug 10, 2023
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
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());
});
}