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

No obvious way to set a read limit for read_until #3346

Closed
Wesmania opened this issue Dec 25, 2020 · 2 comments
Closed

No obvious way to set a read limit for read_until #3346

Wesmania opened this issue Dec 25, 2020 · 2 comments
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-io Module: tokio/io

Comments

@Wesmania
Copy link

In standard library, it's possible to call BufRead's read_until with a read limit using a pattern like this:

reader.by_ref().take(limit).read_until(byte, buf)

The by_ref() call allows us to reuse the reader later. The same pattern works in async_std, as its ReadExt trait implements by_ref:

reader.by_ref().take(limit).read_until(byte, buf).await

Tokio only implements by_ref for *Half traits, which means the above one-liner doesn't work after wrappeng them in a BufReader. I don't see any other way of imposing a read limit on read_until in Tokio other than reimplementing it.

I suppose the simplest (end-user wise) way to fix this would be to implement by_ref in AsyncReadExt for any AsyncRead. I have no idea how hard it is, #2716 mentions some trouble implementing by_ref for TcpStream.

@Wesmania Wesmania added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Dec 25, 2020
@Darksonn Darksonn added the M-io Module: tokio/io label Dec 25, 2020
@Darksonn
Copy link
Contributor

Note that reader.by_ref() is equivalent to (&mut reader), so you should be able to do this:

(&mut reader).take(limit).read_until(byte, buf).await

The TcpStream::by_ref discussion is unrelated. Adding it would be very easy.

@Wesmania
Copy link
Author

It works for me, thanks. I suppose by_ref is just there for chaining.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-io Module: tokio/io
Projects
None yet
Development

No branches or pull requests

2 participants