Skip to content

Commit

Permalink
Merge pull request #304 from link2xt/link2xt/passthrough-asyncbufread
Browse files Browse the repository at this point in the history
Pass through `AsyncBufRead` trait for write-based encoders/decoders
  • Loading branch information
NobodyXu authored Oct 16, 2024
2 parents 535fc9c + bfb76dd commit 5290277
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/futures/write/generic/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
util::PartialBuffer,
};
use futures_core::ready;
use futures_io::{AsyncRead, AsyncWrite, IoSliceMut};
use futures_io::{AsyncBufRead, AsyncRead, AsyncWrite, IoSliceMut};
use pin_project_lite::pin_project;

#[derive(Debug)]
Expand Down Expand Up @@ -202,3 +202,13 @@ impl<W: AsyncRead, D> AsyncRead for Decoder<W, D> {
self.get_pin_mut().poll_read_vectored(cx, bufs)
}
}

impl<W: AsyncBufRead, D> AsyncBufRead for Decoder<W, D> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
self.get_pin_mut().poll_fill_buf(cx)
}

fn consume(self: Pin<&mut Self>, amt: usize) {
self.get_pin_mut().consume(amt)
}
}
12 changes: 11 additions & 1 deletion src/futures/write/generic/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
util::PartialBuffer,
};
use futures_core::ready;
use futures_io::{AsyncRead, AsyncWrite, IoSliceMut};
use futures_io::{AsyncBufRead, AsyncRead, AsyncWrite, IoSliceMut};
use pin_project_lite::pin_project;

#[derive(Debug)]
Expand Down Expand Up @@ -199,3 +199,13 @@ impl<W: AsyncRead, E> AsyncRead for Encoder<W, E> {
self.get_pin_mut().poll_read_vectored(cx, bufs)
}
}

impl<W: AsyncBufRead, E> AsyncBufRead for Encoder<W, E> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
self.get_pin_mut().poll_fill_buf(cx)
}

fn consume(self: Pin<&mut Self>, amt: usize) {
self.get_pin_mut().consume(amt)
}
}
13 changes: 13 additions & 0 deletions src/futures/write/macros/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ macro_rules! decoder {
}
}

impl<$inner: futures_io::AsyncBufRead> futures_io::AsyncBufRead for $name<$inner> {
fn poll_fill_buf(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>
) -> std::task::Poll<std::io::Result<&[u8]>> {
self.get_pin_mut().poll_fill_buf(cx)
}

fn consume(self: std::pin::Pin<&mut Self>, amt: usize) {
self.get_pin_mut().consume(amt)
}
}

const _: () = {
fn _assert() {
use crate::util::{_assert_send, _assert_sync};
Expand Down
13 changes: 13 additions & 0 deletions src/futures/write/macros/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ macro_rules! encoder {
}
}

impl<$inner: futures_io::AsyncBufRead> futures_io::AsyncBufRead for $name<$inner> {
fn poll_fill_buf(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>
) -> std::task::Poll<std::io::Result<&[u8]>> {
self.get_pin_mut().poll_fill_buf(cx)
}

fn consume(self: std::pin::Pin<&mut Self>, amt: usize) {
self.get_pin_mut().consume(amt)
}
}

const _: () = {
fn _assert() {
use crate::util::{_assert_send, _assert_sync};
Expand Down
12 changes: 11 additions & 1 deletion src/tokio/write/generic/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
};
use futures_core::ready;
use pin_project_lite::pin_project;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};

#[derive(Debug)]
enum State {
Expand Down Expand Up @@ -196,3 +196,13 @@ impl<W: AsyncRead, D> AsyncRead for Decoder<W, D> {
self.get_pin_mut().poll_read(cx, buf)
}
}

impl<W: AsyncBufRead, D> AsyncBufRead for Decoder<W, D> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
self.get_pin_mut().poll_fill_buf(cx)
}

fn consume(self: Pin<&mut Self>, amt: usize) {
self.get_pin_mut().consume(amt)
}
}
12 changes: 11 additions & 1 deletion src/tokio/write/generic/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
};
use futures_core::ready;
use pin_project_lite::pin_project;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};

#[derive(Debug)]
enum State {
Expand Down Expand Up @@ -193,3 +193,13 @@ impl<W: AsyncRead, E> AsyncRead for Encoder<W, E> {
self.get_pin_mut().poll_read(cx, buf)
}
}

impl<W: AsyncBufRead, E> AsyncBufRead for Encoder<W, E> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
self.get_pin_mut().poll_fill_buf(cx)
}

fn consume(self: Pin<&mut Self>, amt: usize) {
self.get_pin_mut().consume(amt)
}
}
13 changes: 13 additions & 0 deletions src/tokio/write/macros/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ macro_rules! decoder {
}
}

impl<$inner: tokio::io::AsyncBufRead> tokio::io::AsyncBufRead for $name<$inner> {
fn poll_fill_buf(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>
) -> std::task::Poll<std::io::Result<&[u8]>> {
self.get_pin_mut().poll_fill_buf(cx)
}

fn consume(self: std::pin::Pin<&mut Self>, amt: usize) {
self.get_pin_mut().consume(amt)
}
}

const _: () = {
fn _assert() {
use crate::util::{_assert_send, _assert_sync};
Expand Down
13 changes: 13 additions & 0 deletions src/tokio/write/macros/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ macro_rules! encoder {
}
}

impl<$inner: tokio::io::AsyncBufRead> tokio::io::AsyncBufRead for $name<$inner> {
fn poll_fill_buf(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>
) -> std::task::Poll<std::io::Result<&[u8]>> {
self.get_pin_mut().poll_fill_buf(cx)
}

fn consume(self: std::pin::Pin<&mut Self>, amt: usize) {
self.get_pin_mut().consume(amt)
}
}

const _: () = {
fn _assert() {
use crate::util::{_assert_send, _assert_sync};
Expand Down

0 comments on commit 5290277

Please sign in to comment.