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

It'd be nice to be able to set a default value without needing a function #2254

Closed
ilyvion opened this issue Aug 8, 2022 · 5 comments · May be fixed by hottea773/serde#1
Closed

It'd be nice to be able to set a default value without needing a function #2254

ilyvion opened this issue Aug 8, 2022 · 5 comments · May be fixed by hottea773/serde#1

Comments

@ilyvion
Copy link

ilyvion commented Aug 8, 2022

I could be missing something, but I've got this definition:

#[derive(Debug, Deserialize)]
struct PropertyValue {
    #[serde(default = "default_property_type")]
    pub r#type: String,
    pub value: String,
}
fn default_property_type() -> String {
    "string".to_owned()
}

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")].

@rustonaut
Copy link

rustonaut commented Oct 24, 2022

Proc macros do support code snippets.

So instead of #[serde(default_value = "1")] you can have #[serde(default_value = 1)] or even
something like #[serde(default_value = Some("foo".into()))] or ``.

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 default to accept snippets which are functions e.g.:

struct Foo {
    #[serde(default = || "foobar".into() )]
    field: String,
    
    #[serde(default = || format!("foo-bar-{APPLICATION_NAME}")]
    field2: String,

Through there are two drawbacks:

  1. AFIK in the very early stable days of rust this was not possible, I'm not sure if 1.13 is new enough we would have to test it.
  2. syn has with Meta a nice helper to parse proc-macro attributes, but that helper doesn't support this. We don't have to use it as it's not reducing complexity by that much, but this means you probably would need some preparing PRs which do make sure we can parse attributes as <path> "(" <key> [ "=" <tokens> ] ")"

EDIT:

For reference: https://docs.rs/syn/1.0.103/syn/struct.Attribute.html

As you can see the syntax (ignoring the #[]/#![]) is <path> <tokens> and it's only the Meta helper which further constraints what <tokens> can be.

@kangalio
Copy link

Duplicate of #368

@dtolnay dtolnay closed this as completed Jul 9, 2023
@ken0x0a
Copy link

ken0x0a commented Jul 11, 2023

@dtolnay
I couldn't find commits to close this issue in these 2 days and couldn't find attribute like default_value e.t.c.
Did you want to close this?

@oli-obk
Copy link
Member

oli-obk commented Jul 11, 2023

This issue is a duplicate of #368

You can read and subscribe to the other issue

@jendakol
Copy link

For a random reader... check out https://crates.io/crates/serde-inline-default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

7 participants