Merge

Trait Merge 

Source
pub trait Merge: Sized {
    // Required method
    fn merge(&mut self, other: &mut 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.

You can also set a default strategy for all fields by setting the strategy attribute for the struct.

§Examples

Deriving Merge for a struct:

use merge2::Merge;

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

    #[merge(skip)]
    s: String,

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

let mut val = S {
    option: None,
    s: "some ignored value".to_owned(),
    flag: false,
};
val.merge(&mut 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);

Setting a default merge strategy:

use merge2::Merge;

#[derive(Debug, PartialEq, Merge)]
struct S {
    option1: Option<usize>,
    option2: Option<usize>,
    option3: Option<usize>,
}

let mut val = S {
    option1: None,
    option2: Some(1),
    option3: None,
};
val.merge(&mut S {
    option1: Some(2),
    option2: Some(2),
    option3: None,
});
assert_eq!(S {
    option1: Some(2),
    option2: Some(1),
    option3: None,
}, val);

Required Methods§

Source

fn merge(&mut self, other: &mut Self)

Merge another object into this object

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Merge for &str

Available on crate feature std only.
Source§

fn merge(&mut self, right: &mut Self)

Source§

impl Merge for bool

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for f32

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for f64

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for i8

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for i16

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for i32

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for i64

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for i128

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for isize

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for u8

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for u16

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for u32

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for u64

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for u128

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for usize

Source§

fn merge(&mut self, _: &mut Self)

Source§

impl Merge for String

Available on crate feature std only.
Source§

fn merge(&mut self, right: &mut Self)

Source§

impl<K, V> Merge for HashMap<K, V>

Available on crate feature std only.
Source§

fn merge(&mut self, right: &mut Self)

Source§

impl<T> Merge for Option<T>

Source§

fn merge(&mut self, right: &mut Self)

Overwrite Option only if it is None

Source§

impl<T> Merge for Vec<T>

Available on crate feature std only.
Source§

fn merge(&mut self, right: &mut Self)

Implementors§