#default #automatic #insert #struct #literals #proc-macro #derive-debug

macro autodefault

A proc macro that automatically inserts ..Default::default() into your struct literals

3 stable releases

2.0.0 Apr 11, 2021
1.1.0 Apr 10, 2021
1.0.0 Apr 10, 2021

#1331 in Procedural macros

Download history 23/week @ 2024-03-11 21/week @ 2024-03-18 58/week @ 2024-03-25 40/week @ 2024-04-01 12/week @ 2024-04-08 20/week @ 2024-04-15 21/week @ 2024-04-22 17/week @ 2024-04-29 10/week @ 2024-05-06 19/week @ 2024-05-13 18/week @ 2024-05-20 22/week @ 2024-05-27 16/week @ 2024-06-03 17/week @ 2024-06-10 15/week @ 2024-06-17 20/week @ 2024-06-24

71 downloads per month

MPL-2.0 license

18KB
259 lines

Crates.io Crates.io docs.rs

autodefault

A library that automatically inserts ..Default::default() for you.

The pitch

Has this ever happened to you?

#[derive(Debug, Default, PartialEq, Eq)]
struct Inner {
    x: i32,
    y: i32,
    z: i32,
}

#[derive(Debug, Default, PartialEq, Eq)]
struct Mid {
    a: Inner,
    b: Inner,
    c: Inner,
    d: Inner
}

#[derive(Debug, Default, PartialEq, Eq)]
struct Outer {
    mid1: Mid,
    mid2: Mid,
    mid3: Mid,
    mid4: Mid,
}

fn build_outer() -> Outer {
    Outer {
        mid1: Mid {
            a: Inner {
                x: 10,
                ..Default::default()  // :D
            },
            b: Inner {
                y: 10,
                ..Default::default()  // :)
            },
            ..Default::default()  // :|
        },
        mid2: Mid {
            b: Inner {
                z: 10,
                ..Default::default()  // :/
            },
            ..Default::default()  // :(
        },
        ..Default::default()  // >:(
    }
}

Wouldn't it be nice if you could omit all the tedious ..Default::default() calls when building deeply nested struct literals? Now you can! With autodefault, it's never been easier to build up a large struct literal for your tests, bevy components, or anything else you might need!. Simply tag any function with the #[autodefault] attribute and let us handle the rest:

use autodefault::autodefault;

#[autodefault]
fn build_outer_simple() -> Outer {
    Outer {
        mid1: Mid {
            a: Inner { x: 10 },
            b: Inner { y: 10 },
        },
        mid2: Mid {
            b: Inner { z: 10 },
        }
    }
}  // :O

assert_eq!(build_outer(), build_outer_simple())

It's never been easier! Check out the reference documentation for more details.

Dependencies

~1.5MB
~35K SLoC