Trait merge::Merge

source ·
pub trait Merge {
    // Required method
    fn merge(&mut self, other: Self);
}
Expand description

A trait for objects that can be merged.

§Deriving

Merge can be derived for structs if the derive feature is enabled. The generated implementation calls the merge method for all fields, or the merge strategy function if set. You can use these field attributes to configure the generated implementation:

  • skip: Skip this field in the merge method.
  • strategy = f: Call f(self.field, other.field) instead of calling the merge function for this field.

§Examples

Using the Merge implementation for Option:

use merge::Merge as _;

let mut val = None;
val.merge(Some(42));
assert_eq!(Some(42), val);

Deriving Merge for a struct:

use merge::Merge;

#[derive(Debug, PartialEq, Merge)]
struct S {
    option: Option<usize>,

    #[merge(skip)]
    s: String,

    #[merge(strategy = merge::bool::overwrite_false)]
    flag: bool,
}

let mut val = S {
    option: None,
    s: "some ignored value".to_owned(),
    flag: false,
};
val.merge(S {
    option: Some(42),
    s: "some other ignored value".to_owned(),
    flag: true,
});
assert_eq!(S {
    option: Some(42),
    s: "some ignored value".to_owned(),
    flag: true,
}, val);

Required Methods§

source

fn merge(&mut self, other: Self)

Merge another object into this object.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<T> Merge for Option<T>

source§

fn merge(&mut self, other: Self)

Implementors§