pub trait TripleMapStorage {
type Key1;
type Key2;
type Key3;
type Value;
fn contains_keys(key1: &Self::Key1, key2: &Self::Key2, key3: &Self::Key3) -> bool;
fn get(key1: &Self::Key1, key2: &Self::Key2, key3: &Self::Key3) -> Option<Self::Value>;
fn insert(key1: Self::Key1, key2: Self::Key2, key3: Self::Key3, value: Self::Value);
fn mutate<R, F: FnOnce(&mut Option<Self::Value>) -> R>(
key1: Self::Key1,
key2: Self::Key2,
key3: Self::Key3,
f: F,
) -> R;
fn mutate_exists<R, F: FnOnce(&mut Self::Value) -> R>(
key1: Self::Key1,
key2: Self::Key2,
key3: Self::Key3,
f: F,
) -> Option<R> {
Self::mutate(key1, key2, key3, |opt_val| opt_val.as_mut().map(f))
}
fn mutate_values<F: FnMut(Self::Value) -> Self::Value>(f: F);
fn remove(key1: Self::Key1, key2: Self::Key2, key3: Self::Key3);
fn clear();
fn take(key1: Self::Key1, key2: Self::Key2, key3: Self::Key3) -> Option<Self::Value>;
fn clear_prefix(key1: Self::Key1, key2: Self::Key2);
fn iter_prefix(
key1: &Self::Key1,
key2: &Self::Key2,
) -> impl Iterator<Item = (Self::Key3, Self::Value)>;
}
#[allow(clippy::crate_in_macro_def)]
#[macro_export]
macro_rules! wrap_storage_triple_map {
(storage: $storage: ident, name: $name: ident,
key1: $key1: ty,
key2: $key2: ty,
key3: $key3: ty,
value: $val: ty) => {
pub struct $name<T>(PhantomData<T>);
impl<T: crate::Config> TripleMapStorage for $name<T> {
type Key1 = $key1;
type Key2 = $key2;
type Key3 = $key3;
type Value = $val;
fn contains_keys(key1: &Self::Key1, key2: &Self::Key2, key3: &Self::Key3) -> bool {
$storage::<T>::contains_key((key1, key2, key3))
}
fn get(key1: &Self::Key1, key2: &Self::Key2, key3: &Self::Key3) -> Option<Self::Value> {
$storage::<T>::get((key1, key2, key3))
}
fn insert(key1: Self::Key1, key2: Self::Key2, key3: Self::Key3, value: Self::Value) {
$storage::<T>::insert((key1, key2, key3), value)
}
fn mutate<R, F: FnOnce(&mut Option<Self::Value>) -> R>(
key1: Self::Key1,
key2: Self::Key2,
key3: Self::Key3,
f: F,
) -> R {
$storage::<T>::mutate((key1, key2, key3), f)
}
fn mutate_values<F: FnMut(Self::Value) -> Self::Value>(mut f: F) {
let f = |v| Some(f(v));
$storage::<T>::translate_values(f)
}
fn remove(key1: Self::Key1, key2: Self::Key2, key3: Self::Key3) {
$storage::<T>::remove((key1, key2, key3))
}
fn clear() {
let _ = $storage::<T>::clear(u32::MAX, None);
}
fn take(key1: Self::Key1, key2: Self::Key2, key3: Self::Key3) -> Option<Self::Value> {
$storage::<T>::take((key1, key2, key3))
}
fn clear_prefix(key1: Self::Key1, key2: Self::Key2) {
let _ = $storage::<T>::clear_prefix((key1, key2), u32::MAX, None);
}
fn iter_prefix(
key1: &Self::Key1,
key2: &Self::Key2,
) -> impl Iterator<Item = (Self::Key3, Self::Value)> {
$storage::<T>::iter_prefix((key1, key2))
}
}
};
}