Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tokio-util: add AsyncRead/AsyncWrite passthrough for inspect #5739

Merged

Conversation

cconstantine
Copy link
Contributor

This commit allows you to use InspectReader and InspectWriter on types that implement both AsyncRead + AsyncWriter without losing those trait implementations.

Motivation

While implementing clients for tcp interfaces it can be helpful to inspect the bytes sent / received through a tokio::io::TcpStream. The existing InspectReader / InspectWriter structs are a good start, but using them causes the stream to lose its AsyncWrite / AsyncRead trait impls.

Solution

By adding implementations for AsyncRead to InspectWriter and AsyncWrite to InspectReader it is now possible to inspect bytes sent through a TcpStream (or any other T: AsyncRead+AsyncWrite) and still pass that stream onto another subsystem that expects a T: AsyncRead+AsyncWrite.

For example:

use tokio::io::TcpStream;
use tokio_util::io::{InspectReader, InspectWriter};

#[tokio::main]
async fn main() {
    let stream = TcpStream::connect("127.0.0.1:8080").await.unwrap();
    let stream = InspectReader::new(stream, |buf: &[u8]| ... );
    let stream = InspectWriter::new(stream, |buf: &[u8]| ...);
        
    SomeClient::new(stream).run();
}

This commit allows you to use InspectReader and InspectWriter on types
that implement both AsyncRead + AsyncWriter without losing those trait
implementations.
@cconstantine cconstantine force-pushed the inspect_read_write_improvements branch from 2c1362e to ff6a878 Compare May 29, 2023 00:50
@cconstantine
Copy link
Contributor Author

The failing ci check appears to be unrelated to my changes.

@Darksonn Darksonn added A-tokio-util Area: The tokio-util crate M-io Module: tokio/io labels May 29, 2023
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have some unnecessary trait bounds.

tokio-util/src/io/inspect.rs Outdated Show resolved Hide resolved
tokio-util/src/io/inspect.rs Outdated Show resolved Hide resolved
Remove unnecessary trait bounds.

Co-authored-by: Alice Ryhl <aliceryhl@google.com>
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

@Darksonn Darksonn merged commit 7c12e41 into tokio-rs:master Jun 1, 2023
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-util Area: The tokio-util crate M-io Module: tokio/io
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants