inline_default/
lib.rs

1/// Derive Default within struct definition.
2/// See README for details.
3#[macro_export]
4macro_rules! inline_default {
5    (
6        $(
7            $(#[$s_at:meta])* // attributes
8            $s_v:vis struct $s_nm:ident //struct name, vis
9            $(< $($s_lt:lifetime,)* $( $s_gen:ident $(: $s_tr:ident)? ),* $(,)? >)? // generics
10            { $( $(#[$f_at:meta])* $f_v:vis $f_nm:ident : $f_ty:ty $(= $def:expr)? ),* $(,)? } // definition
11        )*
12    ) => {
13        $(
14            $(#[$s_at])*
15            $s_v struct $s_nm $(< $($s_lt ,)* $($s_gen $(: $s_tr)? ),* >)? {
16                $( $(#[$f_at])* $f_v $f_nm : $f_ty ),*
17            }
18
19            macro_rules! __default_expand {
20                ($t:ty, $d:expr) => { $d };
21                ($t:ty,) => { <$t as Default>::default() }
22            }
23
24            impl $(< $($s_lt ,)* $($s_gen $(: $s_tr)?),* >)? Default for $s_nm $(< $($s_lt ,)* $($s_gen),* >)? {
25                fn default() -> Self {
26                    $(let $f_nm = __default_expand!( $f_ty, $( $def )? );)*
27                    Self { $($f_nm,)* }
28                }
29            }
30        )*
31    }
32}