Skip to content

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.

2 participants