]> Untitled Git - bdk/commitdiff
Refactor db/batch matching
authorTobin Harding <me@tobin.cc>
Wed, 23 Dec 2020 05:19:37 +0000 (16:19 +1100)
committerTobin Harding <me@tobin.cc>
Wed, 24 Feb 2021 02:30:47 +0000 (13:30 +1100)
Remove the TODO; refactor matching to correctly handle conditionally
built `Sled` variants. Use `unreachable` instead of `unimplemented` with
a comment hinting that this is a bug, this makes it explicit, both at
runtime and when reading the code, that this match arm should not be hit.

src/database/any.rs

index 021ab7d679effbb938baf14144210c5d6b47bff4..7436f0c9a7432aa17c453f33e6b37cc46e480feb 100644 (file)
@@ -312,24 +312,17 @@ impl BatchDatabase for AnyDatabase {
         }
     }
     fn commit_batch(&mut self, batch: Self::Batch) -> Result<(), Error> {
-        // TODO: refactor once `move_ref_pattern` is stable
-        #[allow(irrefutable_let_patterns)]
         match self {
-            AnyDatabase::Memory(db) => {
-                if let AnyBatch::Memory(batch) = batch {
-                    db.commit_batch(batch)
-                } else {
-                    unimplemented!()
-                }
-            }
+            AnyDatabase::Memory(db) => match batch {
+                AnyBatch::Memory(batch) => db.commit_batch(batch),
+                #[cfg(feature = "key-value-db")]
+                _ => unimplemented!("Sled batch shouldn't be used with Memory db."),
+            },
             #[cfg(feature = "key-value-db")]
-            AnyDatabase::Sled(db) => {
-                if let AnyBatch::Sled(batch) = batch {
-                    db.commit_batch(batch)
-                } else {
-                    unimplemented!()
-                }
-            }
+            AnyDatabase::Sled(db) => match batch {
+                AnyBatch::Sled(batch) => db.commit_batch(batch),
+                _ => unimplemented!("Memory batch shouldn't be used with Sled db."),
+            },
         }
     }
 }