]> Untitled Git - bdk/commitdiff
feat(chain)!: add `take` convenience method to `Append` trait
author志宇 <hello@evanlinjin.me>
Fri, 14 Jun 2024 15:21:19 +0000 (23:21 +0800)
committer志宇 <hello@evanlinjin.me>
Fri, 14 Jun 2024 16:52:23 +0000 (00:52 +0800)
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
crates/wallet/src/wallet/mod.rs
example-crates/example_bitcoind_rpc_polling/src/main.rs

index b3ab3515ef96c8e2692f0b01e00b4fcc4eee43a0..7944f95c06ca7c288f6b2c881f67dc2e5d7eeaaf 100644 (file)
@@ -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<Self> {
+        if self.is_empty() {
+            None
+        } else {
+            Some(core::mem::take(self))
+        }
+    }
 }
 
 impl<K: Ord, V> Append for BTreeMap<K, V> {
index c3e8fd7c3d438cf9d2ad207f4596646d968919f7..883e954a7925daf7639d3f5b9adc0d36e6c5d133 100644 (file)
@@ -2291,11 +2291,7 @@ impl Wallet {
 
     /// Take the staged [`ChangeSet`] to be persisted now (if any).
     pub fn take_staged(&mut self) -> Option<ChangeSet> {
-        if self.stage.is_empty() {
-            None
-        } else {
-            Some(core::mem::take(&mut self.stage))
-        }
+        self.stage.take()
     }
 
     /// Get a reference to the inner [`TxGraph`].
index 53969764de95e50892a0a3ad1b3a435abd1c8922..38e796474c8a17103a35cff1f3acd3decc89f577 100644 (file)
@@ -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)",