nimiq_database/traits/
table.rs1pub use nimiq_database_value::{AsDatabaseBytes, FromDatabaseBytes};
2
3pub trait Key: AsDatabaseBytes + FromDatabaseBytes + Ord + 'static {}
5impl<T: AsDatabaseBytes + FromDatabaseBytes + Ord + 'static> Key for T {}
6pub trait Value: AsDatabaseBytes + FromDatabaseBytes + 'static {}
8impl<T: AsDatabaseBytes + FromDatabaseBytes + 'static> Value for T {}
9
10pub trait DupTableValue: Value {
12 type SubKey: Key;
14
15 type Value: Value;
17
18 fn subkey(&self) -> &Self::SubKey;
20
21 fn value(&self) -> &Self::Value;
23}
24
25pub trait Table {
27 const NAME: &'static str;
29
30 type Key: Key;
32 type Value: Value;
34}
35
36pub trait RegularTable: Table {}
38
39pub trait DupTable: Table {}
41
42#[macro_export]
47macro_rules! declare_table {
48 ($typ:ident, $name:expr, $key:ty, $value:ty) => {
50 #[derive(Clone, Debug, Default)]
51 pub struct $typ;
52 impl $crate::traits::Table for $typ {
53 const NAME: &'static str = $name;
54 type Key = $key;
55 type Value = $value;
56 }
57 };
58 ($typ:ident, $name:expr, $key:ty => dup($value:ty)) => {
60 $crate::declare_table!($typ, $name, $key, $value);
61 impl $crate::traits::DupTable for $typ {}
62 };
63 ($typ:ident, $name:expr, $key:ty => $value:ty) => {
65 $crate::declare_table!($typ, $name, $key, $value);
66 impl $crate::traits::RegularTable for $typ {}
67 };
68 ($typ:ident, $name:expr, $key:ty => $subkey:ty => $value:ty) => {
70 $crate::declare_table!($typ, $name, $key, $crate::utils::IndexedValue<$subkey, $value>);
71 impl $crate::traits::DupTable for $typ {}
72 };
73}