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

feat: implement Framed::map_codec #4427

Merged
merged 5 commits into from Jan 27, 2022
Merged

Conversation

saiintbrisson
Copy link
Contributor

Motivation

Currently, changing the codec of a Framed is verbose and unnecessarily complicated, as noted in #717.

Solution

This PR implements a method that maps the inner codec to a new type, preserving the read/write buffers and IO state. The syntax was first suggested by Carl Lerche.
The usage is as follows:

let framed: Framed<Io, U32Codec> = Framed::new(io, U32Codec);
let _: u32 = framed.next().await.unwrap();

let framed: Framed<Io, U64Codec> = framed.map_codec(|_codec| U64Codec);
let _: u64 = framed.next().await.unwrap();

@saiintbrisson saiintbrisson changed the title feat: add Framed::map_codec feat: implement Framed::map_codec Jan 26, 2022
@Darksonn Darksonn added A-tokio-util Area: The tokio-util crate M-codec Module: tokio-util/codec labels Jan 26, 2022
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.

Thanks! Looks good.

@Darksonn
Copy link
Contributor

You will need to run rustfmt on this.

rustfmt --check --edition 2018 $(git ls-files '*.rs')

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.

Thanks.

@Darksonn Darksonn merged commit 2a5071f into tokio-rs:master Jan 27, 2022
@saiintbrisson saiintbrisson deleted the feat-717 branch January 27, 2022 11:39
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-codec Module: tokio-util/codec
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants