Struct trie_db::triedbmut::TrieDBMut

source ·
pub struct TrieDBMut<'a, L>
where L: TrieLayout,
{ /* private fields */ }
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

use hash_db::Hasher;
use reference_trie::{RefTrieDBMut, TrieMut};
use trie_db::DBValue;
use keccak_hasher::KeccakHasher;
use memory_db::*;

let mut memdb = MemoryDB::<KeccakHasher, HashKey<_>, DBValue>::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(), b"bar".to_vec());
t.remove(b"foo").unwrap();
assert!(!t.contains(b"foo").unwrap());

Implementations§

source§

impl<'a, L> TrieDBMut<'a, L>
where L: TrieLayout,

source

pub fn db(&self) -> &dyn HashDB<L::Hash, DBValue>

Get the backing database.

source

pub fn db_mut(&mut self) -> &mut dyn HashDB<L::Hash, DBValue>

Get the backing database mutably.

source

pub fn commit(&mut self)

Commit the in-memory changes to disk, freeing their storage and updating the state root.

Trait Implementations§

source§

impl<'a, L> Drop for TrieDBMut<'a, L>
where L: TrieLayout,

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'a, L> TrieMut<L> for TrieDBMut<'a, L>
where L: TrieLayout,

source§

fn root(&mut self) -> &TrieHash<L>

Return the root of the trie.
source§

fn is_empty(&self) -> bool

Is the trie empty?
source§

fn get<'x, 'key>( &'x self, key: &'key [u8], ) -> Result<Option<DBValue>, TrieHash<L>, CError<L>>
where 'x: 'key,

What is the value of the given key in this trie?
source§

fn insert( &mut self, key: &[u8], value: &[u8], ) -> Result<Option<Value<L>>, TrieHash<L>, CError<L>>

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.
source§

fn remove( &mut self, key: &[u8], ) -> Result<Option<Value<L>>, TrieHash<L>, CError<L>>

Remove a key from the trie. Equivalent to making it equal to the empty value. Returns the old value associated with this key, if it existed.
source§

fn contains(&self, key: &[u8]) -> Result<bool, TrieHash<L>, CError<L>>

Does the trie contain a given key?

Auto Trait Implementations§

§

impl<'a, L> !Freeze for TrieDBMut<'a, L>

§

impl<'a, L> !RefUnwindSafe for TrieDBMut<'a, L>

§

impl<'a, L> !Send for TrieDBMut<'a, L>

§

impl<'a, L> !Sync for TrieDBMut<'a, L>

§

impl<'a, L> Unpin for TrieDBMut<'a, L>
where <<L as TrieLayout>::Hash as Hasher>::Out: Unpin,

§

impl<'a, L> !UnwindSafe for TrieDBMut<'a, L>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.