Skip to content
This repository has been archived by the owner on Aug 15, 2021. It is now read-only.

IO error EOF getting through #30

Closed
vorner opened this issue Apr 23, 2017 · 6 comments
Closed

IO error EOF getting through #30

vorner opened this issue Apr 23, 2017 · 6 comments
Milestone

Comments

@vorner
Copy link

vorner commented Apr 23, 2017

Let's say I have this code:

extern crate serde_cbor;

use std::collections::HashMap;

use serde_cbor::{from_slice, to_vec};

fn main() {
    let mut data = HashMap::new();
    data.insert(42, "Hello");
    let encoded = to_vec(&data).expect("Failed to encode");
    let decoded: HashMap<usize, String> = from_slice(&encoded[..encoded.len() - 1]).expect("Failed to decode");
}

The last line correctly fails, as the last byte is missing. However, this is the error I get:

Failed to decode: Io(Error { repr: Custom(Custom { kind: UnexpectedEof, error: StringError("failed to fill whole buffer") }) })

Looking at the documentation, I'd expect the error to be Eof, not Io(Custom(… io-based-eof…)). To be fair, the documentation doesn't explicitly say that all EOF-based errors are going to be reported as Eof, but it was still a bit surprise.

@pyfisch pyfisch modified the milestone: serde 1.0 Apr 23, 2017
@pyfisch
Copy link
Owner

pyfisch commented Apr 23, 2017

The Eof variant was never used in the current code and should have been removed long ago. The Error enum should be improved somehow probably it should be more like serde_json::Error. Thanks for reporting this!

@vorner
Copy link
Author

vorner commented Apr 23, 2017

Hmm. Then I'd like to know if EOF in data can cause only the Io(Custom(EOF)) or something else too. Because I wrote a crate to deserialize CBOR from a network stream (tokio-serde-cbor) and some way to detect if I have enough data to decode a „frame“ is needed.

(The serde_json::Error has is_eof for that purpose)

@pyfisch
Copy link
Owner

pyfisch commented Apr 23, 2017

All io errors are preserved in Error::Io. The std::io::Error has a method kind to get the kind and then test for a UnexpectedEoferror.

@vorner
Copy link
Author

vorner commented Apr 23, 2017

Yes, I already handle that one.

I was asking if an EOF in input can cause another error I'd need to handle. Currently I do this:
https://github.com/vorner/tokio-serde-cbor/blob/master/src/lib.rs#L95

I thought the IO error handling is a temporary workaround, but now it seems like it is going to stay there. So I would like to be sure there's no other branch I need to add.

@pyfisch
Copy link
Owner

pyfisch commented Apr 23, 2017

Yes IO error handling is (probably) here to stay. Would you prefer if I adapted the error type from serde_json for serde_cbor?

@vorner
Copy link
Author

vorner commented Apr 23, 2017

The serd_json way is nice, clean and convenient.

However, I didn't really ask for any particular code or API changes. I just wanted to know if what I do is correct/enough or if I should handle some other case as well O:-).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants