Skip to content

Commit 189bbfc

Browse files
committed
Allow merging markmaps with different value types if there is a From impl
1 parent 3718507 commit 189bbfc

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

cfgrammar/src/lib/markmap.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,9 @@ impl<K: Ord + Clone, V> MarkMap<K, V> {
369369
/// For the behavior of exclusive or mark the behavior as also `Mark::Required`, then after merge call `missing()`
370370
/// to check all required values.
371371
#[doc(hidden)]
372-
pub fn merge_from(&mut self, mut other: Self) -> Result<(), MergeError<K, Box<V>>> {
372+
pub fn merge_from<U>(&mut self, mut other: MarkMap<K, U>) -> Result<(), MergeError<K, Box<V>>>
373+
where V: From<U>
374+
{
373375
for (their_key, their_mark, their_val) in other.contents.drain(..) {
374376
let pos = self.contents.binary_search_by(|x| x.0.cmp(&their_key));
375377
match pos {
@@ -388,21 +390,21 @@ impl<K: Ord + Clone, V> MarkMap<K, V> {
388390
if my_val.is_some() && their_val.is_some() {
389391
return Err(MergeError::Exclusivity(
390392
their_key,
391-
Box::new(their_val.unwrap()),
393+
Box::new(their_val.unwrap().into()),
392394
));
393395
} else if my_val.is_none() {
394-
*my_val = their_val;
396+
*my_val = their_val.map(V::from);
395397
}
396398
} else if merge_behavior == theirs_mark && their_val.is_some()
397399
|| merge_behavior == ours_mark && my_val.is_none()
398400
{
399401
*my_mark = their_mark;
400-
*my_val = their_val;
402+
*my_val = their_val.map(V::from);
401403
}
402404
}
403405
Err(pos) => {
404406
self.contents
405-
.insert(pos, (their_key, their_mark, their_val));
407+
.insert(pos, (their_key, their_mark, their_val.map(V::from)));
406408
}
407409
}
408410
}
@@ -696,7 +698,7 @@ mod test {
696698
fn test_merge_empty() {
697699
{
698700
let mut ours: MarkMap<&str, &str> = MarkMap::new();
699-
let theirs = MarkMap::new();
701+
let theirs: MarkMap<&str, &str> = MarkMap::new();
700702
assert!(ours.merge_from(theirs).is_ok());
701703
}
702704
{

0 commit comments

Comments
 (0)