Skip to content

Commit

Permalink
Merge pull request #9 from paritytech/fix/implement-default-for-memorydb
Browse files Browse the repository at this point in the history
Fix bug where deriven Default impl causes panics
  • Loading branch information
andresilva authored Jul 12, 2018
2 parents 5f05acd + f1ab623 commit 4481687
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion memorydb/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "memorydb"
version = "0.2.0"
version = "0.2.1"
authors = ["Parity Technologies <admin@parity.io>"]
description = "in-memory implementation of hashdb"
license = "GPL-3.0"
Expand Down
19 changes: 15 additions & 4 deletions memorydb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type FastMap<H, T> = HashMap<<H as KeyHasher>::Out, T, hash::BuildHasherDefault<
/// extern crate hashdb;
/// extern crate keccak_hasher;
/// extern crate memorydb;
///
///
/// use hashdb::*;
/// use keccak_hasher::KeccakHasher;
/// use memorydb::*;
Expand Down Expand Up @@ -81,12 +81,16 @@ type FastMap<H, T> = HashMap<<H as KeyHasher>::Out, T, hash::BuildHasherDefault<
/// assert!(!m.contains(&k));
/// }
/// ```
#[derive(Default, Clone, PartialEq)]
#[derive(Clone, PartialEq)]
pub struct MemoryDB<H: KeyHasher> {
data: FastMap<H, (DBValue, i32)>,
hashed_null_node: H::Out,
}

impl<H: KeyHasher> Default for MemoryDB<H> {
fn default() -> Self { Self::new() }
}

impl<H: KeyHasher> MemoryDB<H> {
/// Create a new instance of the memory DB.
pub fn new() -> MemoryDB<H> {
Expand All @@ -103,11 +107,11 @@ impl<H: KeyHasher> MemoryDB<H> {
/// extern crate hashdb;
/// extern crate keccak_hasher;
/// extern crate memorydb;
///
///
/// use hashdb::*;
/// use keccak_hasher::KeccakHasher;
/// use memorydb::*;
///
///
/// fn main() {
/// let mut m = MemoryDB::<KeccakHasher>::new();
/// let hello_bytes = "Hello world!".as_bytes();
Expand Down Expand Up @@ -341,4 +345,11 @@ mod tests {
assert_eq!(overlay.get(&insert_key).unwrap(), &(DBValue::from_slice(b"arf"), 2));
assert_eq!(overlay.get(&negative_remove_key).unwrap(), &(DBValue::from_slice(b"negative"), -2));
}

#[test]
fn default_works() {
let mut db = MemoryDB::<KeccakHasher>::default();
let hashed_null_node = KeccakHasher::hash(&NULL_RLP);
assert_eq!(db.insert(&NULL_RLP), hashed_null_node);
}
}

0 comments on commit 4481687

Please sign in to comment.