-
-
Notifications
You must be signed in to change notification settings - Fork 773
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
It'd be nice to be able to set a default value without needing a function #2254
Comments
Proc macros do support code snippets. So instead of struct Foo {
#[serde(default_value = 1)]
num: u16,
#[serde(default_value = u16::MAX)]
num: u16,
//or even
#[serde(default_value = Cow::Borrowed(APPLICATION_NAME))]
name: Cow<'static, str>, Alternatively we could also change struct Foo {
#[serde(default = || "foobar".into() )]
field: String,
#[serde(default = || format!("foo-bar-{APPLICATION_NAME}")]
field2: String, Through there are two drawbacks:
EDIT: For reference: https://docs.rs/syn/1.0.103/syn/struct.Attribute.html As you can see the syntax (ignoring the |
Duplicate of #368 |
@dtolnay |
This issue is a duplicate of #368 You can read and subscribe to the other issue |
For a random reader... check out https://crates.io/crates/serde-inline-default |
I could be missing something, but I've got this definition:
It feels excessive to have a whole function just for this, it'd be nice if I could do something like
#[serde(default = || "string".to_owned())]
or maybe#[serde(default_value = "string")]
.The text was updated successfully, but these errors were encountered: