I think we can all agree the current Reader and Writer traits are clunky. Not only are they clunky, but have a lot of missed potential. The I/O reform RFC doesn’t really help, and makes some parts even uglier by introducing non-atomic error types everywhere instead of simply removing the offending methods (as they really shouldn’t exist, I’ll go into why later).
Reader also has a lot of overlap with a Iterator<Item = u8>, something which is currently completely ignored aside from a small helper function. Instead, I propose the following:
Then Reader can simply be an extension trait around Iterator where Item = u8.
Of course what I haven’t mentioned here is the error types. To preserve the functionality of Reader Iterator needs to be able to return an error. But for Iterator to still be able to return an Option the error type would need to be higher kinded. This is where I got stuck.
Iterators being able to return errors is also desirable in various other use cases.
As for why read_to_end & co. shouldn’t exist:
They’re extremely arbitrary. They will return an error after reading nothing an unspecified number of times, which the documentation nicely specifies as “too many”.
They’re dangerous. read_to_end and read_to_string allocate a buffer of 64k, which ties into the above. This makes it easy to unintentionally use copious amounts of memory.
Of course those flaws could be amended, but at that point they’re probably better off reimplemented by the user. With this design read_to_end and read_to_string could easily be replaced with an .extend() call instead (assuming the “no progress” check isn’t needed).
I strongly agree with both bringing the iterator and reader near together and having iterators returning results! I thing both changes would really have a lot of benefits and no big mali.
Hmm I like the new changes, but they also should make combining Read and Iterator even easier! What if this was done, and for loops were amended to only work where the error type is () or Void? Then we have all the benefis of fewer traits and impls, while not needing to amend the language with “functional break” etc. Those changes could be dealt with later in a backwards comparable way.
IMO the main problem with combining Reader & Iterator is how errors are handled. In the reader case, errors are often transient. Aka, read -> error, do something, read -> success. Iterator does not at all cover this sort of behavior.
I agree that is generally a point of distinction between instances of the two, but nothing in the current trait definitions enforce that. On one hand, there is BufReader and friends. While I can't think of a transient None from an iterator, the iterator docs specifically say:
The Iterator protocol does not define behavior after None is returned. A concrete Iterator implementation may choose to behave however it wishes, either by returning None infinitely, or by doing something else."
If the newly proposed WriteAllError is used for reading too (something I mentioned on the RFC comments) It, or something similar, could be used to delineate between fatal error and transient error, benign EOF, etc.
With this you could implement a kind a not fail save iterators without panic. Note that this also means you should have some kind of recovery/errorhandle branch entered if Err is returned (or panic if such a branch doesn’t exist).
For example fore something like this:
let iter = some_source.iter();
for item in iter {
/* do something with the content of the returned OK*/
} recover {
println!(”error: {}", item);
iter.reconnect()
if iter.connected() {
true
} else {
false
}
}
Where in the case of a true returned in the recovery branche the iteration will continue or else end.
Not that on the other hand it is probably to late to change the return value of a iterator and that this can always be archived by iterating over a Iterator returningResult and then matching it for Ok /Err manually maybe with help of a macro resulting in code not much more verbose than the one shown above