Skip to content

Commit ff7d703

Browse files
committed
Merge #1961: Add is_empty methods to TxUpdate and {}_Response types
e9263f9 feat(core): Add `is_empty` methods to `TxUpdate` and `{}_Response` types (志宇) Pull request description: ### Description `is_empty` methods are helpful in various contexts so I added them. ### Changelog notice ```md Added - `is_empty` methods to `TxUpdate`, `SyncResponse` and `FullScanResponse` ``` ### 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 #### New Features: * [x] I've added tests for the new feature * [x] I've added docs for the new feature ACKs for top commit: LagginTimes: ACK e9263f9 Tree-SHA512: 767a581b5d66da31f609db3ffba2457cb0f579c386e8e960bd89d6650852ba9d3c00e94432aaeba586d19b01fa8982ffdee2615c867b98ad8efa8e16505e3180
2 parents 4a7c0ed + e9263f9 commit ff7d703

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

crates/core/src/spk_client.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,13 @@ impl<A> Default for SyncResponse<A> {
404404
}
405405
}
406406

407+
impl<A> SyncResponse<A> {
408+
/// Returns true if the `SyncResponse` is empty.
409+
pub fn is_empty(&self) -> bool {
410+
self.tx_update.is_empty() && self.chain_update.is_none()
411+
}
412+
}
413+
407414
/// Builds a [`FullScanRequest`].
408415
///
409416
/// Construct with [`FullScanRequest::builder`].
@@ -561,6 +568,15 @@ impl<K, A> Default for FullScanResponse<K, A> {
561568
}
562569
}
563570

571+
impl<K, A> FullScanResponse<K, A> {
572+
/// Returns true if the `FullScanResponse` is empty.
573+
pub fn is_empty(&self) -> bool {
574+
self.tx_update.is_empty()
575+
&& self.last_active_indices.is_empty()
576+
&& self.chain_update.is_none()
577+
}
578+
}
579+
564580
struct KeychainSpkIter<'r, K> {
565581
keychain: K,
566582
spks: Option<&'r mut Box<dyn Iterator<Item = Indexed<ScriptBuf>> + Send>>,

crates/core/src/tx_update.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ impl<A> Default for TxUpdate<A> {
6464
}
6565
}
6666

67+
impl<A> TxUpdate<A> {
68+
/// Returns true if the `TxUpdate` contains no elements in any of its fields.
69+
pub fn is_empty(&self) -> bool {
70+
self.txs.is_empty()
71+
&& self.txouts.is_empty()
72+
&& self.anchors.is_empty()
73+
&& self.seen_ats.is_empty()
74+
&& self.evicted_ats.is_empty()
75+
}
76+
}
77+
6778
impl<A: Ord> TxUpdate<A> {
6879
/// Transforms the [`TxUpdate`] to have `anchors` (`A`) of another type (`A2`).
6980
///

crates/core/tests/test_spk_client.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use bdk_core::spk_client::{FullScanResponse, SyncResponse};
2+
3+
#[test]
4+
fn test_empty() {
5+
assert!(
6+
FullScanResponse::<(), ()>::default().is_empty(),
7+
"Default `FullScanResponse` must be empty"
8+
);
9+
assert!(
10+
SyncResponse::<()>::default().is_empty(),
11+
"Default `SyncResponse` must be empty"
12+
);
13+
}

crates/core/tests/test_tx_update.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use bdk_core::TxUpdate;
2+
3+
#[test]
4+
fn test_empty() {
5+
assert!(
6+
TxUpdate::<()>::default().is_empty(),
7+
"Default `TxUpdate` must be empty"
8+
);
9+
}

0 commit comments

Comments
 (0)