Skip to content

Commit 0719db8

Browse files
committed
frame_write: Use tokio_util::io::framed_write
1 parent e4cf88c commit 0719db8

File tree

2 files changed

+6
-35
lines changed

2 files changed

+6
-35
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ members = [
4343
futures-core = { version = "0.3", default-features = false }
4444
futures-sink = { version = "0.3", default-features = false }
4545
futures-util = { version = "0.3", default-features = false }
46-
tokio-util = { version = "0.7.1", features = ["codec"] }
46+
tokio-util = { version = "0.7.3", features = ["io", "codec"] }
4747
tokio = { version = "1", features = ["io-util"] }
4848
bytes = "1"
4949
http = "0.2"

src/codec/framed_write.rs

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::pin::Pin;
88
use std::task::{Context, Poll};
99
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
1010

11-
use std::io::{self, Cursor, IoSlice};
11+
use std::io::{self, Cursor};
1212

1313
// A macro to get around a method needing to borrow &mut self
1414
macro_rules! limited_write_buf {
@@ -44,9 +44,6 @@ struct Encoder<B> {
4444

4545
/// Max frame size, this is specified by the peer
4646
max_frame_size: FrameSize,
47-
48-
/// Whether or not the wrapped `AsyncWrite` supports vectored IO.
49-
is_write_vectored: bool,
5047
}
5148

5249
#[derive(Debug)]
@@ -76,7 +73,6 @@ where
7673
B: Buf,
7774
{
7875
pub fn new(inner: T) -> FramedWrite<T, B> {
79-
let is_write_vectored = inner.is_write_vectored();
8076
FramedWrite {
8177
inner,
8278
encoder: Encoder {
@@ -85,7 +81,6 @@ where
8581
next: None,
8682
last_data_frame: None,
8783
max_frame_size: frame::DEFAULT_MAX_FRAME_SIZE,
88-
is_write_vectored,
8984
},
9085
}
9186
}
@@ -126,21 +121,11 @@ where
126121
Some(Next::Data(ref mut frame)) => {
127122
tracing::trace!(queued_data_frame = true);
128123
let mut buf = (&mut self.encoder.buf).chain(frame.payload_mut());
129-
ready!(write(
130-
&mut self.inner,
131-
self.encoder.is_write_vectored,
132-
&mut buf,
133-
cx,
134-
))?
124+
ready!(write(&mut self.inner, &mut buf, cx,))?
135125
}
136126
_ => {
137127
tracing::trace!(queued_data_frame = false);
138-
ready!(write(
139-
&mut self.inner,
140-
self.encoder.is_write_vectored,
141-
&mut self.encoder.buf,
142-
cx,
143-
))?
128+
ready!(write(&mut self.inner, &mut self.encoder.buf, cx,))?
144129
}
145130
}
146131
}
@@ -165,26 +150,12 @@ where
165150
}
166151
}
167152

168-
fn write<T, B>(
169-
writer: &mut T,
170-
is_write_vectored: bool,
171-
buf: &mut B,
172-
cx: &mut Context<'_>,
173-
) -> Poll<io::Result<()>>
153+
fn write<T, B>(writer: &mut T, buf: &mut B, cx: &mut Context<'_>) -> Poll<io::Result<()>>
174154
where
175155
T: AsyncWrite + Unpin,
176156
B: Buf,
177157
{
178-
// TODO(eliza): when tokio-util 0.5.1 is released, this
179-
// could just use `poll_write_buf`...
180-
const MAX_IOVS: usize = 64;
181-
let n = if is_write_vectored {
182-
let mut bufs = [IoSlice::new(&[]); MAX_IOVS];
183-
let cnt = buf.chunks_vectored(&mut bufs);
184-
ready!(Pin::new(writer).poll_write_vectored(cx, &bufs[..cnt]))?
185-
} else {
186-
ready!(Pin::new(writer).poll_write(cx, buf.chunk()))?
187-
};
158+
let n = ready!(tokio_util::io::poll_write_buf(Pin::new(writer), cx, buf))?;
188159
buf.advance(n);
189160
Ok(()).into()
190161
}

0 commit comments

Comments
 (0)