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

Add serialization and deserialization functions #18

Merged
merged 3 commits into from
Mar 3, 2022

Conversation

vbkaisetsu
Copy link
Member

@vbkaisetsu vbkaisetsu commented Mar 2, 2022

This branch adds the following serialization and deserialization functions for convenience.

  • serialize()
  • serialize_to_vec()
  • deserialize_unchecked()
  • deserialize_from_slice_unchecked()

Deserialization functions do not validate the given automaton, so these are defined as unsafe functions.
It means that the deserialization should only be used when the source of the data is completely trusted.

serialize() and deserialize_unchecked() use traits under std::io, so these functions are unavailable in the no_std environment. For bare-metal programming, they must use serialize_to_vec() and deserialize_from_slice_unchecked() instead.

@vbkaisetsu vbkaisetsu marked this pull request as ready for review March 2, 2022 14:10
Copy link
Member

@kampersanda kampersanda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the implementation!

I also think that employing unsafe declaration is a reasonable approach to implement deserialization.

One concern is that Daachorse, or its data structure, is expected to be updated, resulting in some difficulty to manage versions of models.
So, it would be better to add validation of the model version.
That is, I suggest that the serialization functions embed the current package version into the head of a file, and the deserialization functions validate the version.
The package version can be known through env!("CARGO_PKG_VERSION").
https://doc.rust-lang.org/cargo/reference/environment-variables.html

What do you think? @vbkaisetsu

@vbkaisetsu
Copy link
Member Author

vbkaisetsu commented Mar 3, 2022

unsafe functions should take a correct value, so I think any validation including version checking is unnecessary.

The intended use case is as follows:

  • Implementing pattern match on small devices that do not have a power to build automata.
  • Transporting automata on a single program.
  • Building automata using build.rs

This feature should not be used for distributing pre-built automata because users should not trust them.

@vbkaisetsu
Copy link
Member Author

@kampersanda

@kampersanda kampersanda self-requested a review March 3, 2022 04:37
Copy link
Member

@kampersanda kampersanda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you.

@vbkaisetsu vbkaisetsu merged commit 0326b4e into main Mar 3, 2022
@vbkaisetsu vbkaisetsu deleted the unsafe-serialization branch March 3, 2022 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants