Skip to content

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