-
Notifications
You must be signed in to change notification settings - Fork 140
#[derive(From)]: allow specifying default values for struct/enum variant fields #500
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
base: master
Are you sure you want to change the base?
Conversation
fa62dc7
to
cd72645
Compare
When deriving `From`, it is often useful to let some fields be a certain "constant" default value instead of being part of the source tuple type. The most obvious such value is `Default::default()`, but specifying other values is very useful as well. As such, add handling of `#[from]` and `#[from(<default value>)]` attrs on struct (and enum struct) fields. For more information about the handling details, please consult the included documentation changes and/or tests. Closes JelteF#149
My changes have broken a test, which actually was actually incorrect twice: it wasn't doing the right thing (forwarding the From impl to the field type), but it wasn't testing the right thing either so it succeeded. Make the test actually check that from forwarding works, and make the forwarding actually work.
cd72645
to
ffa1cb3
Compare
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.
Thanks for the work on this. Some initial thoughts
inner: u8, | ||
#[from(1)] | ||
not_important: u32, | ||
#[from(HashMap::new())] |
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 I'd prefer to have the syntax be:
#[from(HashMap::new())] | |
#[from(value(HashMap::new()))] |
or
#[from(HashMap::new())] | |
#[from(default(HashMap::new()))] |
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.
Hmm; I think this makes it a bit more annoying to handle, since we then have to parse the token tree (e.g. value(HashMap::new())
ourselves, and I couldn't find an easy way to do that with syn
; the most obvious syn::ExprCall
doesn't have a Parse
trait. Do you have any pointers?
``` | ||
|
||
|
||
If you add a `#[from]` value to any fields of the struct, then only those |
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'd also want a way to specify the opposite:
#[from(default)]
extra_properties: HashMap<String, String>,
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 this can be handled with #[from(Default::default())]
(at least in the current design).
Resolves #149
Synopsis
When deriving
From
, it is often useful to let some fields be a certain"constant" default value instead of being part of the source tuple type.
The most obvious such value is
Default::default()
, but specifyingother values is very useful as well.
This is my first time touching proc-macros, please handle with care :)
The tests pass though (except for one that was broken before my changes), and this change shouldn't break anything.
I'm also open to alternative designs (e.g.
#[from(default = <...>)]
instead of#[from(...)]
)Solution
Add handling of
#[from]
and#[from(<default value>)]
attrson struct (and enum struct) fields. For more information about the
handling details, please consult the included documentation changes
and/or tests.
Excerpt from documentation
If you add a
#[from(<default value>)]
attribute to any fields of the struct,then those fields will be omitted from the tuple and be set to the default value
in the implementation:
If you add a
#[from]
value to any fields of the struct, then only thosefields will be present in the tuple and the rest will be either set to
Default::default()
or taken from their default values specified in#[from(<default value>)]
:Checklist