From 993c4c055354ba6ddb0d81e8e5dbfa1f9bb631ac Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E5=BF=97=E5=AE=87?= Date: Thu, 26 Sep 2024 17:42:58 +0800 Subject: [PATCH] feat(chain,core)!: move `Merge` to `bdk_core` --- crates/chain/src/tx_data_traits.rs | 82 ------------------------------ crates/core/src/lib.rs | 3 ++ crates/core/src/merge.rs | 82 ++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 82 deletions(-) create mode 100644 crates/core/src/merge.rs diff --git a/crates/chain/src/tx_data_traits.rs b/crates/chain/src/tx_data_traits.rs index d3d562bf..2ca314d2 100644 --- a/crates/chain/src/tx_data_traits.rs +++ b/crates/chain/src/tx_data_traits.rs @@ -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. /// @@ -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 { - if self.is_empty() { - None - } else { - Some(core::mem::take(self)) - } - } -} - -impl Merge for BTreeMap { - 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 Merge for BTreeSet { - 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 Merge for Vec { - 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); diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index aeb34dca..95bebe90 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -68,4 +68,7 @@ pub use checkpoint::*; mod tx_update; pub use tx_update::*; +mod merge; +pub use merge::*; + pub mod spk_client; diff --git a/crates/core/src/merge.rs b/crates/core/src/merge.rs new file mode 100644 index 00000000..59c0d7f4 --- /dev/null +++ b/crates/core/src/merge.rs @@ -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 { + if self.is_empty() { + None + } else { + Some(core::mem::take(self)) + } + } +} + +impl Merge for BTreeMap { + 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 Merge for BTreeSet { + 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 Merge for Vec { + 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.49.0