Skip to content

Commit e4ee629

Browse files
committed
Merge #1625: feat(chain,core)!: move Merge to bdk_core
a4cf905 feat(file_store): rm `bdk_chain` dependency (志宇) 993c4c0 feat(chain,core)!: move `Merge` to `bdk_core` (志宇) Pull request description: Fixes #1624 ### Description Moving `Merge` into `bdk_core` (from `bdk_chain`) allows persist crates to only depend on `bdk_core`. ### Changelog notice * Move `Merge` into `bdk_core`. * Remove `bdk_chain` dependency from `bdk_file_store`. ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing ACKs for top commit: LagginTimes: ACK a4cf905 oleonardolima: ACK a4cf905 ValuedMammal: ACK a4cf905 notmandatory: ACK a4cf905 Tree-SHA512: a94e24e04929cf790defd7ca0c8b5194eb5e9b31d295a55f5d3326d3eeb46821eda73191f2f68d8bb7e178bacd2cccfa45c8ded0ce50aabd4b7293edb338c956
2 parents d1f453e + a4cf905 commit e4ee629

File tree

5 files changed

+87
-84
lines changed

5 files changed

+87
-84
lines changed

crates/chain/src/tx_data_traits.rs

-82
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

+3
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

+82
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);

crates/file_store/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ readme = "README.md"
1414
workspace = true
1515

1616
[dependencies]
17-
bdk_chain = { path = "../chain", version = "0.19.0", features = [ "serde", "miniscript" ] }
17+
bdk_core = { path = "../core", version = "0.2.0", features = ["serde"]}
1818
bincode = { version = "1" }
1919
serde = { version = "1", features = ["derive"] }
2020

crates/file_store/src/store.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{bincode_options, EntryIter, FileError, IterError};
2-
use bdk_chain::Merge;
2+
use bdk_core::Merge;
33
use bincode::Options;
44
use std::{
55
fmt::{self, Debug},

0 commit comments

Comments
 (0)