Skip to content

Commit 096b161

Browse files
ctzdjc
authored andcommitted
Extract SyncWriteAdapter from write_io() body
1 parent 6f18143 commit 096b161

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

src/common/mod.rs

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -118,41 +118,7 @@ where
118118
}
119119

120120
pub fn write_io(&mut self, cx: &mut Context) -> Poll<io::Result<usize>> {
121-
struct Writer<'a, 'b, T> {
122-
io: &'a mut T,
123-
cx: &'a mut Context<'b>,
124-
}
125-
126-
impl<'a, 'b, T: Unpin> Writer<'a, 'b, T> {
127-
#[inline]
128-
fn poll_with<U>(
129-
&mut self,
130-
f: impl FnOnce(Pin<&mut T>, &mut Context<'_>) -> Poll<io::Result<U>>,
131-
) -> io::Result<U> {
132-
match f(Pin::new(self.io), self.cx) {
133-
Poll::Ready(result) => result,
134-
Poll::Pending => Err(io::ErrorKind::WouldBlock.into()),
135-
}
136-
}
137-
}
138-
139-
impl<'a, 'b, T: AsyncWrite + Unpin> Write for Writer<'a, 'b, T> {
140-
#[inline]
141-
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
142-
self.poll_with(|io, cx| io.poll_write(cx, buf))
143-
}
144-
145-
#[inline]
146-
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
147-
self.poll_with(|io, cx| io.poll_write_vectored(cx, bufs))
148-
}
149-
150-
fn flush(&mut self) -> io::Result<()> {
151-
self.poll_with(|io, cx| io.poll_flush(cx))
152-
}
153-
}
154-
155-
let mut writer = Writer { io: self.io, cx };
121+
let mut writer = SyncWriteAdapter { io: self.io, cx };
156122

157123
match self.session.write_tls(&mut writer) {
158124
Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => Poll::Pending,
@@ -360,5 +326,43 @@ impl<'a, 'b, T: AsyncRead + Unpin> Read for SyncReadAdapter<'a, 'b, T> {
360326
}
361327
}
362328

329+
/// An adapter that implements a [`Write`] interface for [`AsyncWrite`] types and an
330+
/// associated [`Context`].
331+
///
332+
/// Turns `Poll::Pending` into `WouldBlock`.
333+
pub struct SyncWriteAdapter<'a, 'b, T> {
334+
pub io: &'a mut T,
335+
pub cx: &'a mut Context<'b>,
336+
}
337+
338+
impl<'a, 'b, T: Unpin> SyncWriteAdapter<'a, 'b, T> {
339+
#[inline]
340+
fn poll_with<U>(
341+
&mut self,
342+
f: impl FnOnce(Pin<&mut T>, &mut Context<'_>) -> Poll<io::Result<U>>,
343+
) -> io::Result<U> {
344+
match f(Pin::new(self.io), self.cx) {
345+
Poll::Ready(result) => result,
346+
Poll::Pending => Err(io::ErrorKind::WouldBlock.into()),
347+
}
348+
}
349+
}
350+
351+
impl<'a, 'b, T: AsyncWrite + Unpin> Write for SyncWriteAdapter<'a, 'b, T> {
352+
#[inline]
353+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
354+
self.poll_with(|io, cx| io.poll_write(cx, buf))
355+
}
356+
357+
#[inline]
358+
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
359+
self.poll_with(|io, cx| io.poll_write_vectored(cx, bufs))
360+
}
361+
362+
fn flush(&mut self) -> io::Result<()> {
363+
self.poll_with(|io, cx| io.poll_flush(cx))
364+
}
365+
}
366+
363367
#[cfg(test)]
364368
mod test_stream;

0 commit comments

Comments
 (0)