Skip to main content

impl_merge

Macro impl_merge 

Source
macro_rules! impl_merge {
    (
        $struct_name:ident {
            $(option { $($opt_field:ident),* $(,)? })?
            $(nested { $($nest_field:ident),* $(,)? })?
            $(optional_nested { $($on_field:ident),* $(,)? })?
        }
    ) => { ... };
}
Expand description

Generates merge() and is_empty() methods for theme structs.

Three field categories:

  • option { field1, field2, ... } โ€“ Option<T> leaf fields
  • nested { field1, field2, ... } โ€“ nested struct fields with their own merge()
  • optional_nested { field1, field2, ... } โ€“ Option<T> where T has its own merge()

For option fields, Some values in the overlay replace the corresponding fields in self; None fields are left unchanged. For nested fields, merge is called recursively. For optional_nested fields: if both base and overlay are Some, the inner values are merged recursively. If base is None and overlay is Some, overlay is cloned. If overlay is None, the base field is preserved unchanged.

ยงExamples

use native_theme::impl_merge;

#[derive(Clone, Debug, Default)]
struct MyColors {
    accent: Option<String>,
    background: Option<String>,
}

impl_merge!(MyColors {
    option { accent, background }
});

let mut base = MyColors { accent: Some("blue".into()), background: None };
let overlay = MyColors { accent: None, background: Some("white".into()) };
base.merge(&overlay);
assert_eq!(base.accent.as_deref(), Some("blue"));
assert_eq!(base.background.as_deref(), Some("white"));