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

Relative encodings #24

Merged
merged 47 commits into from
Jul 25, 2024
Merged

Relative encodings #24

merged 47 commits into from
Jul 25, 2024

Conversation

sgwilym
Copy link
Contributor

@sgwilym sgwilym commented Jul 14, 2024

New relative encoding traits

Adds RelativeEncodable and RelativeDecodable traits. But wait! Why the new traits, you ask? Why not just define a Relativity struct and implement Encoder and Decoder on those?

  • Decoder::decode is a static method which takes a Ufotofu producer as its single parameter. But we need the reference value to to decode relative to! Thus we must have a RelativeDecoder trait which has the reference in question as a (&self) parameter.
  • While it is possible to implement Encodable on a Relativity struct (see the first commit), symmetry between Encodable and Decodable traits has now been broken, which I found a bit confusing
  • There is another kind of error exclusive to relative encodings which we'd like to be able to surface, which is IllegalRelativity. Some relative encodings require the subject being encoded to be logically included by the reference item, e.g. an Entry encoded relative to an Area must be included by that Area. With a Relativity struct, the only way to check for this would be to define custom constructors for each kind of relative pairing, which was possible to accidentally forget to do. But with a dedicated RelativeEncoder trait, we can return Result<(), RelativeEncodeError>.

All this considered it made more sense to me to have dedicated traits.

Fuzzing utilities

  • relative_encoding_roundtrip (encode, decode, check it came out okay)
  • relative_encoding_random (decode random bytes, encode, assert new encoding is exactly the same as the previous one)
  • relative_encoding_random_less_strict(decode random bytes, encode, decode it again, assert the decoded object is the same the second time around)

Encodings

  • Path relative to path
  • Entry relative entry
  • Entry relative area
  • Entry relative range
  • Area relative area
  • 3dRange relative 3dRange

@sgwilym sgwilym added the enhancement New feature or request label Jul 14, 2024
@sgwilym sgwilym requested a review from AljoschaMeyer July 14, 2024 15:15
@AljoschaMeyer
Copy link
Contributor

Decoder::decode is a static method which takes a Ufotofu producer as its single parameter. But we need the reference value to to decode relative to! Thus we must have a RelativeDecoder trait which has the reference in question as a (&self) parameter.

Good point. Yeah, I like the two separate traits, then.

There is another kind of error exclusive to relative encodings which we'd like to be able to surface, which is IllegalRelativity.

Not sure about this one. I think this should be a panic rather than an error, since it indicates a bug in your own implementation rather than an expected case that is allowed to happen.

@sgwilym sgwilym force-pushed the relative-encodings branch from be0b244 to a977f64 Compare July 14, 2024 21:48
@sgwilym
Copy link
Contributor Author

sgwilym commented Jul 15, 2024

Not sure about this one. I think this should be a panic rather than an error, since it indicates a bug in your own implementation rather than an expected case that is allowed to happen.

Agreed. RelativeEncodeError has been removed.

@sgwilym sgwilym marked this pull request as ready for review July 22, 2024 16:02
@sgwilym sgwilym merged commit 45341a0 into main Jul 25, 2024
1 check passed
@sgwilym sgwilym deleted the relative-encodings branch July 25, 2024 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants