This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
refactor(experimental): add support for variable-sized items in remainder-sized codecs #2020
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR allows
Array
,Map
andSet
codecs of"remainder"
size to have variable-size items (as recently updated on Umi's serializers).These array-like codecs support the following three configurable size strategies:
{ size: 42 }
means a fixed-size array of 42 items.{ size: getU32Codec() }
means the array is prefixed by au32
number storing its number of items.{ size: "remainder" }
means the remainder of the bytes should be used to encode and/or decode the items inside the array.Prior to this PR, we were enforcing arrays of
"remainder"
size to have fixed-size items. This allowed us to pre-compute the size of the array by dividing the size of what's left in the bytes by the fixed size of its item codec. If the modulo of that division was not zero, we would throw an error.However, this restriction is not actually necessary and prevents valid use cases. This PR lifts this restriction and allows arrays of
"remainder"
size to have variable-size items. When decoding the bytes, instead of pre-computing the size of the array, we simply decode the rest of the bytes continuously until we've reached the end of the bytes. If anything goes wrong during that process, the item codec will throw an error as it won't get the expected number of bytes.