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

Don’t declare schema for empty enum variants #227

Closed
wants to merge 1 commit into from
Closed

Conversation

mina86
Copy link
Contributor

@mina86 mina86 commented Sep 19, 2023

No information of the source type is lost by defining empty enum variants
as unit type rather than creating a custom declaration for them. At the
same time, not doing that simplifies the schemas and reduces chances for
name collisions in derived schemas.

<ASalad as borsh::BorshSchema>::add_definitions_recursively(definitions);
<ASausage as borsh::BorshSchema>::add_definitions_recursively(definitions);
let definition = borsh::schema::Definition::Enum {
tag_width: 1,
variants: borsh::__private::maybestd::vec![
("Bacon".to_string(), < ABacon > ::declaration()), ("Eggs".to_string(), <
Copy link
Collaborator

@dj8yfo dj8yfo Sep 19, 2023

Choose a reason for hiding this comment

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

actually, this change implies, that "nil" starts to mean different things in different contexts (as if it were mapped to 2 different Definition-s, picked from in different contexts).
It adds a new rule for interpreting "nil" as a unit struct variant in these situations.
Let's keep it more verbose (and more straightforward) as it is now. One looks at a variant definition, and sees that it's a unit struct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But with this change one can look at a variant definition and sees it’s a unit struct. In fact, it’s now easier to see it’s a unit struct since when I see ("Bacon", "nil") I immediately know it’s a variant with no data; when I see ("Bacon", "ABecon") I know have to look up what ABecon is.

What’s more, nil can be mapped to an empty tuple which would better match that it’s used for (). (To be honest, I’d actually change the declaration from "nil" to "()" and then also change declaration for tuples to use (...)).

Copy link
Collaborator

Choose a reason for hiding this comment

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

@mina86 i think, it's a good idea to change declarations:

  1. nil -> ()
  2. Tuple<T0, T1, T2> -> (T0, T1, T2)
  3. Array<T0, N> -> [T0; N]

i've created #231, please post there if you want to go ahead and create one or more of 3 subtasks prs,
so that we don't accidentally concurrently do the same thing.

No information of the source type is lost by defining empty enum variants
as unit type rather than creating a custom declaration for them.  At the
same time, not doing that simplifies the schemas and reduces chances for
name collisions in derived schemas.
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.

2 participants