macro serde_default

Generate a Default impl using serde attributes

2 unstable releases

0.2.0 Jul 8, 2024
0.1.0 Jan 14, 2022

#172 in Procedural macros

Download history 7552/week @ 2024-07-25 6017/week @ 2024-08-01 8610/week @ 2024-08-08 8043/week @ 2024-08-15 5536/week @ 2024-08-22 5738/week @ 2024-08-29 6630/week @ 2024-09-05 10053/week @ 2024-09-12 8235/week @ 2024-09-19 8258/week @ 2024-09-26 8192/week @ 2024-10-03 9269/week @ 2024-10-10 11289/week @ 2024-10-17 8913/week @ 2024-10-24 8784/week @ 2024-10-31 11447/week @ 2024-11-07

42,403 downloads per month
Used in 6 crates (5 directly)

MIT license

11KB
191 lines

serde_default

This is a library for generating impls of Default from serde attributes.


lib.rs:

Generate a Default impl based on field-level defaults in serde attributes.

Usage

On a struct that derives Serialize or Deserialize, add DefaultFromSerde.

#[derive(Debug, DefaultFromSerde, PartialEq, Eq)]
pub struct MyStruct {
    #[serde(default = "field_1_default")]
    field1: u16,
    #[serde(default)]
    field2: String,
}

fn field_1_default() -> u16 {
    3
}

fn main() {
    assert_eq!(MyStruct::default(), MyStruct { field1: 3, field2: "".into() });
}

Dependencies

~0.6–1MB
~24K SLoC