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

Expanded #[serder(default = ...)] capabilities. #2728

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

Threadzless
Copy link

Before

Giving a field a default value that isn't the same as that type's Default required users to define a function for each field.

#[derive(Serialize, Deserialize)]
pub struct MyStruct {
    #[serde(default = "my_struct_a_default")]
    a: i32,
    #[serde(default = "my_struct_b_default")]
    b: i32,
}

fn my_struct_a_default() -> i32 {
    12
}
fn my_struct_b_default() -> i32 {
    16
}

Now

Constant expressions may also be used, so long as they are wrapped in parenthesis

#[derive(Serialize, Deserialize)]
pub struct MyStruct {
    #[serde(default = (12))]
    a: i32,
    #[serde(default = (16))]
    b: i32,
}

The requirement of wrapping expressions in parenthesis is to ensure string values are not mistaken for a path to a function. This ensures full backwards compatibility.

I have also modified some of the tests to verify this new capability, and incremented the version number

@Threadzless
Copy link
Author

updated to match latest version

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

Successfully merging this pull request may close these issues.

1 participant