have hashdb be generic over DBValue

This commit is contained in:
Johann Tuffe 2018-08-22 18:05:48 +08:00
parent 49b896e99c
commit 09da6da010
2 changed files with 10 additions and 18 deletions

View File

@ -6,8 +6,7 @@ description = "trait for hash-keyed databases."
license = "GPL-3.0"
[dependencies]
elastic-array = { version = "0.10", optional = true }
[features]
default = ["std"]
std = ["elastic-array"]
std = []

View File

@ -18,13 +18,10 @@
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
extern crate elastic_array;
#[cfg(feature = "std")]
extern crate core;
#[cfg(feature = "std")]
use elastic_array::ElasticArray128;
#[cfg(feature = "std")]
use std::collections::HashMap;
use core::{fmt::Debug, hash::Hash};
@ -44,19 +41,15 @@ pub trait Hasher: Sync + Send {
fn hash(x: &[u8]) -> Self::Out;
}
/// `HashDB` value type.
#[cfg(feature = "std")]
pub type DBValue = ElasticArray128<u8>;
/// Trait modelling datastore keyed by a hash defined by the `Hasher`.
#[cfg(feature = "std")]
pub trait HashDB<H: Hasher>: Send + Sync + AsHashDB<H> {
pub trait HashDB<H: Hasher, T>: Send + Sync + AsHashDB<H, T> {
/// Get the keys in the database together with number of underlying references.
fn keys(&self) -> HashMap<H::Out, i32>;
/// Look up a given hash into the bytes that hash to it, returning None if the
/// hash is not known.
fn get(&self, key: &H::Out) -> Option<DBValue>;
fn get(&self, key: &H::Out) -> Option<&T>;
/// Check for the existance of a hash-key.
fn contains(&self, key: &H::Out) -> bool;
@ -67,7 +60,7 @@ pub trait HashDB<H: Hasher>: Send + Sync + AsHashDB<H> {
fn insert(&mut self, value: &[u8]) -> H::Out;
/// Like `insert()`, except you provide the key and the data is all moved.
fn emplace(&mut self, key: H::Out, value: DBValue);
fn emplace(&mut self, key: H::Out, value: T);
/// Remove a datum previously inserted. Insertions can be "owed" such that the same number of `insert()`s may
/// happen without the data being eventually being inserted into the DB. It can be "owed" more than once.
@ -76,18 +69,18 @@ pub trait HashDB<H: Hasher>: Send + Sync + AsHashDB<H> {
/// Upcast trait.
#[cfg(feature = "std")]
pub trait AsHashDB<H: Hasher> {
pub trait AsHashDB<H: Hasher, T> {
/// Perform upcast to HashDB for anything that derives from HashDB.
fn as_hashdb(&self) -> &HashDB<H>;
fn as_hashdb(&self) -> &HashDB<H, T>;
/// Perform mutable upcast to HashDB for anything that derives from HashDB.
fn as_hashdb_mut(&mut self) -> &mut HashDB<H>;
fn as_hashdb_mut(&mut self) -> &mut HashDB<H, T>;
}
// NOTE: There used to be a `impl<T> AsHashDB for T` but that does not work with generics. See https://stackoverflow.com/questions/48432842/implementing-a-trait-for-reference-and-non-reference-types-causes-conflicting-im
// This means we need concrete impls of AsHashDB in several places, which somewhat defeats the point of the trait.
#[cfg(feature = "std")]
impl<'a, H: Hasher> AsHashDB<H> for &'a mut HashDB<H> {
fn as_hashdb(&self) -> &HashDB<H> { &**self }
fn as_hashdb_mut(&mut self) -> &mut HashDB<H> { &mut **self }
impl<'a, H: Hasher, T> AsHashDB<H, T> for &'a mut HashDB<H, T> {
fn as_hashdb(&self) -> &HashDB<H, T> { &**self }
fn as_hashdb_mut(&mut self) -> &mut HashDB<H, T> { &mut **self }
}