Skip to content

Commit 8a36b7c

Browse files
committed
Pass through AsyncRead and AsyncWrite
1 parent 2d57a18 commit 8a36b7c

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

src/tokio/bufread/generic/decoder.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use core::{
22
pin::Pin,
33
task::{Context, Poll},
44
};
5-
use std::io::Result;
5+
use std::io::{IoSlice, Result};
66

77
use crate::{codec::Decode, util::PartialBuffer};
88
use futures_core::ready;
99
use pin_project_lite::pin_project;
10-
use tokio::io::{AsyncBufRead, AsyncRead, ReadBuf};
10+
use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};
1111

1212
#[derive(Debug)]
1313
enum State {
@@ -162,3 +162,33 @@ impl<R: AsyncBufRead, D: Decode> AsyncRead for Decoder<R, D> {
162162
}
163163
}
164164
}
165+
166+
impl<R: AsyncBufRead + AsyncWrite, D: Decode> AsyncWrite for Decoder<R, D> {
167+
fn poll_write(
168+
mut self: Pin<&mut Self>,
169+
cx: &mut Context<'_>,
170+
buf: &[u8],
171+
) -> Poll<Result<usize>> {
172+
self.get_pin_mut().poll_write(cx, buf)
173+
}
174+
175+
fn poll_write_vectored(
176+
mut self: Pin<&mut Self>,
177+
cx: &mut Context<'_>,
178+
mut bufs: &[IoSlice<'_>],
179+
) -> Poll<Result<usize>> {
180+
self.get_pin_mut().poll_write_vectored(cx, bufs)
181+
}
182+
183+
fn is_write_vectored(&self) -> bool {
184+
self.get_ref().is_write_vectored()
185+
}
186+
187+
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
188+
self.get_pin_mut().poll_flush(cx)
189+
}
190+
191+
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
192+
self.get_pin_mut().poll_shutdown(cx)
193+
}
194+
}

src/tokio/bufread/macros/decoder.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,30 @@ macro_rules! decoder {
7272
}
7373
}
7474

75+
impl<$inner: tokio::io::AsyncBufRead + tokio::io::AsyncWrite> tokio::io::AsyncWrite for $name<$inner> {
76+
fn poll_write(
77+
self: std::pin::Pin<&mut Self>,
78+
cx: &mut std::task::Context<'_>,
79+
buf: &[u8],
80+
) -> std::task::Poll<std::io::Result<usize>> {
81+
self.project().inner.poll_write(cx, buf)
82+
}
83+
84+
fn poll_flush(
85+
self: std::pin::Pin<&mut Self>,
86+
cx: &mut std::task::Context<'_>,
87+
) -> std::task::Poll<std::io::Result<()>> {
88+
self.project().inner.poll_flush(cx)
89+
}
90+
91+
fn poll_shutdown(
92+
self: std::pin::Pin<&mut Self>,
93+
cx: &mut std::task::Context<'_>,
94+
) -> std::task::Poll<std::io::Result<()>> {
95+
self.project().inner.poll_shutdown(cx)
96+
}
97+
}
98+
7599
const _: () = {
76100
fn _assert() {
77101
use crate::util::{_assert_send, _assert_sync};

src/tokio/write/generic/encoder.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
};
1212
use futures_core::ready;
1313
use pin_project_lite::pin_project;
14-
use tokio::io::AsyncWrite;
14+
use tokio::io::{AsyncWrite, AsyncRead, ReadBuf};
1515

1616
#[derive(Debug)]
1717
enum State {
@@ -179,3 +179,13 @@ impl<W: AsyncWrite, E: Encode> AsyncWrite for Encoder<W, E> {
179179
Poll::Ready(Ok(()))
180180
}
181181
}
182+
183+
impl<W: AsyncWrite + AsyncRead, E: Encode> AsyncRead for Encoder<W, E> {
184+
fn poll_read(
185+
self: Pin<&mut Self>,
186+
cx: &mut Context<'_>,
187+
buf: &mut ReadBuf<'_>,
188+
) -> Poll<io::Result<()>> {
189+
self.get_pin_mut().poll_read(cx, buf)
190+
}
191+
}

src/tokio/write/macros/encoder.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ macro_rules! encoder {
7676
}
7777
}
7878

79+
impl<$inner: tokio::io::AsyncWrite + tokio::io::AsyncRead> tokio::io::AsyncRead for $name<$inner> {
80+
fn poll_read(
81+
self: std::pin::Pin<&mut Self>,
82+
cx: &mut std::task::Context<'_>,
83+
buf: &mut tokio::io::ReadBuf<'_>,
84+
) -> std::task::Poll<std::io::Result<()>> {
85+
self.project().inner.poll_read(cx, buf)
86+
}
87+
}
88+
7989
const _: () = {
8090
fn _assert() {
8191
use crate::util::{_assert_send, _assert_sync};

0 commit comments

Comments
 (0)