Skip to content

Commit a4188e9

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

File tree

2 files changed

+8
-42
lines changed

2 files changed

+8
-42
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: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use bytes::{Buf, BufMut, BytesMut};
77
use std::pin::Pin;
88
use std::task::{Context, Poll};
99
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
10+
use tokio_util::io::poll_write_buf;
1011

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

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

4546
/// Max frame size, this is specified by the peer
4647
max_frame_size: FrameSize,
47-
48-
/// Whether or not the wrapped `AsyncWrite` supports vectored IO.
49-
is_write_vectored: bool,
5048
}
5149

5250
#[derive(Debug)]
@@ -76,7 +74,6 @@ where
7674
B: Buf,
7775
{
7876
pub fn new(inner: T) -> FramedWrite<T, B> {
79-
let is_write_vectored = inner.is_write_vectored();
8077
FramedWrite {
8178
inner,
8279
encoder: Encoder {
@@ -85,7 +82,6 @@ where
8582
next: None,
8683
last_data_frame: None,
8784
max_frame_size: frame::DEFAULT_MAX_FRAME_SIZE,
88-
is_write_vectored,
8985
},
9086
}
9187
}
@@ -126,21 +122,15 @@ where
126122
Some(Next::Data(ref mut frame)) => {
127123
tracing::trace!(queued_data_frame = true);
128124
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-
))?
125+
ready!(poll_write_buf(Pin::new(&mut self.inner), cx, &mut buf))?;
135126
}
136127
_ => {
137128
tracing::trace!(queued_data_frame = false);
138-
ready!(write(
139-
&mut self.inner,
140-
self.encoder.is_write_vectored,
141-
&mut self.encoder.buf,
129+
ready!(poll_write_buf(
130+
Pin::new(&mut self.inner),
142131
cx,
143-
))?
132+
&mut self.encoder.buf
133+
))?;
144134
}
145135
}
146136
}
@@ -165,30 +155,6 @@ where
165155
}
166156
}
167157

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<()>>
174-
where
175-
T: AsyncWrite + Unpin,
176-
B: Buf,
177-
{
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-
};
188-
buf.advance(n);
189-
Ok(()).into()
190-
}
191-
192158
#[must_use]
193159
enum ControlFlow {
194160
Continue,

0 commit comments

Comments
 (0)