Crate struct_update[][src]

Expand description

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

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.

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,
       ...
    }
};

Macros

Struct