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

Codecs documentation could use some fully formed examples #6299

Closed
rageshkrishna opened this issue Jan 20, 2024 · 3 comments · Fixed by #6310
Closed

Codecs documentation could use some fully formed examples #6299

rageshkrishna opened this issue Jan 20, 2024 · 3 comments · Fixed by #6310
Labels
A-tokio-util Area: The tokio-util crate C-bug Category: This is a bug. E-help-wanted Call for participation: Help is requested to fix this issue. M-codec Module: tokio-util/codec T-docs Topic: documentation

Comments

@rageshkrishna
Copy link

I've been trying to understand codecs and how to use them. I started at https://docs.rs/tokio-util/latest/tokio_util/codec/index.html, which got me thinking that what I need is either a LinesCodec or LengthDelimitedCodec (I'm just playing around so doesn't really matter which one) and a FramedRead to use it with a stream. I followed through to https://docs.rs/tokio-util/latest/tokio_util/codec/length_delimited/index.html which shows me how to configure a decoder, and https://docs.rs/tokio-util/latest/tokio_util/codec/struct.FramedRead.html which tells me what FramedRead has.

But I still haven't really seen any concrete example that ties everything together and shows me exactly how to read a frame from a stream. I'm gathering that I actually need tokio_stream::StreamExt so I can do .next() on my FramedRead. This does not seem to be explained anywhere in the docs I've seen so far, and I only figured it out by looking at https://users.rust-lang.org/t/understanding-framed-in-tokio/42449/2

The documentation, by itself, does not help a beginner to get to a working state.

@rageshkrishna rageshkrishna added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Jan 20, 2024
@Darksonn Darksonn added C-bug Category: This is a bug. E-help-wanted Call for participation: Help is requested to fix this issue. T-docs Topic: documentation A-tokio-util Area: The tokio-util crate M-codec Module: tokio-util/codec and removed A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Jan 20, 2024
@Darksonn
Copy link
Contributor

Thank you for the report. Indeed, the current documentation explains how to write your own codec, but we don't explain how to use FramedRead/FramedWrite. Documentation for this would be very good to add.

@Dessix
Copy link

Dessix commented Jan 21, 2024

Is composition actually possible with these, in their current state? It's not clear from the Framed*::map setup, and either this is a hole in documentation or in functionality, but it's currently unclear which.

@Darksonn
Copy link
Contributor

Composition of two codecs is fundamentally not possible. A codec is a function from AsyncRead to Stream<Item = T>. Therefore, to compose two codecs, you would need a conversion from Stream<Item = T> to AsyncRead in the middle.

However, codecs can be combined with stream combinators. For example, you can use StreamExt::map to perform an additional operation after the codec.

(The same is true for FramedWrite, just in reverse.)

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 C-bug Category: This is a bug. E-help-wanted Call for participation: Help is requested to fix this issue. M-codec Module: tokio-util/codec T-docs Topic: documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants