diff --git a/substrate/frame/support/src/storage/generator/double_map.rs b/substrate/frame/support/src/storage/generator/double_map.rs index 90ec92c622..4ffe32651a 100644 --- a/substrate/frame/support/src/storage/generator/double_map.rs +++ b/substrate/frame/support/src/storage/generator/double_map.rs @@ -151,6 +151,13 @@ where unhashed::get(&Self::storage_double_map_final_key(k1, k2)).ok_or(()) } + fn set, KArg2: EncodeLike>(k1: KArg1, k2: KArg2, q: Self::Query) { + match G::from_query_to_optional_value(q) { + Some(v) => Self::insert(k1, k2, v), + None => Self::remove(k1, k2), + } + } + fn take(k1: KArg1, k2: KArg2) -> Self::Query where KArg1: EncodeLike, diff --git a/substrate/frame/support/src/storage/generator/map.rs b/substrate/frame/support/src/storage/generator/map.rs index bd24e14e9e..d190145ea4 100644 --- a/substrate/frame/support/src/storage/generator/map.rs +++ b/substrate/frame/support/src/storage/generator/map.rs @@ -245,6 +245,13 @@ impl> storage::StorageMap unhashed::get(Self::storage_map_final_key(key).as_ref()).ok_or(()) } + fn set>(key: KeyArg, q: Self::Query) { + match G::from_query_to_optional_value(q) { + Some(v) => Self::insert(key, v), + None => Self::remove(key), + } + } + fn insert, ValArg: EncodeLike>(key: KeyArg, val: ValArg) { unhashed::put(Self::storage_map_final_key(key).as_ref(), &val) } diff --git a/substrate/frame/support/src/storage/generator/nmap.rs b/substrate/frame/support/src/storage/generator/nmap.rs index 0175d681df..f1d0f9a5f0 100755 --- a/substrate/frame/support/src/storage/generator/nmap.rs +++ b/substrate/frame/support/src/storage/generator/nmap.rs @@ -138,6 +138,13 @@ where unhashed::get(&Self::storage_n_map_final_key::(key)).ok_or(()) } + fn set + TupleToEncodedIter>(key: KArg, q: Self::Query) { + match G::from_query_to_optional_value(q) { + Some(v) => Self::insert(key, v), + None => Self::remove(key), + } + } + fn take + TupleToEncodedIter>(key: KArg) -> Self::Query { let final_key = Self::storage_n_map_final_key::(key); diff --git a/substrate/frame/support/src/storage/mod.rs b/substrate/frame/support/src/storage/mod.rs index c878766643..6911da630c 100644 --- a/substrate/frame/support/src/storage/mod.rs +++ b/substrate/frame/support/src/storage/mod.rs @@ -167,6 +167,9 @@ pub trait StorageMap { /// Load the value associated with the given key from the map. fn get>(key: KeyArg) -> Self::Query; + /// Store or remove the value to be associated with `key` so that `get` returns the `query`. + fn set>(key: KeyArg, query: Self::Query); + /// Try to get the value for the given key from the map. /// /// Returns `Ok` if it exists, `Err` if not. @@ -481,6 +484,9 @@ pub trait StorageDoubleMap { KArg1: EncodeLike, KArg2: EncodeLike; + /// Store or remove the value to be associated with `key` so that `get` returns the `query`. + fn set, KArg2: EncodeLike>(k1: KArg1, k2: KArg2, query: Self::Query); + /// Take a value from storage, removing it afterwards. fn take(k1: KArg1, k2: KArg2) -> Self::Query where @@ -657,6 +663,9 @@ pub trait StorageNMap { /// Returns `Ok` if it exists, `Err` if not. fn try_get + TupleToEncodedIter>(key: KArg) -> Result; + /// Store or remove the value to be associated with `key` so that `get` returns the `query`. + fn set + TupleToEncodedIter>(key: KArg, query: Self::Query); + /// Swap the values of two keys. fn swap(key1: KArg1, key2: KArg2) where diff --git a/substrate/frame/support/src/storage/types/counted_map.rs b/substrate/frame/support/src/storage/types/counted_map.rs index 44fcbf6079..c4027acfe7 100644 --- a/substrate/frame/support/src/storage/types/counted_map.rs +++ b/substrate/frame/support/src/storage/types/counted_map.rs @@ -132,6 +132,11 @@ where ::Map::try_get(key) } + /// Store or remove the value to be associated with `key` so that `get` returns the `query`. + pub fn set>(key: KeyArg, q: QueryKind::Query) { + ::Map::set(key, q) + } + /// Swap the values of two keys. pub fn swap, KeyArg2: EncodeLike>(key1: KeyArg1, key2: KeyArg2) { ::Map::swap(key1, key2) diff --git a/substrate/frame/support/src/storage/types/double_map.rs b/substrate/frame/support/src/storage/types/double_map.rs index bf957dc0ff..2e090d3011 100644 --- a/substrate/frame/support/src/storage/types/double_map.rs +++ b/substrate/frame/support/src/storage/types/double_map.rs @@ -203,6 +203,15 @@ where >::try_get(k1, k2) } + /// Store or remove the value to be associated with `key` so that `get` returns the `query`. + pub fn set, KArg2: EncodeLike>( + k1: KArg1, + k2: KArg2, + q: QueryKind::Query, + ) { + >::set(k1, k2, q) + } + /// Take a value from storage, removing it afterwards. pub fn take(k1: KArg1, k2: KArg2) -> QueryKind::Query where diff --git a/substrate/frame/support/src/storage/types/map.rs b/substrate/frame/support/src/storage/types/map.rs index 1a5500e589..f4ac83c226 100644 --- a/substrate/frame/support/src/storage/types/map.rs +++ b/substrate/frame/support/src/storage/types/map.rs @@ -152,6 +152,11 @@ where >::swap(key1, key2) } + /// Store or remove the value to be associated with `key` so that `get` returns the `query`. + pub fn set>(key: KeyArg, q: QueryKind::Query) { + >::set(key, q) + } + /// Store a value to be associated with the given key from the map. pub fn insert, ValArg: EncodeLike>(key: KeyArg, val: ValArg) { >::insert(key, val) diff --git a/substrate/frame/support/src/storage/types/nmap.rs b/substrate/frame/support/src/storage/types/nmap.rs index 1f303525b7..dcbdac761f 100755 --- a/substrate/frame/support/src/storage/types/nmap.rs +++ b/substrate/frame/support/src/storage/types/nmap.rs @@ -142,6 +142,14 @@ where >::try_get(key) } + /// Store or remove the value to be associated with `key` so that `get` returns the `query`. + pub fn set + TupleToEncodedIter>( + key: KArg, + query: QueryKind::Query, + ) { + >::set(key, query) + } + /// Take a value from storage, removing it afterwards. pub fn take + TupleToEncodedIter>( key: KArg,