Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 5614bb2

Browse files
committed
Merge remote-tracking branch 'origin/master' into gav-referenda-submit-origin
2 parents 2996e35 + a3aa0e8 commit 5614bb2

File tree

8 files changed

+57
-0
lines changed

8 files changed

+57
-0
lines changed

frame/support/src/storage/generator/double_map.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ where
151151
unhashed::get(&Self::storage_double_map_final_key(k1, k2)).ok_or(())
152152
}
153153

154+
fn set<KArg1: EncodeLike<K1>, KArg2: EncodeLike<K2>>(k1: KArg1, k2: KArg2, q: Self::Query) {
155+
match G::from_query_to_optional_value(q) {
156+
Some(v) => Self::insert(k1, k2, v),
157+
None => Self::remove(k1, k2),
158+
}
159+
}
160+
154161
fn take<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Self::Query
155162
where
156163
KArg1: EncodeLike<K1>,

frame/support/src/storage/generator/map.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ impl<K: FullEncode, V: FullCodec, G: StorageMap<K, V>> storage::StorageMap<K, V>
245245
unhashed::get(Self::storage_map_final_key(key).as_ref()).ok_or(())
246246
}
247247

248+
fn set<KeyArg: EncodeLike<K>>(key: KeyArg, q: Self::Query) {
249+
match G::from_query_to_optional_value(q) {
250+
Some(v) => Self::insert(key, v),
251+
None => Self::remove(key),
252+
}
253+
}
254+
248255
fn insert<KeyArg: EncodeLike<K>, ValArg: EncodeLike<V>>(key: KeyArg, val: ValArg) {
249256
unhashed::put(Self::storage_map_final_key(key).as_ref(), &val)
250257
}

frame/support/src/storage/generator/nmap.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ where
138138
unhashed::get(&Self::storage_n_map_final_key::<K, _>(key)).ok_or(())
139139
}
140140

141+
fn set<KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter>(key: KArg, q: Self::Query) {
142+
match G::from_query_to_optional_value(q) {
143+
Some(v) => Self::insert(key, v),
144+
None => Self::remove(key),
145+
}
146+
}
147+
141148
fn take<KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter>(key: KArg) -> Self::Query {
142149
let final_key = Self::storage_n_map_final_key::<K, _>(key);
143150

frame/support/src/storage/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ pub trait StorageMap<K: FullEncode, V: FullCodec> {
167167
/// Load the value associated with the given key from the map.
168168
fn get<KeyArg: EncodeLike<K>>(key: KeyArg) -> Self::Query;
169169

170+
/// Store or remove the value to be associated with `key` so that `get` returns the `query`.
171+
fn set<KeyArg: EncodeLike<K>>(key: KeyArg, query: Self::Query);
172+
170173
/// Try to get the value for the given key from the map.
171174
///
172175
/// Returns `Ok` if it exists, `Err` if not.
@@ -481,6 +484,9 @@ pub trait StorageDoubleMap<K1: FullEncode, K2: FullEncode, V: FullCodec> {
481484
KArg1: EncodeLike<K1>,
482485
KArg2: EncodeLike<K2>;
483486

487+
/// Store or remove the value to be associated with `key` so that `get` returns the `query`.
488+
fn set<KArg1: EncodeLike<K1>, KArg2: EncodeLike<K2>>(k1: KArg1, k2: KArg2, query: Self::Query);
489+
484490
/// Take a value from storage, removing it afterwards.
485491
fn take<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Self::Query
486492
where
@@ -657,6 +663,9 @@ pub trait StorageNMap<K: KeyGenerator, V: FullCodec> {
657663
/// Returns `Ok` if it exists, `Err` if not.
658664
fn try_get<KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter>(key: KArg) -> Result<V, ()>;
659665

666+
/// Store or remove the value to be associated with `key` so that `get` returns the `query`.
667+
fn set<KArg: EncodeLikeTuple<K::KArg> + TupleToEncodedIter>(key: KArg, query: Self::Query);
668+
660669
/// Swap the values of two keys.
661670
fn swap<KOther, KArg1, KArg2>(key1: KArg1, key2: KArg2)
662671
where

frame/support/src/storage/types/counted_map.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ where
132132
<Self as MapWrapper>::Map::try_get(key)
133133
}
134134

135+
/// Store or remove the value to be associated with `key` so that `get` returns the `query`.
136+
pub fn set<KeyArg: EncodeLike<Key>>(key: KeyArg, q: QueryKind::Query) {
137+
<Self as MapWrapper>::Map::set(key, q)
138+
}
139+
135140
/// Swap the values of two keys.
136141
pub fn swap<KeyArg1: EncodeLike<Key>, KeyArg2: EncodeLike<Key>>(key1: KeyArg1, key2: KeyArg2) {
137142
<Self as MapWrapper>::Map::swap(key1, key2)

frame/support/src/storage/types/double_map.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ where
203203
<Self as crate::storage::StorageDoubleMap<Key1, Key2, Value>>::try_get(k1, k2)
204204
}
205205

206+
/// Store or remove the value to be associated with `key` so that `get` returns the `query`.
207+
pub fn set<KArg1: EncodeLike<Key1>, KArg2: EncodeLike<Key2>>(
208+
k1: KArg1,
209+
k2: KArg2,
210+
q: QueryKind::Query,
211+
) {
212+
<Self as crate::storage::StorageDoubleMap<Key1, Key2, Value>>::set(k1, k2, q)
213+
}
214+
206215
/// Take a value from storage, removing it afterwards.
207216
pub fn take<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> QueryKind::Query
208217
where

frame/support/src/storage/types/map.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ where
152152
<Self as crate::storage::StorageMap<Key, Value>>::swap(key1, key2)
153153
}
154154

155+
/// Store or remove the value to be associated with `key` so that `get` returns the `query`.
156+
pub fn set<KeyArg: EncodeLike<Key>>(key: KeyArg, q: QueryKind::Query) {
157+
<Self as crate::storage::StorageMap<Key, Value>>::set(key, q)
158+
}
159+
155160
/// Store a value to be associated with the given key from the map.
156161
pub fn insert<KeyArg: EncodeLike<Key>, ValArg: EncodeLike<Value>>(key: KeyArg, val: ValArg) {
157162
<Self as crate::storage::StorageMap<Key, Value>>::insert(key, val)

frame/support/src/storage/types/nmap.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ where
142142
<Self as crate::storage::StorageNMap<Key, Value>>::try_get(key)
143143
}
144144

145+
/// Store or remove the value to be associated with `key` so that `get` returns the `query`.
146+
pub fn set<KArg: EncodeLikeTuple<Key::KArg> + TupleToEncodedIter>(
147+
key: KArg,
148+
query: QueryKind::Query,
149+
) {
150+
<Self as crate::storage::StorageNMap<Key, Value>>::set(key, query)
151+
}
152+
145153
/// Take a value from storage, removing it afterwards.
146154
pub fn take<KArg: EncodeLikeTuple<Key::KArg> + TupleToEncodedIter>(
147155
key: KArg,

0 commit comments

Comments
 (0)