-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
add back bufferless read_to_string/read_to_end methods #970
Conversation
4ee8119
to
1ce7faa
Compare
629b0b4
to
a17468d
Compare
|
||
But we'd like to be able to just write | ||
|
||
Or this: |
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.
Is there a missing example before this?
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.
Actually it's just a superfluous Or this:
;) I updated the PR
a17468d
to
78c633c
Compare
|
||
`fn read_to_end(&mut self) -> Result<Vec<u8>>` | ||
|
||
`fn read_to_string(&mut self) -> Result<String>` |
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.
Don't these conflict with the existing read_to_end
and read_to_string
methods?
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.
Good point. What's the state of method overloading in Rust? I've been basically away from Rust for the last couple of months which means I have to start over. If there's no such thing as method overloading I'd need to give them other names.
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.
You can provide multiple methods of the same name from differing traits, and use UFCS (ie ReadExt::read_to_end(&mut rdr, Vec::new())
) to differentiate. But that would not improve the convenience you mentioned in this RFC. You'd need a new name.
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.
read_into_vec
and read_into_string
, possibly
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.
@reem I like those, updated the PR
👍. There should be a simple way to do simple things. |
Also created a PR against the source rust-lang/rust#23335 |
There were two concerns which motivated moving to taking a buffer instead of returning it, the first of which you've discussed here (performance), but perhaps the more important one is the loss of information. The APIs for |
Sorry @alexcrichton I may not see the forrest for the trees. How is this
|
With read_to_string, even if there's an error, all previously read data has On Sun, Mar 15, 2015, 3:13 PM Christoph Burgdorf [email protected]
|
Ah, sure. I see it. How about a special error type that will include the |
It is plausible to create a |
I understand the stance you take on that. We probably look at this from too different perspectives. For me, it just feels strange not to have such methods on the The way I see it, we should follow the principle of least astonishment here. |
I like this. Getting the contents as a newly-created
I don't think this should be a concern. Even with the current I definitely don't think it's worth adding a I think combined with |
I think we should wait to see how much of a problem this is once we have a larger body of rust code and add this later. This is a very easy API to add later but quite an awkward one to keep around if we decide we don't want any data-losing APIs . |
Instead of specifically a ReadIntoStringError, there could be a PartialReadError or similar, which can be used by any read operation which might fail partway through. The error would allow access to the partially read data. |
Ok, for now I created a I didn't manage to get the tests working yet, so I'm not actually sure if it works. |
Never mind, it's working now with version 0.1.0 |
Commenting just because I'm an example of one of those .NET guys who was surprised (astonished! :P) to find there was no trivial way to do this. I guessed why it wasn't there (the reasons mentioned above) and figured the justification was that it's so easy to add that, if I want it, I'm expected to do it myself... But I'd be totally cool with this being a thing I didn't have to implement, too. :) |
Haha, awesome. You are validating my theory ;)
For now just use my readext crate |
Imagine the case of reading a configuration file. This will be done once at the start of the program, so is hardly performance critical, and there isn't much use in loading half a configuration file, so it's really all or nothing. That kind of thing is a lot less convenient with the revised IO APIs than it was before. |
Thanks for the RFC! The goal of the recent IO reform was to provide high-performance, low-level APIs that map as directly to system APIs as possible, and can be used to build various higher-level abstractions and conveniences. While additional conveniences as proposed here may eventually be prudent to add to |
The
Read
trait lost it'sread_to_string
andread_to_end
methods that didn't require to pass a buffer. That comes as a small convenience loss. @steveklabnik suggested to write a PR to bring them back.Rendered view