Derive Macro PartialDefault

Source
#[derive(PartialDefault)]
{
    // Attributes available to this derive:
    #[partial_default]
}
Available on crate feature derive only.
Expand description

Derive the PartialDefault trait.

The value used for a field can be overridden using the #[partial_default(value = "alternative()")] syntax, where alternative() is a Rust expression that evaluates to the correct type.

By default, the derived implementation will add a T: PartialDefault trait for every generic parameter, like the built-in derive(Default). You can override this by adding #[partial_default(bound = "T: MyTrait")] to the type, which replaces any inferred bounds. Use an empty string to impose no restrictions at all.

ยงExamples

use partial_default::PartialDefault;

#[derive(PartialDefault)]
#[partial_default(bound = "")]
enum Foo<T> {
    Bar,
    #[partial_default]
    Baz {
        #[partial_default(value = "12")]
        a: i32,
        b: i32,
        #[partial_default(value = "Some(Default::default())")]
        c: Option<i32>,
        #[partial_default(value = "vec![1, 2, 3]")]
        d: Vec<u32>,
        #[partial_default(value = r#""four".to_owned()"#)]
        e: String,
    },
    Qux(T),
}

assert!(Foo::<&u8>::partial_default() == Foo::<&u8>::Baz {
    a: 12,
    b: 0,
    c: Some(0),
    d: vec![1, 2, 3],
    e: "four".to_owned(),
});