-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(strategy): implement merge strategies for BTreeMap (#13)
* feat: implement strategies for btreemap Signed-off-by: simonsan <[email protected]> * fix: clippy Signed-off-by: simonsan <[email protected]> * change method names Signed-off-by: simonsan <[email protected]> * adapt test names Signed-off-by: simonsan <[email protected]> * corrections --------- Signed-off-by: simonsan <[email protected]> Co-authored-by: Alexander Weiss <[email protected]>
- Loading branch information
Showing
26 changed files
with
206 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//! Merge strategies for `BTreeMap`s. | ||
//! | ||
//! These strategies are only available if the `std` feature is enabled. | ||
use std::collections::BTreeMap; | ||
|
||
/// Append values, on conflict, overwrite elements of `left` with `right`. | ||
/// | ||
/// In other words, this gives precedence to `right`. | ||
pub fn append_or_overwrite<K: Eq + Ord, V>(left: &mut BTreeMap<K, V>, right: BTreeMap<K, V>) { | ||
left.extend(right) | ||
} | ||
|
||
/// Append values, on conflict, ignore elements from `right`. | ||
/// | ||
/// In other words, this gives precedence to `left`. | ||
pub fn append_or_ignore<K: Eq + Ord, V>(left: &mut BTreeMap<K, V>, right: BTreeMap<K, V>) { | ||
for (k, v) in right { | ||
left.entry(k).or_insert(v); | ||
} | ||
} | ||
|
||
/// Append values, on conflict, recursively merge the elements. | ||
pub fn append_or_recurse<K: Eq + Ord, V: crate::Merge>( | ||
left: &mut BTreeMap<K, V>, | ||
right: BTreeMap<K, V>, | ||
) { | ||
use std::collections::btree_map::Entry; | ||
|
||
for (k, v) in right { | ||
match left.entry(k) { | ||
Entry::Occupied(mut existing) => existing.get_mut().merge(v), | ||
Entry::Vacant(empty) => { | ||
empty.insert(v); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
error: conflate::Merge can only be derived for structs | ||
--> tests/compile/derive-enum.rs:6:10 | ||
| | ||
6 | #[derive(Merge)] | ||
| ^^^^^ | ||
| | ||
= note: this error originates in the derive macro `Merge` (in Nightly builds, run with -Z macro-backtrace for more info) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions
23
crates/conflate/tests/compile/derive-invalid-default-strategy.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
error[E0308]: arguments to this function are incorrect | ||
--> tests/compile/derive-invalid-default-strategy.rs:7:20 | ||
| | ||
7 | #[merge(strategy = my_custom_merge_strategy)] | ||
| ____________________-^^^^^^^^^^^^^^^^^^^^^^^ | ||
8 | | struct S { | ||
9 | | field1: u16, | ||
| | - | ||
| | | | ||
| |__________expected `&mut u8`, found `&mut u16` | ||
| expected `u8`, found `u16` | ||
| | ||
= note: expected mutable reference `&mut u8` | ||
found mutable reference `&mut u16` | ||
note: function defined here | ||
--> tests/compile/derive-invalid-default-strategy.rs:12:4 | ||
| | ||
12 | fn my_custom_merge_strategy(left: &mut u8, right: u8) { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ ------------- --------- | ||
help: you can convert a `u16` to a `u8` and panic if the converted value doesn't fit | ||
| | ||
9 | field1.try_into().unwrap(): u16, | ||
| ++++++++++++++++++++ |
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions
20
crates/conflate/tests/compile/derive-invalid-strategy.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error[E0308]: mismatched types | ||
--> tests/compile/derive-invalid-strategy.rs:8:24 | ||
| | ||
8 | #[merge(strategy = my_custom_merge_strategy)] | ||
| ^----------------------- | ||
| | | ||
| ________________________arguments to this function are incorrect | ||
| | | ||
9 | | field1: u8, | ||
| |__________^ expected `u8`, found `&mut u8` | ||
| | ||
note: function defined here | ||
--> tests/compile/derive-invalid-strategy.rs:12:4 | ||
| | ||
12 | fn my_custom_merge_strategy(left: u8, right: u8) -> u8 { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ -------- | ||
help: consider removing the borrow | ||
| | ||
8 | #[merge(strategy = my_custom_merge_strategy)] | ||
| |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: expected `=` | ||
--> tests/compile/derive-no-strategy.rs:8:21 | ||
| | ||
8 | #[merge(strategy)] | ||
| ^ |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error[E0277]: the trait bound `Option<u8>: Merge` is not satisfied | ||
--> tests/compile/derive-u8.rs:8:5 | ||
| | ||
8 | field1: Option<u8>, | ||
| ^^^^^^ the trait `Merge` is not implemented for `Option<u8>` | ||
| | ||
= help: the trait `Merge` is implemented for `S` | ||
|
||
error[E0277]: the trait bound `u8: Merge` is not satisfied | ||
--> tests/compile/derive-u8.rs:9:5 | ||
| | ||
9 | field2: u8, | ||
| ^^^^^^ the trait `Merge` is not implemented for `u8` | ||
| | ||
= help: the trait `Merge` is implemented for `S` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.