From: Tobin Harding Date: Wed, 23 Dec 2020 05:19:37 +0000 (+1100) Subject: Refactor db/batch matching X-Git-Tag: v0.5.0~10^2~10 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/database/struct.EncoderStringWriter.html?a=commitdiff_plain;h=0e6add0cfbf0e25f06b354e82a3f4bf704fcbe69;p=bdk Refactor db/batch matching 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. --- diff --git a/src/database/any.rs b/src/database/any.rs index 021ab7d6..7436f0c9 100644 --- a/src/database/any.rs +++ b/src/database/any.rs @@ -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."), + }, } } }