-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Take does not reserve its limit on read_to_end #51746
Comments
Make io::Read::read_to_end consider io::Take::limit Add a custom implementation of `io::Read::read_to_end` for `io::Take` that doesn't reserve the default 32 bytes but rather `Take::limit` if `Take::limit < 32`. It's a conservative adjustment that preserves the default behavior for `Take::limit >= 32`. Fixes rust-lang#51746.
Make io::Read::read_to_end consider io::Take::limit Add a custom implementation of `io::Read::read_to_end` for `io::Take` that doesn't reserve the default 32 bytes but rather `Take::limit` if `Take::limit < 32`. It's a conservative adjustment that preserves the default behavior for `Take::limit >= 32`. Fixes rust-lang#51746.
@ljedrz thanks for the PR. This solved my issue for I wanted to ask about the other scenarios where capacity is not a multiple of 32. If I do Is this by design? Asking because it was also a reason I reported this. Also, are there concerns (like I/O) against simply reserving |
@MichaelTheriot I had thought about this, but was worried about cases where the size of the readable object is unknown; in those cases you might want to e.g. As I mentioned in my PR, I was just being cautious; if you were to benchmark different scenarios and it turned out that just reserving Adjusting the code would be easy: you'd just need to change this line to |
Using
readable.take(x).read_to_end(&mut buf)
can cause the capacity ofbuf
to exceed what is needed.I tried this code:
https://play.rust-lang.org/?gist=83401a704fddcec007bcfb0ecc85fc6d&version=nightly&mode=debug
I expected to see this happen:
readable.take(1)
should reserve1
.Take
should reserve a maximum oftake.limit()
.Instead, this happened:
buf
capacity increased becausereadable.take(1)
reserved32
which exceeds itslimit
.Take
will always reserve in increments of32
rather than its knownlimit
.I'm new to Rust; my guess is implementing
read_to_end
specifically forTake
is the way to improve this.The text was updated successfully, but these errors were encountered: