1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
//! # Merge Behavior
//!
//! The following will explain the merge behavior on the example of a single field.
//!
//! ## Merge behavior of `merge` and `merge_ref`
//!
//! #### Same Type
//!
//! ```rust,ignore
//! struct Src {
//! test: T
//! }
//! struct Target {
//! test: T
//! }
//! ```
//!
//! This will simply merge `src.test` into `target.test`: \
//! `target.test = src.test`
//!
//! #### Target is Optional
//!
//! ```rust,ignore
//! struct Src {
//! test: T
//! }
//! struct Target {
//! test: Option<T>
//! }
//! ```
//!
//! This will wrap `src.test` into an `Option` and merge it into `target.test`: \
//! `target.test = Some(src.test);`
//!
//! #### Source is Optional
//!
//! ```rust,ignore
//! struct Src {
//! test: Option<T>
//! }
//! struct Target {
//! test: T
//! }
//! ```
//!
//! This will only merge `src.test` into `target.test` if `src.test` is `Some`: \
//! ```rust,ignore
//! if let Some(value) = src.test {
//! target.test = value;
//! }
//! ```
/// Merge another struct into `Self`.
pub trait StructMerge<Src> {
/// Merge the given struct into `Self` whilst consuming it.
fn merge(&mut self, src: Src);
}
/// Counterpart of [StructMerge].
/// This will merge `Self` into a given target.
pub trait StructMergeInto<Target: ?Sized> {
/// Check the [StructMerge::merge] docs.
fn merge_into(self, target: &mut Target);
}
/// Implement the [StructMerge] trait for all types that provide [StructMergeInto] for it.
impl<Target, Src: StructMergeInto<Target>> StructMerge<Src> for Target {
fn merge(&mut self, src: Src) {
src.merge_into(self);
}
}
/// Merge another borrowed struct into `Self`.
///
/// All fields to be merged on the borrowed struct have to implement [Clone].
pub trait StructMergeRef<Src> {
/// Merge the given struct into `Self`.
fn merge_ref(&mut self, src: &Src);
}
/// Counterpart of [StructMergeRef].
/// This will merge `&Self` into a given target.
pub trait StructMergeIntoRef<Target: ?Sized> {
/// Check the [StructMergeRef::merge_ref] docs.
fn merge_into_ref(&self, target: &mut Target);
}
/// Implement the [StructMergeRef] trait for all types that provide [StructMergeInto] for it.
impl<Target, Src: StructMergeIntoRef<Target>> StructMergeRef<Src> for Target {
fn merge_ref(&mut self, src: &Src) {
src.merge_into_ref(self);
}
}