#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
use std::fmt::Debug;
#[cfg(feature = "std")]
use std::hash;
#[cfg(not(feature = "std"))]
use core::hash;
#[cfg(feature = "std")]
pub trait MaybeDebug: Debug {}
#[cfg(feature = "std")]
impl<T: Debug> MaybeDebug for T {}
#[cfg(not(feature = "std"))]
pub trait MaybeDebug {}
#[cfg(not(feature = "std"))]
impl<T> MaybeDebug for T {}
pub type Prefix<'a> = (&'a[u8], Option<u8>);
pub static EMPTY_PREFIX: Prefix<'static> = (&[], None);
pub trait Hasher: Sync + Send {
type Out: AsRef<[u8]> + AsMut<[u8]> + Default + MaybeDebug + PartialEq + Eq
+ hash::Hash + Send + Sync + Clone + Copy;
type StdHasher: Sync + Send + Default + hash::Hasher;
const LENGTH: usize;
fn hash(x: &[u8]) -> Self::Out;
}
pub trait PlainDB<K, V>: Send + Sync + AsPlainDB<K, V> {
fn get(&self, key: &K) -> Option<V>;
fn contains(&self, key: &K) -> bool;
fn emplace(&mut self, key: K, value: V);
fn remove(&mut self, key: &K);
}
pub trait PlainDBRef<K, V> {
fn get(&self, key: &K) -> Option<V>;
fn contains(&self, key: &K) -> bool;
}
impl<'a, K, V> PlainDBRef<K, V> for &'a dyn PlainDB<K, V> {
fn get(&self, key: &K) -> Option<V> { PlainDB::get(*self, key) }
fn contains(&self, key: &K) -> bool { PlainDB::contains(*self, key) }
}
impl<'a, K, V> PlainDBRef<K, V> for &'a mut dyn PlainDB<K, V> {
fn get(&self, key: &K) -> Option<V> { PlainDB::get(*self, key) }
fn contains(&self, key: &K) -> bool { PlainDB::contains(*self, key) }
}
pub trait HashDB<H: Hasher, T>: Send + Sync + AsHashDB<H, T> {
fn get(&self, key: &H::Out, prefix: Prefix) -> Option<T>;
fn contains(&self, key: &H::Out, prefix: Prefix) -> bool;
fn insert(&mut self, prefix: Prefix, value: &[u8]) -> H::Out;
fn emplace(&mut self, key: H::Out, prefix: Prefix, value: T);
fn remove(&mut self, key: &H::Out, prefix: Prefix);
}
pub trait HashDBRef<H: Hasher, T> {
fn get(&self, key: &H::Out, prefix: Prefix) -> Option<T>;
fn contains(&self, key: &H::Out, prefix: Prefix) -> bool;
}
impl<'a, H: Hasher, T> HashDBRef<H, T> for &'a dyn HashDB<H, T> {
fn get(&self, key: &H::Out, prefix: Prefix) -> Option<T> { HashDB::get(*self, key, prefix) }
fn contains(&self, key: &H::Out, prefix: Prefix) -> bool {
HashDB::contains(*self, key, prefix)
}
}
impl<'a, H: Hasher, T> HashDBRef<H, T> for &'a mut dyn HashDB<H, T> {
fn get(&self, key: &H::Out, prefix: Prefix) -> Option<T> { HashDB::get(*self, key, prefix) }
fn contains(&self, key: &H::Out, prefix: Prefix) -> bool {
HashDB::contains(*self, key, prefix)
}
}
pub trait AsHashDB<H: Hasher, T> {
fn as_hash_db(&self) -> &dyn HashDB<H, T>;
fn as_hash_db_mut<'a>(&'a mut self) -> &'a mut (dyn HashDB<H, T> + 'a);
}
pub trait AsPlainDB<K, V> {
fn as_plain_db(&self) -> &dyn PlainDB<K, V>;
fn as_plain_db_mut<'a>(&'a mut self) -> &'a mut (dyn PlainDB<K, V> + 'a);
}
impl<'a, H: Hasher, T> AsHashDB<H, T> for &'a mut dyn HashDB<H, T> {
fn as_hash_db(&self) -> &dyn HashDB<H, T> { &**self }
fn as_hash_db_mut<'b>(&'b mut self) -> &'b mut (dyn HashDB<H, T> + 'b) { &mut **self }
}
#[cfg(feature = "std")]
impl<'a, K, V> AsPlainDB<K, V> for &'a mut dyn PlainDB<K, V> {
fn as_plain_db(&self) -> &dyn PlainDB<K, V> { &**self }
fn as_plain_db_mut<'b>(&'b mut self) -> &'b mut (dyn PlainDB<K, V> + 'b) { &mut **self }
}