redb_model_trait/lib.rs
1//! Traits for the `redb_model` crate.
2
3/// Trait for table definition.
4pub trait Model<'a> {
5 /// The table type.
6 type TableType;
7 /// The table definition.
8 const DEFINITION: Self::TableType;
9}
10
11/// Conversion methods for a `Model` and the associated keys and values.
12pub trait ModelExt<'a>: Model<'a> + Sized + 'a {
13 /// The `redb` definition key type(s).
14 type RedbKey: redb::Key;
15 /// The `redb` definition value type(s).
16 type RedbValue: redb::Value;
17
18 /// The model key type(s).
19 type ModelKey;
20 /// The model key type(s).
21 type ModelValue;
22
23 /// Instantiate from a `redb` (`K`, `V`) pair.
24 fn from_values(
25 values: (
26 <Self::RedbKey as redb::Value>::SelfType<'a>,
27 <Self::RedbValue as redb::Value>::SelfType<'a>,
28 ),
29 ) -> Self;
30
31 /// Instantiate from a `redb` (`AccessGuard<K>`, `AccessGuard<V>`) pair.
32 fn from_guards(
33 values: (
34 &redb::AccessGuard<'a, Self::RedbKey>,
35 &redb::AccessGuard<'a, Self::RedbValue>,
36 ),
37 ) -> Self;
38
39 /// Instantiate from a `redb` (`K`, `AccessGuard<V>`) pair.
40 fn from_key_and_guard(
41 values: (
42 <Self::RedbKey as redb::Value>::SelfType<'a>,
43 &redb::AccessGuard<'a, Self::RedbValue>,
44 ),
45 ) -> Self;
46
47 /// Get the `redb` `K` value. May copy, clone or borrow
48 /// depending on the definition.
49 fn as_key(&'a self) -> <Self::RedbKey as redb::Value>::SelfType<'a>;
50
51 /// Get the `redb` `V` value. May copy, clone or borrow
52 /// depending on the definition.
53 fn as_value(&'a self) -> <Self::RedbValue as redb::Value>::SelfType<'a>;
54
55 /// Get all variables as a `redb` `(K, V)` pair. May copy, clone or borrow
56 /// depending on the definition.
57 fn as_key_and_value(
58 &'a self,
59 ) -> (
60 <Self::RedbKey as redb::Value>::SelfType<'a>,
61 <Self::RedbValue as redb::Value>::SelfType<'a>,
62 );
63}