[][src]Module exonum_merkledb::generic

Access generalizations, mainly useful for bindings.

This module provides:

  • GenericRawAccess, an enumeration of all available types of raw accesses (e.g., Snapshot or Fork)
  • GenericAccess, an enumeration of all high-level access types (e.g., Prefixed or Migration)
  • ErasedAccess, which combines the previous two types and thus is the most abstract kind of access to the database.

Examples

Basic usage of ErasedAccess:

use exonum_merkledb::{
    access::{AccessExt, Prefixed}, migration::Migration, Database, TemporaryDB,
};
use exonum_merkledb::generic::{ErasedAccess, IntoErased};

fn manipulate_db(access: &ErasedAccess<'_>) {
    assert!(access.is_mutable());
    let mut list = access.get_list::<_, u32>("list");
    list.extend(vec![1, 2, 3]);
    access.get_proof_entry("entry").set("!".to_owned());
}

fn check_db(access: &ErasedAccess<'_>) {
    assert!(!access.is_mutable());
    let list = access.get_list::<_, u32>("list");
    assert_eq!(list.len(), 3);
    assert_eq!(list.iter().collect::<Vec<_>>(), vec![1, 2, 3]);
    let entry = access.get_proof_entry::<_, String>("entry");
    assert_eq!(entry.get().unwrap(), "!");
}

let db = TemporaryDB::new();
let fork = db.fork();
// Create a `Prefixed` access and use `IntoErased` trait to convert it
// to the most generic access.
{
    let erased = Prefixed::new("ns", &fork).into_erased();
    manipulate_db(&erased);
}
// The same method may be applied to other access kinds, e.g., `Migration`s.
{
    let erased = Migration::new("other-ns", &fork).into_erased();
    manipulate_db(&erased);
}
db.merge(fork.into_patch()).unwrap();

let snapshot = db.snapshot();
let erased = Prefixed::new("ns", snapshot.as_ref()).into_erased();
check_db(&erased);
let erased = Migration::new("other-ns", snapshot.as_ref()).into_erased();
check_db(&erased);

Use of GenericRawAccess with owned accesses:

use exonum_merkledb::{access::AccessExt, Database, TemporaryDB};
use exonum_merkledb::generic::GenericRawAccess;
use std::rc::Rc;

let db = TemporaryDB::new();
let fork = db.fork();
let access = GenericRawAccess::from(fork); // Consumes `fork`!
access.get_proof_map("list").put("foo", "bar".to_owned());
// Get `Fork` back from the access. The caller should ensure
// that `access` is not used elsewhere at this point, e.g.,
// by instantiated indexes.
let fork = match access {
    GenericRawAccess::OwnedFork(fork) => Rc::try_unwrap(fork).unwrap(),
    _ => unreachable!(),
};
db.merge(fork.into_patch()).unwrap();

Enums

GenericAccess

Generic access containing any kind of accesses supported by the database.

GenericRawAccess

Container for an arbitrary raw access. For Forks and Snapshots, this type provides both owned and borrowed variants.

Traits

IntoErased

Conversion to a most generic access to the database.

Type Definitions

ErasedAccess

Most generic access to the database, encapsulating any of base accesses and any of possible access restrictions.