fedimint_core

Macro impl_db_record

Source
macro_rules! impl_db_record {
    (key = $key:ty, value = $val:ty, db_prefix = $db_prefix:expr $(, notify_on_modify = $notify:tt)? $(,)?) => { ... };
    (@impl_notify_marker key = $key:ty, notify_on_modify = true) => { ... };
    (@impl_notify_marker key = $key:ty, notify_on_modify = false) => { ... };
}
Expand description

This is a helper macro that generates the implementations of DatabaseRecord necessary for reading/writing to the database and fetching by prefix.

  • key: This is the type of struct that will be used as the key into the database
  • value: This is the type of struct that will be used as the value into the database
  • db_prefix: Required enum expression that is represented as a u8 and is prepended to this key
  • query_prefix: Optional type of struct that can be passed zero or more times. Every query prefix can be used to query the database via find_by_prefix

ยงExamples

use fedimint_core::encoding::{Decodable, Encodable};
use fedimint_core::impl_db_record;

#[derive(Debug, Encodable, Decodable)]
struct MyKey;

#[derive(Debug, Encodable, Decodable)]
struct MyValue;

#[repr(u8)]
#[derive(Clone, Debug)]
pub enum DbKeyPrefix {
    MyKey = 0x50,
}

impl_db_record!(key = MyKey, value = MyValue, db_prefix = DbKeyPrefix::MyKey);

Use the required parameters and specify one query_prefix

use fedimint_core::encoding::{Decodable, Encodable};
use fedimint_core::{impl_db_lookup, impl_db_record};

#[derive(Debug, Encodable, Decodable)]
struct MyKey;

#[derive(Debug, Encodable, Decodable)]
struct MyValue;

#[repr(u8)]
#[derive(Clone, Debug)]
pub enum DbKeyPrefix {
    MyKey = 0x50,
}

#[derive(Debug, Encodable, Decodable)]
struct MyKeyPrefix;

impl_db_record!(key = MyKey, value = MyValue, db_prefix = DbKeyPrefix::MyKey,);

impl_db_lookup!(key = MyKey, query_prefix = MyKeyPrefix);