1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//! This crate export a macro [`Struct`] to instantiate a struct with others which have common
//! fields, the usage is similar to struct update syntax, but allow to different types and must
//! provide field names.
//!
//! ## Usage
//! ```usage
//! let a = Struct! { StructA,
//!     field: value,
//!     ...
//!     [field2, field3, ...]: other,
//!     ...
//! };
//! ```
//! which expand to:
//! ```usage
//! let a = StructA {
//!    field: value,
//!    ...
//!    field2: other.field2,
//!    field3: other.field3,
//!    ...
//! };
//! ```
//! ## Example
//! ```
//! # struct A {
//! #     f1: i32,
//! #     f2: &'static str,
//! #     f3: i64,
//! #     f4: &'static str,
//! #     f5: u64,
//! # }
//! # struct B {
//! #     f3: i64,
//! #     f4: &'static str,
//! # }
//! # struct C {
//! #     f5: u64,
//! # }
//! #
//! # let b = B { f3: 5, f4: "tt" };
//! # let c = C { f5: 7 };
//! #
//! use struct_update::Struct;
//! let a = Struct! {A,
//!             f1: 3 + 5,
//!             f2: "sss",
//!             [f3, f4]: ..b,
//!             [f5]: ..c,
//!         };
//! assert_eq!(a.f1, 8);
//! assert_eq!(a.f2, "sss");
//! assert_eq!(a.f3, b.f3);
//! assert_eq!(a.f4, b.f4);
//! assert_eq!(a.f5, c.f5);
//! ```
//!
//! ## Workaroud for macro path
//! Pitifully, in macro context we cannot use path(::) to instantiate a struct, the workaround is
//! converting a path to an ident.
//! ### Usage
//! The `as _Struct` is optional, `_Struct` is the default name.
//! ```usage
//! let a = Struct! { a_crate::a_mod::StructA as _Struct,
//!     field: value,
//!     ...
//!     [field2, field3, ...]: other,
//!     ...
//! };
//! ```
//! which expand to:
//! ```usage
//! let a = {
//!     use a_crate::a_mod::StructA as _Struct;
//!     _Struct {
//!        field: value,
//!        ...
//!        field2: other.field2,
//!        field3: other.field3,
//!        ...
//!     }
//! };
//! ```
#[macro_export]
macro_rules! Struct {
    (@() -> {$S:ident $($f:ident : $v:expr,)*}) => {
        $S {
            $($f: $v,)*
        }
    };

    (@($f:ident : $v:expr, $($t:tt)*) -> {$($r:tt)*}) => {
        Struct!{
            @($($t)*) -> {
                $($r)*
                $f: $v,
            }
        }
    };

    (@([$($f:ident),*$(,)?] : ..$other:expr, $($t:tt)*) -> {$($r:tt)*}) => {
        Struct!{
            @($($t)*) -> {
                $($r)*
                $($f: $other.$f,)*
            }
        }
    };


    ($S:ident , $($f:ident : $v:expr,)* $([$($f2:ident),*$(,)?] : ..$other:expr,)*) => {
        $S {
            $($f: $v,)*
            $(
                $($f2: $other.$f2,)*
            )*
        }
    };

    ($S:ident, $($t:tt)*) => {
        Struct!{@($($t)*) -> { $S }}
    };

    ($P:path as $S:ident, $($t:tt)*) => {
        {
            use $P as $S;
            Struct!{@($($t)*) -> { $S }}
        }
    };

    ($P:path, $($t:tt)*) => {
        {
            use $P as _Struct;
            Struct!{@($($t)*) -> { _Struct }}
        }
    };
}