2 releases

0.1.1 Oct 11, 2023
0.1.0 Oct 9, 2023

#825 in Procedural macros

Download history 1345/week @ 2025-03-10 1562/week @ 2025-03-17 1567/week @ 2025-03-24 1703/week @ 2025-03-31 2079/week @ 2025-04-07 1792/week @ 2025-04-14 1696/week @ 2025-04-21 1480/week @ 2025-04-28 2081/week @ 2025-05-05 1951/week @ 2025-05-12 1267/week @ 2025-05-19 1198/week @ 2025-05-26 1768/week @ 2025-06-02 1418/week @ 2025-06-09 1201/week @ 2025-06-16 1421/week @ 2025-06-23

5,835 downloads per month
Used in 5 crates (3 directly)

Apache-2.0

8KB
107 lines

Latest Version

Usage

[dependencies]
serde_derive_default = "0.1"
#[derive(Deserialize, serde_derive_default::Default)]
struct MyStruct {
    
}

Problem

When using serde defaulting users currently have to manually create a Default implementations that matches the serde field level annotations.

If you use the regular #[derive(Default)], it you will get unexpected results.

For example:

#[derive(Deserialize)]
struct Container {
    a: A,
}
#[derive(Deserialize)]
struct A {
    #[serde(default)]
    b: B,
}

#[derive(Deserialize, Default)]
struct B {
    #[serde(default = "true_fn")]
    c: bool,
}

fn true_fn() -> bool {
    true
}

fn main() {
    let container1 = serde_yaml::from_str::<Container>("a: {}").unwrap();
    let container2 = serde_yaml::from_str::<Container>("a: {b: {}}").unwrap();
    if container1.a.b.c == container2.a.b.c {
        println!("serde and Default match!");
    } else {
        println!("serde and Default do not match, this is a bug!");
    }
}
    

The output is:

serde and Default do not match, this is a bug!

This is because the implementation of Default disagrees with the serde defaults.

If instead serde_serive_default::Default is used it will use the same annotations used by serde to create the default implementation:

#[derive(Deserialize, serde_serive_default::Default)]
struct B {
    #[serde(default = "true_fn")]
    c: bool,
}

The output is:

serde and Default match!

Note that tha above problem only manifests when using field level annotations. If you are using container level #[serde(default)] then the regular #[derive(Default)] or a manual implementation of Default will work as expected.

Dependencies

~2.2–3.5MB
~67K SLoC