Skip to content
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

what is the behavior of deserialize_from #249

Closed
evnix opened this issue Oct 3, 2018 · 5 comments
Closed

what is the behavior of deserialize_from #249

evnix opened this issue Oct 3, 2018 · 5 comments

Comments

@evnix
Copy link

@evnix evnix commented Oct 3, 2018

My struct looks as follows:

pub struct Record  {
    pub uuid:       [u8;16],
    pub error_count: i32,
    pub distance:    i64,
    pub data:       Vec<u8>
}

and then I write a loop to write around 1000 such Record appended to a file using serialize:

config().big_endian().serialize(&my_record)

Now when I do deserialize as follows, will it read the first record, if so, how will it know the exact size to read. (I could not find anything related to this in the docs)

    let record: Record = config().big_endian().deserialize_from(&file_read_handle)?;

if not, what is a good/right way to read such data (for example the 1000 records mentioned above.)

@TyOverby
Copy link
Collaborator

@TyOverby TyOverby commented Oct 6, 2018

It won't know how many to read. Your deserialization code will only read one.

@TyOverby
Copy link
Collaborator

@TyOverby TyOverby commented Oct 6, 2018

If I were you, I'd put them in a vec and then serialize the vec.

Otherwise, you could just keep reading until you run out of bytes in the file?

@evnix
Copy link
Author

@evnix evnix commented Oct 6, 2018

Otherwise, you could just keep reading until you run out of bytes in the file?

how will it know the exact size of a Record, as each record will be of variable size.

Let me put it another way, I am trying to understand if the below code is valid when Records are of variable size:

   while(has_more_data(file_read_handle)){
    let record: Record = config().big_endian().deserialize_from(&file_read_handle)?;
    //do something with the individual record
   }

@evnix
Copy link
Author

@evnix evnix commented Oct 6, 2018

an update,
The above code seems to work (I don't know how it works internally) but it saved me close to a 100 lines of code. :-)

@TyOverby
Copy link
Collaborator

@TyOverby TyOverby commented Oct 6, 2018

Yes, it does know how to deserialize records of variable size. The only way that the record is variable-length is when the the length of your data Vec changes. Because bincode stores the length of vectors in the output, we can successfully accommodate changes in size.

@TyOverby TyOverby closed this Oct 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.