object_merge/
lib.rs

1use std::hash::Hash;
2
3mod imp;
4
5/// Combine a type with a template of what values to use when they're unspecified
6/// in the `self` instance. On containers like maps, this will recursively call
7/// merge for elements that have the same key.
8pub trait Merge {
9    fn merge(&mut self, template: &Self);
10}
11
12/// Used for types like `Option` when no recursive merging should be performed.
13pub trait ShallowMerge {
14    fn shallow_merge(&mut self, template: &Self);
15}
16
17/// Combine two containers by combining all of their elements. This does not deduplicate
18/// anything for types which support multiple values of the same key.
19pub trait Combine {
20    fn combine(&mut self, template: &Self);
21}
22
23/// Combine two map-like containers by recursively calling Merge on elements that
24/// have the same key, and adding any missing key values from the template.
25pub trait MergeCombine {
26    fn merge_combine(&mut self, template: &Self);
27}
28
29/// Merge the elements in two containers using a custom function for getting the key
30/// that should be used for each element. This allows merging Vec<T>.
31pub trait MergeByKey {
32    type Elem;
33
34    fn merge_by_key<F, K>(&mut self, template: &Self, get_key: F)
35    where
36        F: FnMut(&Self::Elem) -> K,
37        K: Hash + Eq;
38}
39
40/// Combine and deduplicate items in a container that allows duplicates normally (such as Vec<T>)
41/// while preserving the original element order.
42pub trait CombineByKey {
43    type Elem;
44
45    fn combine_by_key<F, K>(&mut self, template: &Self, get_key: F)
46    where
47        F: FnMut(&Self::Elem) -> K,
48        K: Hash + Eq;
49}
50
51/// Merge and combine the elements in two containers using a custom function for getting
52/// the key that should be used for each element. This allows merging and combining Vec<T>.
53pub trait MergeCombineByKey {
54    type Elem;
55
56    fn merge_combine_by_key<F, K>(&mut self, template: &Self, get_key: F)
57    where
58        F: FnMut(&Self::Elem) -> K,
59        K: Hash + Eq;
60}