gear_common/storage/primitives/
map.rs1use sp_runtime::codec::{Encode, EncodeAppend, EncodeLike};
10pub trait MapStorage {
16 type Key;
18 type Value;
20
21 fn contains_key(key: &Self::Key) -> bool;
23
24 fn get(key: &Self::Key) -> Option<Self::Value>;
26
27 fn insert(key: Self::Key, value: Self::Value);
29
30 fn mutate<R, F: FnOnce(&mut Option<Self::Value>) -> R>(key: Self::Key, f: F) -> R;
35
36 fn mutate_exists<R, F: FnOnce(&mut Self::Value) -> R>(key: Self::Key, f: F) -> Option<R> {
38 Self::mutate(key, |opt_val| opt_val.as_mut().map(f))
39 }
40
41 fn mutate_values<F: FnMut(Self::Value) -> Self::Value>(f: F);
43
44 fn remove(key: Self::Key);
46
47 fn clear();
49
50 fn take(key: Self::Key) -> Option<Self::Value>;
53}
54
55pub trait AppendMapStorage<Item, Key, Value>: MapStorage<Key = Key, Value = Value>
56where
57 Item: Encode,
58 Key: Encode,
59 Value: EncodeAppend<Item = Item>,
60{
61 fn append<EncodeLikeKey, EncodeLikeItem>(key: EncodeLikeKey, item: EncodeLikeItem)
62 where
63 EncodeLikeKey: EncodeLike<Key>,
64 EncodeLikeItem: EncodeLike<Item>;
65}
66
67#[allow(clippy::crate_in_macro_def)]
82#[macro_export]
83macro_rules! wrap_storage_map {
84 (storage: $storage: ident, name: $name: ident, key: $key: ty, value: $val: ty) => {
85 pub struct $name<T>(PhantomData<T>);
86
87 impl<T: crate::Config> MapStorage for $name<T> {
88 type Key = $key;
89 type Value = $val;
90
91 fn contains_key(key: &Self::Key) -> bool {
92 $storage::<T>::contains_key(key)
93 }
94
95 fn get(key: &Self::Key) -> Option<Self::Value> {
96 $storage::<T>::get(key)
97 }
98
99 fn insert(key: Self::Key, value: Self::Value) {
100 $storage::<T>::insert(key, value)
101 }
102
103 fn mutate<R, F: FnOnce(&mut Option<Self::Value>) -> R>(key: Self::Key, f: F) -> R {
104 $storage::<T>::mutate(key, f)
105 }
106
107 fn mutate_values<F: FnMut(Self::Value) -> Self::Value>(mut f: F) {
108 let f = |v| Some(f(v));
109 $storage::<T>::translate_values(f)
110 }
111
112 fn remove(key: Self::Key) {
113 $storage::<T>::remove(key)
114 }
115
116 fn clear() {
117 let _ = $storage::<T>::clear(u32::MAX, None);
118 }
119
120 fn take(key: Self::Key) -> Option<Self::Value> {
121 $storage::<T>::take(key)
122 }
123 }
124 };
125}
126
127#[allow(clippy::crate_in_macro_def)]
132#[macro_export]
133macro_rules! wrap_counted_storage_map {
134 (storage: $storage: ident, name: $name: ident, key: $key: ty, value: $val: ty, length: $len: ty) => {
135 $crate::wrap_storage_map!(storage: $storage, name: $name, key: $key, value: $val);
136
137 impl<T: crate::Config> Counted for $name<T> {
138 type Length = $len;
139
140 fn len() -> Self::Length {
141 $storage::<T>::count() as Self::Length
142 }
143 }
144 };
145}