The goal of the Simple API (both reader and writer) is to make usage easier and reduce risk of panics. However, when a closure / function provided by the user by accident discards an error from reader or writer without propagating it, this can still lead to incorrect behavior and possibly panics. That could for example happen when users ignore or overlook compiler warnings about unused results, or use methods such as Result::map_or or Result::or_else on the result of reader or writer methods.
Enhancement description
If possible the reader and writer should track whether an error occurred, and prevent any subsequent explicit and implicit usage (e.g. when the method performs additional implicit calls to the underlying JsonReader).
Potential implementations:
Create wrapper JsonReader and JsonWriter structs which track whether errors have occurred
The disadvantage might be that if JsonReader or JsonWriter methods with default implementation are not overwritten to delegate to the underlying reader or writer, the default implementation might be less efficient.
For the current structs guard every access to the underlying JsonReader and JsonWriter with an error check
When a reader or writer method is used but a previous error exists, the implementation could:
Panic
Probably not desired since the Simple API should avoid panics
Return the previous error
Might be confusing if the previous error is about value type / structure mismatch and the currently called method is completely unrelated. This could possibly be avoided by allowing to retry on value type / structure mismatch (which is allowed by JsonReader); but that might be error-prone because for example for read_seeked retrying is not safe.
Return a wrapping / dummy error, which contains the original error message if possible
Additionally the documentation for SimpleJsonReader and SimpleJsonWriter should make it clearer that errors from the reader or writer inside closures / functions must be propagated, ideally with the ? operator.
Problem solved by the enhancement
The goal of the Simple API (both reader and writer) is to make usage easier and reduce risk of panics. However, when a closure / function provided by the user by accident discards an error from reader or writer without propagating it, this can still lead to incorrect behavior and possibly panics. That could for example happen when users ignore or overlook compiler warnings about unused results, or use methods such as
Result::map_or
orResult::or_else
on the result of reader or writer methods.Enhancement description
If possible the reader and writer should track whether an error occurred, and prevent any subsequent explicit and implicit usage (e.g. when the method performs additional implicit calls to the underlying
JsonReader
).Potential implementations:
JsonReader
andJsonWriter
structs which track whether errors have occurred The disadvantage might be that ifJsonReader
orJsonWriter
methods with default implementation are not overwritten to delegate to the underlying reader or writer, the default implementation might be less efficient.JsonReader
andJsonWriter
with an error checkWhen a reader or writer method is used but a previous error exists, the implementation could:
JsonReader
); but that might be error-prone because for example forread_seeked
retrying is not safe.Additionally the documentation for
SimpleJsonReader
andSimpleJsonWriter
should make it clearer that errors from the reader or writer inside closures / functions must be propagated, ideally with the?
operator.Alternatives / workarounds
Only adjust documentation