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]: Include byteSize for custom types #83

Open
febo opened this issue Sep 10, 2022 · 4 comments
Open

[feat]: Include byteSize for custom types #83

febo opened this issue Sep 10, 2022 · 4 comments
Assignees

Comments

@febo
Copy link
Contributor

febo commented Sep 10, 2022

Solita generates a BeetArgsStruct that exposes a type's attributes using beet types. We could also include the information of how many bytes the struct would take at most. This would help to create custom serialization/deserialization patterns. This could be either an autogenerated function or the facility to query beet with the attributes of the generated type.

@thlorenz
Copy link
Contributor

This would only work for types that have a clear max defined, i.e. Options Some vs None is an example.

However once you have a string or an array then the max size is potentially infinite. Do you have any ideas on how to handle those?

@thlorenz thlorenz self-assigned this Sep 19, 2022
@febo
Copy link
Contributor Author

febo commented Sep 21, 2022

Would it make sense to annotate the Rust struct with the max size? Even when we use strings and arrays in programs, we know what would be the max size since we need to allocate the account space. Then solita could get the information from the annotation and be able to determine the size of the data type.

@thlorenz
Copy link
Contributor

Yes that is an option. So combining these ideas we could do the following:

  • for fixed size structs provide a maxSize getter which just returns byteSize (since it is constant)
  • for structs annotated with a #[derive(ShankType(max_size=<size>)] shank would pick that up and include this info in the IDL + solita generates a getter that just returns that <size>
  • for non-fixed size structs without an annotation the getter will throw an Error alerting the user that they need to annotate the struct to get a maxSize provided

For the second option we need to extend shank and the types with a maxSize need to derive a ShankType(maxSize=x) (right now they just need to be BorshSerialize/BorshDeserialize.

Does that make sense?

@febo
Copy link
Contributor Author

febo commented Oct 4, 2022

Sounds good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants