Skip to content

Iterator support for serde deserialization  #116

Open
@vorner

Description

@vorner

I have a file that is a sequence of serialized structs and I want to read them, one by one. I believe this is quite common situation. This is currently a bit hard, I have the following code:

while let Ok(record) = from_read::<_, Record>(&mut reader) {
  // Do stuff with record here
}
// Check that the above loop ended (errored) on eof, not something else
let mut ctl_buf = [0];
assert_eq!(0, reader.read(&mut ctl_buf).unwrap());

This checks first tries to repeatedly read a value from the reader until it fails and then checks that it reached the end of the file, not some other error. This, however, might also mean an EOF in the middle of the record, not only on the boundary (eg. if there's no more record).

There could be two ways to allow handling the situation in a better way:

  • Have an error type that can distinguish the situations (eg. an EOF before we read any data, an EOF in the middle of the record…).
  • Have an iterator reader, similar to serde-json has in StreamDeserializer.

Would it be hard to add either of these?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions