#[derive(From)]: allow specifying default values for struct/enum variant fields#500
Open
balsoft wants to merge 2 commits intoJelteF:masterfrom
Open
#[derive(From)]: allow specifying default values for struct/enum variant fields#500balsoft wants to merge 2 commits intoJelteF:masterfrom
balsoft wants to merge 2 commits intoJelteF:masterfrom
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
JelteF
reviewed
Sep 1, 2025
Owner
JelteF
left a comment
There was a problem hiding this comment.
Thanks for the work on this. Some initial thoughts
| inner: u8, | ||
| #[from(1)] | ||
| not_important: u32, | ||
| #[from(HashMap::new())] |
Owner
There was a problem hiding this comment.
I think I'd prefer to have the syntax be:
Suggested change
| #[from(HashMap::new())] | |
| #[from(value(HashMap::new()))] |
or
Suggested change
| #[from(HashMap::new())] | |
| #[from(default(HashMap::new()))] |
Author
There was a problem hiding this comment.
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 |
Owner
There was a problem hiding this comment.
I'd also want a way to specify the opposite:
#[from(default)]
extra_properties: HashMap<String, String>,
Author
There was a problem hiding this comment.
I think this can be handled with #[from(Default::default())] (at least in the current design).
scutuatua-crypto
approved these changes
Dec 12, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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