-
Notifications
You must be signed in to change notification settings - Fork 64
Add options to configure how traits are rendered in Rust #242
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
Conversation
It makes it easier to plug into generated code
🦋 Changeset detectedLatest commit: 91cb4c8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
febo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Couple of micro nits on the comments/docs.
buffalojoec
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Post-humous review here!
I love the change. I think it's going to make working with the generated types a lot easier and less restrictive. A couple of thoughts from my side:
- Technically speaking, what these configs are representing are traits that are derived on the types. There's nothing stopping developers from implementing traits on generated types in a file outside of the
generateddir. I guess this would just be a naming nit then. - Some traits often rely on other traits being implemented on the type as well. for example, bytemuck's
PodrequiresCopy. I think it's okay to not really worry about this hiccup, since devs can just add the trait necessary and regenerate. - Some traits like bytemuck's
Podalso require#[repr(C)]. Even without bytemuck, some types may want to use this (like enums). Should we include a config forrepr?
| const traitOptions = { | ||
| baseDefaults: [ | ||
| 'borsh::BorshSerialize', | ||
| 'borsh::BorshDeserialize', | ||
| 'serde::Serialize', | ||
| 'serde::Deserialize', | ||
| 'Clone', | ||
| 'Debug', | ||
| 'Eq', | ||
| 'PartialEq', | ||
| ], | ||
| dataEnumDefaults: [], | ||
| scalarEnumDefaults: ['Copy', 'PartialOrd', 'Hash', 'num_derive::FromPrimitive'], | ||
| structDefaults: [], | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's a case to be made for the default being zero traits and zero scalar enum configurations, but I don't feel too strongly about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We had to keep them like that in order to avoid introducing breaking changes. This produces code in the exact same way we were before. But I think these are sensible defaults for most clients, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, it doesn't really hurt to just let people change the base defaults anyway, and avoid the breaking change.
|
|
||
| ### Default traits | ||
|
|
||
| Using the `traitOptions` attribute, you may configure the default traits that will be applied to every Rust type. These default traits can be configured using 4 different attributes: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
micro-micro-nit:
| Using the `traitOptions` attribute, you may configure the default traits that will be applied to every Rust type. These default traits can be configured using 4 different attributes: | |
| Using the `traitOptions` attribute, you may configure the default traits that will be applied to every generated Rust type. These default traits can be configured using 4 different attributes: |
|
|
||
| ### Overridden traits | ||
|
|
||
| In addition to configure the default traits, you may also override the traits for specific types. This will completely replace the default traits for the given type. To do so, you may use the `overrides` attribute of the `traitOptions` object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slick, I like it!
|
|
||
| ### Feature Flags | ||
|
|
||
| You may also configure which traits should be rendered under a feature flag by using the `featureFlags` attribute. This attribute is a map where the keys are feature flag names and the values are the traits that should be rendered under that feature flag. Here is an example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this even more!
@buffalojoec Would this be a blocker for you right now on some programs? If not, let's wait until more people ask for it. Otherwise, we can brainstorm a new |
Nope, not a blocker for me. I think if anyone tries to use the new trait configs to set up bytemuck traits then they'll hit the |
|
Hello, I'm trying to use Codama with a pinocchio+bytemuck program and I'm hitting this issue of the generated code not having the |
This PR adds the following
TraitOptionsconfiguration object to the Rust renderer allowing users to tweak how traits are rendered in their clients.The default values for these options are:
See readme changes for more details.