-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add IO#read_greedy
#16535
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
base: master
Are you sure you want to change the base?
Add IO#read_greedy
#16535
Conversation
straight-shoota
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add some specs for this new method.
|
This pull request has been mentioned on Crystal Forum. There might be relevant details there: https://forum.crystal-lang.org/t/empty-content-when-reading-big-packets-on-tcp-ip/8652/4 |
|
suggestion: We could refactor the implementation of def read_fully?(slice : Bytes) : Int32?
count = read_greedy(slice)
return nil unless count == slice.size
count
endThis could also be a follow-up. |
Co-authored-by: Johannes Müller <[email protected]>
|
I applied all suggestions and added the cross-reference to the docs. |
Closes #14605
Adds the proposed
#read_greedymethod which tries to fill aSlicemultiple times if it was only filled partially by the read method.Since some users seem to expect this kind of behaviour from
#read(leading to hard-to-debug bugs), this could be seen as a "safer" version of #read for some use-cases.I didn't try to add a lazy reading function as there would be too many interpretations of what reading lazily even means depending on the use-case.
There's also a small optimization for IO::Memory (not benchmarked).