-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
serialize::Decodable decode() should return a IoResult/DecodeResult? #12292
Comments
I am volunteering to provide a patch (as I am also working on the rust-msgpack and rust-toml libraries, which needs to be changed as well), if we can agree upon a |
It seems like Encodable should do something similar if we do do this. |
Yes, so in case the underlying Encoder fails with an IoError, we can propagate it back. The big question is only, what will the type of EncodeResult/DecodeResult be? |
For performance reasons, I don't like to bloat up the return values of each pub enum DecodeError = ...
pub type DecodeResult<T> = Result<T, ~DecodeError> which would only add one extra word (but of course incurrs an allocation in the error case). |
As the underlying error can be manifold (it can be an IoError or a protocol error), it would be better to just signal an error condition, and being able to retrieve the exact error condition (e.g. the IoError) from the Decoder or Encoder struct. Also the error heavily depends on the underlying Encoder/Decoder. For example a Decoder which is based on a MemReader would not need an IoError. |
I agree with this. I was implementing a Decoder and the only real way to propagate errors at the moment is to store the error information in the Decoder and move on. However, this means you're forced to continue parsing a source that you already know is invalid (or explicitly handle this in In my experience so far, IoError is actually not the predominant kind of error. Instead, a common problem is that the struct indicates that it wants a uint but the source gave you a string. For example, if you had a struct like this: These kinds of errors need to be handled by virtually all decoders, and should be supported out of the box rather than requiring an error field in the struct plus extra work in the handlers. Also, the current approach requires each of the handlers to still return a dummy value ( |
Every encoder/decoder will deal with errors differently so my suggestion is to change traits to this:
|
The other solution would be using IoResult but i'm not convinced it's the right type for this. I think custom types with apropriate error information are the best way. For decoding only EndOfFile, InvalidInput and OtherIoError are useful and any error information must be included in message string. |
All of Decoder and Encoder's methods now return a Result. Encodable.encode() and Decodable.decode() return a Result as well. fixes #12292
…n, r=jonas-schievink internal: Bump extension version
…ATTR, r=flip1995 Add new lint `DEPRECATED_CLIPPY_CFG_ATTR` As discussed [on zulip](https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/Is.20.60--cfg.20feature.3Dcargo-clippy.60.20deprecated.20or.20can.20it.20be.3F). This lint suggests to replace `feature = "cargo-clippy"` with `clippy`. r? `@flip1995` changelog: Add new lint `DEPRECATED_CLIPPY_CFG_ATTR`
Decoding can fail in the same way as any IO operation or any
read_xxx()
function in traitReader
. So should theserialize::Decodable
trait, no? Otherwise, we again depend on usingtask::try
for decoding data structures.My proposal is to change the signature of trait
Decodable
to something like:Likewise, the trait
serialize::Decoder
must be changed to returnDecodeResult
s as well.This of course comes with a slight performance penalty in the non-error case.
The text was updated successfully, but these errors were encountered: