From feb27df180938759670afd2c9cd15529a22c4772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Fri, 14 Jun 2024 23:21:19 +0800 Subject: [PATCH] feat(chain)!: add `take` convenience method to `Append` trait This is useful if the caller wishes to use the type as a staging area. This is breaking as `Append` has a `Default` bound now. --- crates/chain/src/tx_data_traits.rs | 11 ++++++++++- crates/wallet/src/wallet/mod.rs | 6 +----- .../example_bitcoind_rpc_polling/src/main.rs | 12 ++++++------ 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/crates/chain/src/tx_data_traits.rs b/crates/chain/src/tx_data_traits.rs index b3ab3515e..7944f95c0 100644 --- a/crates/chain/src/tx_data_traits.rs +++ b/crates/chain/src/tx_data_traits.rs @@ -114,12 +114,21 @@ pub trait AnchorFromBlockPosition: Anchor { } /// Trait that makes an object appendable. -pub trait Append { +pub trait Append: Default { /// Append another object of the same type onto `self`. fn append(&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 Append for BTreeMap { diff --git a/crates/wallet/src/wallet/mod.rs b/crates/wallet/src/wallet/mod.rs index c3e8fd7c3..883e954a7 100644 --- a/crates/wallet/src/wallet/mod.rs +++ b/crates/wallet/src/wallet/mod.rs @@ -2291,11 +2291,7 @@ impl Wallet { /// Take the staged [`ChangeSet`] to be persisted now (if any). pub fn take_staged(&mut self) -> Option { - if self.stage.is_empty() { - None - } else { - Some(core::mem::take(&mut self.stage)) - } + self.stage.take() } /// Get a reference to the inner [`TxGraph`]. diff --git a/example-crates/example_bitcoind_rpc_polling/src/main.rs b/example-crates/example_bitcoind_rpc_polling/src/main.rs index 53969764d..38e796474 100644 --- a/example-crates/example_bitcoind_rpc_polling/src/main.rs +++ b/example-crates/example_bitcoind_rpc_polling/src/main.rs @@ -196,8 +196,8 @@ fn main() -> anyhow::Result<()> { if last_db_commit.elapsed() >= DB_COMMIT_DELAY { let db = &mut *db.lock().unwrap(); last_db_commit = Instant::now(); - if !db_stage.is_empty() { - db.append_changeset(&core::mem::take(&mut db_stage))?; + if let Some(changeset) = db_stage.take() { + db.append_changeset(&changeset)?; } println!( "[{:>10}s] committed to db (took {}s)", @@ -235,8 +235,8 @@ fn main() -> anyhow::Result<()> { { let db = &mut *db.lock().unwrap(); db_stage.append((local_chain::ChangeSet::default(), graph_changeset)); - if !db_stage.is_empty() { - db.append_changeset(&core::mem::take(&mut db_stage))?; + if let Some(changeset) = db_stage.take() { + db.append_changeset(&changeset)?; } } } @@ -325,8 +325,8 @@ fn main() -> anyhow::Result<()> { if last_db_commit.elapsed() >= DB_COMMIT_DELAY { let db = &mut *db.lock().unwrap(); last_db_commit = Instant::now(); - if !db_stage.is_empty() { - db.append_changeset(&core::mem::take(&mut db_stage))?; + if let Some(changeset) = db_stage.take() { + db.append_changeset(&changeset)?; } println!( "[{:>10}s] committed to db (took {}s)",