Skip to content

Commit 993c4c0

Browse files
committed
feat(chain,core)!: move Merge to bdk_core
1 parent 2cf46a2 commit 993c4c0

File tree

3 files changed

+85
-82
lines changed

3 files changed

+85
-82
lines changed

crates/chain/src/tx_data_traits.rs

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::collections::{BTreeMap, BTreeSet};
21
use crate::{BlockId, ConfirmationBlockTime};
3-
use alloc::vec::Vec;
42

53
/// Trait that "anchors" blockchain data to a specific block of height and hash.
64
///
@@ -121,83 +119,3 @@ impl AnchorFromBlockPosition for ConfirmationBlockTime {
121119
}
122120
}
123121
}
124-
125-
/// Trait that makes an object mergeable.
126-
pub trait Merge: Default {
127-
/// Merge another object of the same type onto `self`.
128-
fn merge(&mut self, other: Self);
129-
130-
/// Returns whether the structure is considered empty.
131-
fn is_empty(&self) -> bool;
132-
133-
/// Take the value, replacing it with the default value.
134-
fn take(&mut self) -> Option<Self> {
135-
if self.is_empty() {
136-
None
137-
} else {
138-
Some(core::mem::take(self))
139-
}
140-
}
141-
}
142-
143-
impl<K: Ord, V> Merge for BTreeMap<K, V> {
144-
fn merge(&mut self, other: Self) {
145-
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
146-
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
147-
BTreeMap::extend(self, other)
148-
}
149-
150-
fn is_empty(&self) -> bool {
151-
BTreeMap::is_empty(self)
152-
}
153-
}
154-
155-
impl<T: Ord> Merge for BTreeSet<T> {
156-
fn merge(&mut self, other: Self) {
157-
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
158-
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
159-
BTreeSet::extend(self, other)
160-
}
161-
162-
fn is_empty(&self) -> bool {
163-
BTreeSet::is_empty(self)
164-
}
165-
}
166-
167-
impl<T> Merge for Vec<T> {
168-
fn merge(&mut self, mut other: Self) {
169-
Vec::append(self, &mut other)
170-
}
171-
172-
fn is_empty(&self) -> bool {
173-
Vec::is_empty(self)
174-
}
175-
}
176-
177-
macro_rules! impl_merge_for_tuple {
178-
($($a:ident $b:tt)*) => {
179-
impl<$($a),*> Merge for ($($a,)*) where $($a: Merge),* {
180-
181-
fn merge(&mut self, _other: Self) {
182-
$(Merge::merge(&mut self.$b, _other.$b) );*
183-
}
184-
185-
fn is_empty(&self) -> bool {
186-
$(Merge::is_empty(&self.$b) && )* true
187-
}
188-
}
189-
}
190-
}
191-
192-
impl_merge_for_tuple!();
193-
impl_merge_for_tuple!(T0 0);
194-
impl_merge_for_tuple!(T0 0 T1 1);
195-
impl_merge_for_tuple!(T0 0 T1 1 T2 2);
196-
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3);
197-
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4);
198-
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5);
199-
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6);
200-
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7);
201-
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8);
202-
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9);
203-
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10);

crates/core/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,7 @@ pub use checkpoint::*;
6868
mod tx_update;
6969
pub use tx_update::*;
7070

71+
mod merge;
72+
pub use merge::*;
73+
7174
pub mod spk_client;

crates/core/src/merge.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use crate::alloc::vec::Vec;
2+
use crate::collections::{BTreeMap, BTreeSet};
3+
4+
/// Trait that makes an object mergeable.
5+
pub trait Merge: Default {
6+
/// Merge another object of the same type onto `self`.
7+
fn merge(&mut self, other: Self);
8+
9+
/// Returns whether the structure is considered empty.
10+
fn is_empty(&self) -> bool;
11+
12+
/// Take the value, replacing it with the default value.
13+
fn take(&mut self) -> Option<Self> {
14+
if self.is_empty() {
15+
None
16+
} else {
17+
Some(core::mem::take(self))
18+
}
19+
}
20+
}
21+
22+
impl<K: Ord, V> Merge for BTreeMap<K, V> {
23+
fn merge(&mut self, other: Self) {
24+
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
25+
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
26+
BTreeMap::extend(self, other)
27+
}
28+
29+
fn is_empty(&self) -> bool {
30+
BTreeMap::is_empty(self)
31+
}
32+
}
33+
34+
impl<T: Ord> Merge for BTreeSet<T> {
35+
fn merge(&mut self, other: Self) {
36+
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
37+
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
38+
BTreeSet::extend(self, other)
39+
}
40+
41+
fn is_empty(&self) -> bool {
42+
BTreeSet::is_empty(self)
43+
}
44+
}
45+
46+
impl<T> Merge for Vec<T> {
47+
fn merge(&mut self, mut other: Self) {
48+
Vec::append(self, &mut other)
49+
}
50+
51+
fn is_empty(&self) -> bool {
52+
Vec::is_empty(self)
53+
}
54+
}
55+
56+
macro_rules! impl_merge_for_tuple {
57+
($($a:ident $b:tt)*) => {
58+
impl<$($a),*> Merge for ($($a,)*) where $($a: Merge),* {
59+
60+
fn merge(&mut self, _other: Self) {
61+
$(Merge::merge(&mut self.$b, _other.$b) );*
62+
}
63+
64+
fn is_empty(&self) -> bool {
65+
$(Merge::is_empty(&self.$b) && )* true
66+
}
67+
}
68+
}
69+
}
70+
71+
impl_merge_for_tuple!();
72+
impl_merge_for_tuple!(T0 0);
73+
impl_merge_for_tuple!(T0 0 T1 1);
74+
impl_merge_for_tuple!(T0 0 T1 1 T2 2);
75+
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3);
76+
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4);
77+
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5);
78+
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6);
79+
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7);
80+
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8);
81+
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9);
82+
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10);

0 commit comments

Comments
 (0)