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
let a = Struct! { StructA,
field: value,
...
[field2, field3, ...]: other,
...
};
which expand to:
let a = StructA {
field: value,
...
field2: other.field2,
field3: other.field3,
...
};
Example
let b = B ;
let c = C ;
use Struct;
let a = Struct! ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
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.
let a = Struct! { a_crate::a_mod::StructA as _Struct,
field: value,
...
[field2, field3, ...]: other,
...
};
which expand to:
let a = {
use a_crate::a_mod::StructA as _Struct;
_Struct {
field: value,
...
field2: other.field2,
field3: other.field3,
...
}
};