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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! Forwarding implementations for smart pointers and borrowed types
use cratePolicy;
use crateDeepMerge;
use ;
/// `DeepMerge` implementation for `Box<T>`.
///
/// Forwards the merge operation to the boxed value, allowing seamless
/// merging of heap-allocated values.
///
/// # Examples
///
/// ```rust
/// use std::collections::HashMap;
/// use deepmerge::prelude::*;
///
/// #[derive(DeepMerge)]
/// #[merge(policy(string = concat))]
/// struct Config {
/// name: String,
/// data: HashMap<String, i32>,
/// }
///
/// let mut boxed_config = Box::new(Config {
/// name: "app".to_string(),
/// data: [("count".to_string(), 5)].into(),
/// });
///
/// let other_config = Box::new(Config {
/// name: "_v2".to_string(),
/// data: [("users".to_string(), 100)].into(),
/// });
///
/// boxed_config.merge_with_policy(other_config, &DefaultPolicy);
///
/// assert_eq!(boxed_config.name, "app_v2");
/// assert_eq!(boxed_config.data.get("count"), Some(&5));
/// assert_eq!(boxed_config.data.get("users"), Some(&100));
/// ```
/// `DeepMerge` implementation for `Rc<T>`.
///
/// Creates a new `Rc` with the merged value since `Rc` is immutable.
/// Requires `T: Clone` to extract and clone the inner values.
///
/// # Examples
///
/// ```rust
/// use std::rc::Rc;
/// use deepmerge::prelude::*;
///
/// #[derive(DeepMerge, Clone)]
/// struct Config {
/// version: String,
/// count: i32,
/// }
///
/// let mut rc_config = Rc::new(Config {
/// version: "v1".to_string(),
/// count: 5,
/// });
///
/// let other_config = Rc::new(Config {
/// version: "v2".to_string(),
/// count: 10,
/// });
///
/// <Rc<Config> as DeepMerge<DefaultPolicy>>::merge_with_policy(&mut rc_config, other_config, &DefaultPolicy);
///
/// // Note: Rc is immutable, so merge creates a new Rc
/// assert_eq!(rc_config.version, "v2");
/// assert_eq!(rc_config.count, 10);
/// ```
/// `DeepMerge` implementation for `Arc<T>`.
///
/// Creates a new `Arc` with the merged value since `Arc` is immutable.
/// Requires `T: Clone` to extract and clone the inner values.
/// Useful for thread-safe sharing of merged configuration data.
///
/// # Examples
///
/// ```rust
/// use std::sync::Arc;
/// use std::collections::HashMap;
/// use deepmerge::prelude::*;
///
/// #[derive(DeepMerge, Clone)]
/// struct SharedConfig {
/// tags: Vec<String>,
/// settings: HashMap<String, i32>,
/// }
///
/// let mut arc_config = Arc::new(SharedConfig {
/// tags: vec!["web".to_string()],
/// settings: [("timeout".to_string(), 30)].into(),
/// });
///
/// let other_config = Arc::new(SharedConfig {
/// tags: vec!["api".to_string()],
/// settings: [("retries".to_string(), 3)].into(),
/// });
///
/// <Arc<SharedConfig> as DeepMerge<DefaultPolicy>>::merge_with_policy(&mut arc_config, other_config, &DefaultPolicy);
///
/// // Note: Arc is immutable, so merge creates a new Arc
/// // Vec merges by appending with DefaultPolicy
/// assert_eq!(arc_config.tags, vec!["web".to_string(), "api".to_string()]);
/// // HashMap overlays - both entries are present
/// assert_eq!(arc_config.settings.get("timeout"), Some(&30));
/// assert_eq!(arc_config.settings.get("retries"), Some(&3));
/// ```
/// `DeepMerge` implementation for `Cow<'_, str>`.
///
/// Converts both borrowed and owned string data to `String` for merging,
/// then stores the result as `Cow::Owned`. This allows seamless merging
/// of string data regardless of whether it's borrowed or owned.
///
/// # Examples
///
/// ```rust
/// use std::borrow::Cow;
/// use deepmerge::prelude::*;
///
/// // Concat: concatenate strings
/// let policy = ComposedPolicy::new(DefaultPolicy)
/// .with_string_merge(StringMerge::Concat);
///
/// let mut cow_str: Cow<str> = Cow::Borrowed("Hello");
/// let other_cow: Cow<str> = Cow::Owned(" World".to_string());
///
/// cow_str.merge_with_policy(other_cow, &policy);
/// assert_eq!(cow_str, "Hello World");
/// assert!(matches!(cow_str, Cow::Owned(_))); // Result is owned
///
/// // ConcatWithSep: concatenate with separator
/// let policy = ComposedPolicy::new(DefaultPolicy)
/// .with_string_merge(StringMerge::ConcatWithSep(", "));
///
/// let mut items: Cow<str> = Cow::Borrowed("apple");
/// let more_items: Cow<str> = Cow::Borrowed("banana");
///
/// items.merge_with_policy(more_items, &policy);
/// assert_eq!(items, "apple, banana");
/// ```
/// `DeepMerge` implementation for `Cow<'_, [T]>`.
///
/// Converts both borrowed and owned slice data to `Vec<T>` for merging,
/// then stores the result as `Cow::Owned`. This allows seamless merging
/// of slice data regardless of whether it's borrowed or owned.
/// Requires `T: Clone` to convert slices to vectors.
///
/// # Examples
///
/// ```rust
/// use std::borrow::Cow;
/// use deepmerge::prelude::*;
///
/// // Append: concatenate sequences
/// let policy = ComposedPolicy::new(DefaultPolicy)
/// .with_sequence_merge(SequenceMerge::Append);
///
/// let mut cow_slice: Cow<[i32]> = Cow::Borrowed(&[1, 2, 3]);
/// let other_cow: Cow<[i32]> = Cow::Owned(vec![4, 5]);
///
/// cow_slice.merge_with_policy(other_cow, &policy);
/// assert_eq!(cow_slice.as_ref(), &[1, 2, 3, 4, 5]);
/// assert!(matches!(cow_slice, Cow::Owned(_))); // Result is owned
///
/// // Prepend: add new elements at the beginning
/// let policy = ComposedPolicy::new(DefaultPolicy)
/// .with_sequence_merge(SequenceMerge::Prepend);
///
/// let mut tags: Cow<[&str]> = Cow::Borrowed(&["web", "api"]);
/// let more_tags: Cow<[&str]> = Cow::Borrowed(&["mobile"]);
///
/// tags.merge_with_policy(more_tags, &policy);
/// let result: Vec<_> = tags.iter().collect();
/// assert_eq!(result, vec![&"mobile", &"web", &"api"]);
/// ```