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

feat!: add length_width to schema::Definition::Sequence #229

Merged
merged 4 commits into from
Sep 23, 2023
Merged

Conversation

mina86
Copy link
Contributor

@mina86 mina86 commented Sep 22, 2023

Add Definition::Sequence::length_width field which allows specifying
the width of the length tag for dynamically-sized arrays. This allows
defining custom schema where length is, say, one-byte.

Furthermore, support length_width being zero which indicates untagged
sequences. Use that as a new way to describe a fixed-length arrays
thus making Definition::Array no longer necessary.

Lastly, zero length_width with length range which is more than just
one value allows to document a custom encoding which cannot be fully
specified with the schema.

resolves #211 (VarInt and SmallVec<T>)

Add Definition::Sequence::length_width field which allows specifying
the width of the length tag for dynamically-sized arrays.  This
allows defining custom schema where length is, say, one-byte.

Furthermore, support length_width being zero which indicates untagged
sequences.  Use that as a new way to describe a fixed-length arrays thus
making Definition::Array no longer necessary.

Lastly, zero length_width with length range which is more than just
one value allows to document a custom encoding which cannot be fully
specified with the schema.
@mina86 mina86 requested review from frol and dj8yfo as code owners September 22, 2023 19:35
dj8yfo pushed a commit that referenced this pull request Sep 22, 2023
dj8yfo pushed a commit that referenced this pull request Sep 22, 2023
dj8yfo pushed a commit that referenced this pull request Sep 22, 2023
@dj8yfo
Copy link
Collaborator

dj8yfo commented Sep 22, 2023

Duplicate of #228

@dj8yfo dj8yfo marked this as a duplicate of #228 Sep 22, 2023
@dj8yfo
Copy link
Collaborator

dj8yfo commented Sep 22, 2023

@mina86, can you make #229 identical in content (with respect to code and tests, modify doc freely) to #228?
We'll then merge #229.

@mina86 mina86 requested a review from dj8yfo September 22, 2023 23:56
@@ -53,14 +66,15 @@ fn validate_impl<'a>(
stack.push(declaration);
match definition {
Definition::Primitive(_size) => {}
Definition::Array { elements, .. } => validate_impl(elements, schema, stack)?,
Copy link
Collaborator

Choose a reason for hiding this comment

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

thread 'validate_for_derived_types' panicked at 'assertion failed: `(left == right)`
  left: `Ok(())`,
 right: `Err(ZSTSequence("Array<nil, 300>"))`'

@dj8yfo dj8yfo self-requested a review September 23, 2023 08:15
@dj8yfo dj8yfo changed the title Add Definition::Sequence::length_width; remove Definition::Array feat!: add length_width to schema::Definition::Sequence Sep 23, 2023
@dj8yfo dj8yfo merged commit 2e02f68 into near:master Sep 23, 2023
@frol frol mentioned this pull request Sep 23, 2023
@mina86 mina86 deleted the a branch September 23, 2023 12:03
Comment on lines +35 to +36
/// only 0, 1, 2, 4 and 8 bytes long enum tags and sequences' `length_width` are alowed
TagNotPowerOfTwo(Declaration),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, why?

Comment on lines 95 to 112
// TODO(#230): variants.len() is actually incorrect here. Since
// arbitrary discriminant can be used for variants, an actual
// largest discriminant may be larger than the number of variants.
// For the time being we’re using variants.len() as a proxy but
// eventually variant discriminants should be added to the schema.
let max_discriminant = variants.len().saturating_sub(1) as u64;
check_tag_width(declaration, *tag_width, max_discriminant)?;
if *tag_width > U64_LEN {
return Err(Error::TagTooWide(declaration.to_string()));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So length_width: 3 is invalid (for whatever reason), but tag_width: 3 is valid?

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.

BorshSchema vs custom serialisation
2 participants