Expand description
A Trie
implementation using a generic HashDB
backing database.
Use it as a TrieMut
trait object. You can use db()
to get the backing database object.
Note that changes are not committed to the database until commit
is called.
Querying the root or dropping the trie will commit automatically.
Example
extern crate trie_db;
extern crate reference_trie;
extern crate hash_db;
extern crate keccak_hasher;
extern crate memory_db;
use hash_db::Hasher;
use reference_trie::{RefTrieDBMut, TrieMut};
use trie_db::DBValue;
use keccak_hasher::KeccakHasher;
use memory_db::*;
fn main() {
let mut memdb = MemoryDB::default();
let mut root = Default::default();
let mut t = RefTrieDBMut::new(&mut memdb, &mut root);
assert!(t.is_empty());
assert_eq!(*t.root(), KeccakHasher::hash(&[0u8][..]));
t.insert(b"foo", b"bar").unwrap();
assert!(t.contains(b"foo").unwrap());
assert_eq!(t.get(b"foo").unwrap().unwrap(), DBValue::from_slice(b"bar"));
t.remove(b"foo").unwrap();
assert!(!t.contains(b"foo").unwrap());
}
Implementations
sourceimpl<'a, H, C> TrieDBMut<'a, H, C> where
H: Hasher,
C: NodeCodec<H>,
impl<'a, H, C> TrieDBMut<'a, H, C> where
H: Hasher,
C: NodeCodec<H>,
sourcepub fn new(db: &'a mut dyn HashDB<H, DBValue>, root: &'a mut H::Out) -> Self
pub fn new(db: &'a mut dyn HashDB<H, DBValue>, root: &'a mut H::Out) -> Self
Create a new trie with backing database db
and empty root
.
Trait Implementations
sourceimpl<'a, H, C> TrieMut<H, C> for TrieDBMut<'a, H, C> where
H: Hasher,
C: NodeCodec<H>,
impl<'a, H, C> TrieMut<H, C> for TrieDBMut<'a, H, C> where
H: Hasher,
C: NodeCodec<H>,
sourcefn get<'x, 'key>(
&'x self,
key: &'key [u8]
) -> Result<Option<DBValue>, H::Out, C::Error> where
'x: 'key,
fn get<'x, 'key>(
&'x self,
key: &'key [u8]
) -> Result<Option<DBValue>, H::Out, C::Error> where
'x: 'key,
What is the value of the given key in this trie?
sourcefn insert(
&mut self,
key: &[u8],
value: &[u8]
) -> Result<Option<DBValue>, H::Out, C::Error>
fn insert(
&mut self,
key: &[u8],
value: &[u8]
) -> Result<Option<DBValue>, H::Out, C::Error>
Insert a key
/value
pair into the trie. An empty value is equivalent to removing
key
from the trie. Returns the old value associated with this key, if it existed. Read more
Auto Trait Implementations
impl<'a, H, C> !RefUnwindSafe for TrieDBMut<'a, H, C>
impl<'a, H, C> Send for TrieDBMut<'a, H, C> where
C: Send,
impl<'a, H, C> Sync for TrieDBMut<'a, H, C> where
C: Sync,
impl<'a, H, C> Unpin for TrieDBMut<'a, H, C> where
C: Unpin,
<H as Hasher>::Out: Unpin,
impl<'a, H, C> !UnwindSafe for TrieDBMut<'a, H, C>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more