Skip to content

Commit 0d322ce

Browse files
author
Minh Vu
authored
io: support vectored writes in sink (#8663)
1 parent 4167c56 commit 0d322ce

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎tokio/src/io/util/sink.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ impl AsyncWrite for Sink {
6262
Poll::Ready(Ok(buf.len()))
6363
}
6464

65+
#[inline]
66+
fn is_write_vectored(&self) -> bool {
67+
true
68+
}
69+
70+
#[inline]
71+
fn poll_write_vectored(
72+
self: Pin<&mut Self>,
73+
cx: &mut Context<'_>,
74+
bufs: &[io::IoSlice<'_>],
75+
) -> Poll<Result<usize, io::Error>> {
76+
ready!(crate::trace::trace_leaf(cx));
77+
ready!(poll_proceed_and_make_progress(cx));
78+
Poll::Ready(Ok(bufs.iter().map(|b| b.len()).sum()))
79+
}
80+
6581
#[inline]
6682
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
6783
ready!(crate::trace::trace_leaf(cx));

‎tokio/tests/io_sink.rs‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
)
1010
))]
1111

12-
use tokio::io::AsyncWriteExt;
12+
use std::io::IoSlice;
13+
use tokio::io::{AsyncWrite, AsyncWriteExt};
1314

1415
#[tokio::test]
1516
async fn sink_poll_write_is_cooperative() {
@@ -25,6 +26,34 @@ async fn sink_poll_write_is_cooperative() {
2526
}
2627
}
2728

29+
#[tokio::test]
30+
async fn sink_write_vectored_consumes_all_buffers() {
31+
let mut sink = tokio::io::sink();
32+
assert!(sink.is_write_vectored());
33+
34+
let bufs = [
35+
IoSlice::new(b""),
36+
IoSlice::new(b"hello"),
37+
IoSlice::new(b""),
38+
IoSlice::new(b"world"),
39+
];
40+
assert_eq!(sink.write_vectored(&bufs).await.unwrap(), 10);
41+
}
42+
43+
#[tokio::test]
44+
async fn sink_poll_write_vectored_is_cooperative() {
45+
tokio::select! {
46+
biased;
47+
_ = async {
48+
let bufs = [IoSlice::new(b"hello"), IoSlice::new(b"world")];
49+
loop {
50+
assert_eq!(tokio::io::sink().write_vectored(&bufs).await.unwrap(), 10);
51+
}
52+
} => {},
53+
_ = tokio::task::yield_now() => {}
54+
}
55+
}
56+
2857
#[tokio::test]
2958
async fn sink_poll_flush_is_cooperative() {
3059
tokio::select! {

0 commit comments

Comments
 (0)