Trait matterdb::access::FromAccess[][src]

pub trait FromAccess<T: Access>: Sized {
    fn from_access(access: T, addr: IndexAddress) -> Result<Self, AccessError>;

    fn from_root(access: T) -> Result<Self, AccessError> { ... }
}

Constructs an object atop the database. The constructed object provides access to data in the DB, akin to an object-relational mapping.

The access to DB can be readonly or read-write, depending on the T: Access type param. Most object should implement FromAccess<T> for all T: Access.

Simplest FromAccess implementors are indexes; it is also implemented for Lazy and Group. FromAccess can be implemented for more complex components. Thus, FromAccess can be used to compose storage objects from simpler ones.

Examples

Component with two inner indexes. FromAccess is automatically derived using the matterdb_derive crate.

use matterdb_derive::FromAccess;

#[derive(FromAccess)]
struct InsertOnlyMap<T: Access> {
    map: MapIndex<T::Base, str, String>,
    len: Entry<T::Base, u64>,
}

impl<T: Access> InsertOnlyMap<T>
where
    T::Base: RawAccessMut,
{
    fn insert(&mut self, key: &str, value: String) -> bool {
        if self.map.contains(key) { return false; }
        self.map.put(&key.to_owned(), value);
        self.len.set(self.len.get().unwrap_or_default() + 1);
        true
    }
}

let db = TemporaryDB::new();
let fork = db.fork();
let mut map = InsertOnlyMap::from_access(&fork, "test".into())?;
map.insert("foo", "FOO".to_owned());
map.insert("bar", "BAR".to_owned());
assert_eq!(map.len.get(), Some(2));

// Components could be used with `Group` / `Lazy` out of the box:
let lazy_map: Lazy<_, InsertOnlyMap<_>> =
    Lazy::from_access(&fork, "test".into())?;
assert_eq!(lazy_map.get().map.get("foo").unwrap(), "FOO");

let group_of_maps: Group<_, u16, InsertOnlyMap<_>> =
    fork.get_group("test_group");
group_of_maps.get(&1).insert("baz", "BAZ".to_owned());
group_of_maps.get(&2).insert("baz", "BUZZ".to_owned());

Required methods

fn from_access(access: T, addr: IndexAddress) -> Result<Self, AccessError>[src]

Constructs the object at the given address.

Return value

Returns the constructed object. An error should be returned if the object cannot be constructed.

Loading content...

Provided methods

fn from_root(access: T) -> Result<Self, AccessError>[src]

Constructs the object from the root of the access.

The default implementation uses Self::from_access() with an empty address.

Loading content...

Implementors

impl<T, I> FromAccess<T> for Lazy<T, I> where
    T: Access,
    I: FromAccess<T>, 
[src]

impl<T, K: ?Sized> FromAccess<T> for KeySetIndex<T::Base, K> where
    T: Access,
    K: BinaryKey
[src]

impl<T, K: ?Sized, I> FromAccess<T> for Group<T, K, I> where
    T: Access,
    K: BinaryKey,
    I: FromAccess<T>, 
[src]

impl<T, K: ?Sized, V> FromAccess<T> for MapIndex<T::Base, K, V> where
    T: Access,
    K: BinaryKey,
    V: BinaryValue
[src]

impl<T, V> FromAccess<T> for Entry<T::Base, V> where
    T: Access,
    V: BinaryValue
[src]

impl<T, V> FromAccess<T> for ListIndex<T::Base, V> where
    T: Access,
    V: BinaryValue
[src]

impl<T, V> FromAccess<T> for SparseListIndex<T::Base, V> where
    T: Access,
    V: BinaryValue
[src]

Loading content...