Skip to content

Commit

Permalink
Merge #1625: feat(chain,core)!: move Merge to bdk_core
Browse files Browse the repository at this point in the history
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
  • Loading branch information
notmandatory committed Oct 1, 2024
2 parents d1f453e + a4cf905 commit e4ee629
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 84 deletions.
82 changes: 0 additions & 82 deletions crates/chain/src/tx_data_traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::collections::{BTreeMap, BTreeSet};
use crate::{BlockId, ConfirmationBlockTime};
use alloc::vec::Vec;

/// Trait that "anchors" blockchain data to a specific block of height and hash.
///
Expand Down Expand Up @@ -121,83 +119,3 @@ impl AnchorFromBlockPosition for ConfirmationBlockTime {
}
}
}

/// Trait that makes an object mergeable.
pub trait Merge: Default {
/// Merge another object of the same type onto `self`.
fn merge(&mut self, other: Self);

/// Returns whether the structure is considered empty.
fn is_empty(&self) -> bool;

/// Take the value, replacing it with the default value.
fn take(&mut self) -> Option<Self> {
if self.is_empty() {
None
} else {
Some(core::mem::take(self))
}
}
}

impl<K: Ord, V> Merge for BTreeMap<K, V> {
fn merge(&mut self, other: Self) {
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
BTreeMap::extend(self, other)
}

fn is_empty(&self) -> bool {
BTreeMap::is_empty(self)
}
}

impl<T: Ord> Merge for BTreeSet<T> {
fn merge(&mut self, other: Self) {
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
BTreeSet::extend(self, other)
}

fn is_empty(&self) -> bool {
BTreeSet::is_empty(self)
}
}

impl<T> Merge for Vec<T> {
fn merge(&mut self, mut other: Self) {
Vec::append(self, &mut other)
}

fn is_empty(&self) -> bool {
Vec::is_empty(self)
}
}

macro_rules! impl_merge_for_tuple {
($($a:ident $b:tt)*) => {
impl<$($a),*> Merge for ($($a,)*) where $($a: Merge),* {

fn merge(&mut self, _other: Self) {
$(Merge::merge(&mut self.$b, _other.$b) );*
}

fn is_empty(&self) -> bool {
$(Merge::is_empty(&self.$b) && )* true
}
}
}
}

impl_merge_for_tuple!();
impl_merge_for_tuple!(T0 0);
impl_merge_for_tuple!(T0 0 T1 1);
impl_merge_for_tuple!(T0 0 T1 1 T2 2);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9);
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);
3 changes: 3 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,7 @@ pub use checkpoint::*;
mod tx_update;
pub use tx_update::*;

mod merge;
pub use merge::*;

pub mod spk_client;
82 changes: 82 additions & 0 deletions crates/core/src/merge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use crate::alloc::vec::Vec;
use crate::collections::{BTreeMap, BTreeSet};

/// Trait that makes an object mergeable.
pub trait Merge: Default {
/// Merge another object of the same type onto `self`.
fn merge(&mut self, other: Self);

/// Returns whether the structure is considered empty.
fn is_empty(&self) -> bool;

/// Take the value, replacing it with the default value.
fn take(&mut self) -> Option<Self> {
if self.is_empty() {
None
} else {
Some(core::mem::take(self))
}
}
}

impl<K: Ord, V> Merge for BTreeMap<K, V> {
fn merge(&mut self, other: Self) {
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
BTreeMap::extend(self, other)
}

fn is_empty(&self) -> bool {
BTreeMap::is_empty(self)
}
}

impl<T: Ord> Merge for BTreeSet<T> {
fn merge(&mut self, other: Self) {
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
BTreeSet::extend(self, other)
}

fn is_empty(&self) -> bool {
BTreeSet::is_empty(self)
}
}

impl<T> Merge for Vec<T> {
fn merge(&mut self, mut other: Self) {
Vec::append(self, &mut other)
}

fn is_empty(&self) -> bool {
Vec::is_empty(self)
}
}

macro_rules! impl_merge_for_tuple {
($($a:ident $b:tt)*) => {
impl<$($a),*> Merge for ($($a,)*) where $($a: Merge),* {

fn merge(&mut self, _other: Self) {
$(Merge::merge(&mut self.$b, _other.$b) );*
}

fn is_empty(&self) -> bool {
$(Merge::is_empty(&self.$b) && )* true
}
}
}
}

impl_merge_for_tuple!();
impl_merge_for_tuple!(T0 0);
impl_merge_for_tuple!(T0 0 T1 1);
impl_merge_for_tuple!(T0 0 T1 1 T2 2);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9);
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);
2 changes: 1 addition & 1 deletion crates/file_store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "README.md"
workspace = true

[dependencies]
bdk_chain = { path = "../chain", version = "0.19.0", features = [ "serde", "miniscript" ] }
bdk_core = { path = "../core", version = "0.2.0", features = ["serde"]}
bincode = { version = "1" }
serde = { version = "1", features = ["derive"] }

Expand Down
2 changes: 1 addition & 1 deletion crates/file_store/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{bincode_options, EntryIter, FileError, IterError};
use bdk_chain::Merge;
use bdk_core::Merge;
use bincode::Options;
use std::{
fmt::{self, Debug},
Expand Down

0 comments on commit e4ee629

Please sign in to comment.