Skip to content

Commit 02c4ddb

Browse files
authored
Adds try_get for StorageMap and StorageDoubleMap (#7774)
* Adds `try_get` for `StorageMap` and `StorageDoubleMap` * Switch to Value as return type
1 parent 926555d commit 02c4ddb

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

src/storage/generator/double_map.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ impl<K1, K2, V, G> storage::StorageDoubleMap<K1, K2, V> for G where
153153
G::from_optional_value_to_query(unhashed::get(&Self::storage_double_map_final_key(k1, k2)))
154154
}
155155

156+
fn try_get<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Result<V, ()>
157+
where
158+
KArg1: EncodeLike<K1>,
159+
KArg2: EncodeLike<K2> {
160+
unhashed::get(&Self::storage_double_map_final_key(k1, k2)).ok_or(())
161+
}
162+
156163
fn take<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Self::Query where
157164
KArg1: EncodeLike<K1>,
158165
KArg2: EncodeLike<K2>,

src/storage/generator/map.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ impl<K: FullEncode, V: FullCodec, G: StorageMap<K, V>> storage::StorageMap<K, V>
226226
G::from_optional_value_to_query(unhashed::get(Self::storage_map_final_key(key).as_ref()))
227227
}
228228

229+
fn try_get<KeyArg: EncodeLike<K>>(key: KeyArg) -> Result<V, ()> {
230+
unhashed::get(Self::storage_map_final_key(key).as_ref()).ok_or(())
231+
}
232+
229233
fn insert<KeyArg: EncodeLike<K>, ValArg: EncodeLike<V>>(key: KeyArg, val: ValArg) {
230234
unhashed::put(Self::storage_map_final_key(key).as_ref(), &val)
231235
}

src/storage/mod.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ pub fn with_transaction<R>(f: impl FnOnce() -> TransactionOutcome<R>) -> R {
107107

108108
/// A trait for working with macro-generated storage values under the substrate storage API.
109109
///
110-
/// Details on implementation can be found at
111-
/// [`generator::StorageValue`]
110+
/// Details on implementation can be found at [`generator::StorageValue`].
112111
pub trait StorageValue<T: FullCodec> {
113112
/// The type that get/take return.
114113
type Query;
@@ -122,8 +121,9 @@ pub trait StorageValue<T: FullCodec> {
122121
/// Load the value from the provided storage instance.
123122
fn get() -> Self::Query;
124123

125-
/// Try to get the underlying value from the provided storage instance; `Ok` if it exists,
126-
/// `Err` if not.
124+
/// Try to get the underlying value from the provided storage instance.
125+
///
126+
/// Returns `Ok` if it exists, `Err` if not.
127127
fn try_get() -> Result<T, ()>;
128128

129129
/// Translate a value from some previous type (`O`) to the current type.
@@ -200,8 +200,7 @@ pub trait StorageValue<T: FullCodec> {
200200

201201
/// A strongly-typed map in storage.
202202
///
203-
/// Details on implementation can be found at
204-
/// [`generator::StorageMap`]
203+
/// Details on implementation can be found at [`generator::StorageMap`].
205204
pub trait StorageMap<K: FullEncode, V: FullCodec> {
206205
/// The type that get/take return.
207206
type Query;
@@ -215,6 +214,11 @@ pub trait StorageMap<K: FullEncode, V: FullCodec> {
215214
/// Load the value associated with the given key from the map.
216215
fn get<KeyArg: EncodeLike<K>>(key: KeyArg) -> Self::Query;
217216

217+
/// Try to get the value for the given key from the map.
218+
///
219+
/// Returns `Ok` if it exists, `Err` if not.
220+
fn try_get<KeyArg: EncodeLike<K>>(key: KeyArg) -> Result<V, ()>;
221+
218222
/// Swap the values of two keys.
219223
fn swap<KeyArg1: EncodeLike<K>, KeyArg2: EncodeLike<K>>(key1: KeyArg1, key2: KeyArg2);
220224

@@ -233,7 +237,9 @@ pub trait StorageMap<K: FullEncode, V: FullCodec> {
233237
f: F,
234238
) -> Result<R, E>;
235239

236-
/// Mutate the value under a key. Deletes the item if mutated to a `None`.
240+
/// Mutate the value under a key.
241+
///
242+
/// Deletes the item if mutated to a `None`.
237243
fn mutate_exists<KeyArg: EncodeLike<K>, R, F: FnOnce(&mut Option<V>) -> R>(key: KeyArg, f: F) -> R;
238244

239245
/// Mutate the item, only if an `Ok` value is returned. Deletes the item if mutated to a `None`.
@@ -354,8 +360,7 @@ pub trait IterableStorageDoubleMap<
354360
/// It provides an important ability to efficiently remove all entries
355361
/// that have a common first key.
356362
///
357-
/// Details on implementation can be found at
358-
/// [`generator::StorageDoubleMap`]
363+
/// Details on implementation can be found at [`generator::StorageDoubleMap`].
359364
pub trait StorageDoubleMap<K1: FullEncode, K2: FullEncode, V: FullCodec> {
360365
/// The type that get/take returns.
361366
type Query;
@@ -378,6 +383,14 @@ pub trait StorageDoubleMap<K1: FullEncode, K2: FullEncode, V: FullCodec> {
378383
KArg1: EncodeLike<K1>,
379384
KArg2: EncodeLike<K2>;
380385

386+
/// Try to get the value for the given key from the double map.
387+
///
388+
/// Returns `Ok` if it exists, `Err` if not.
389+
fn try_get<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Result<V, ()>
390+
where
391+
KArg1: EncodeLike<K1>,
392+
KArg2: EncodeLike<K2>;
393+
381394
/// Take a value from storage, removing it afterwards.
382395
fn take<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Self::Query
383396
where

src/storage/types/double_map.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ where
141141
<Self as crate::storage::StorageDoubleMap<Key1, Key2, Value>>::get(k1, k2)
142142
}
143143

144+
/// Try to get the value for the given key from the double map.
145+
///
146+
/// Returns `Ok` if it exists, `Err` if not.
147+
pub fn try_get<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> Result<Value, ()>
148+
where
149+
KArg1: EncodeLike<Key1>,
150+
KArg2: EncodeLike<Key2> {
151+
<Self as crate::storage::StorageDoubleMap<Key1, Key2, Value>>::try_get(k1, k2)
152+
}
153+
144154
/// Take a value from storage, removing it afterwards.
145155
pub fn take<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> QueryKind::Query
146156
where
@@ -514,6 +524,7 @@ mod test {
514524
});
515525
assert_eq!(A::contains_key(2, 20), true);
516526
assert_eq!(A::get(2, 20), Some(100));
527+
assert_eq!(A::try_get(2, 20), Ok(100));
517528
let _: Result<(), ()> = AValueQueryWithAnOnEmpty::try_mutate_exists(2, 20, |v| {
518529
*v = Some(v.unwrap() * 10);
519530
Err(())
@@ -527,6 +538,7 @@ mod test {
527538
assert_eq!(A::contains_key(2, 20), false);
528539
assert_eq!(AValueQueryWithAnOnEmpty::take(2, 20), 97);
529540
assert_eq!(A::contains_key(2, 20), false);
541+
assert_eq!(A::try_get(2, 20), Err(()));
530542

531543
B::insert(2, 20, 10);
532544
assert_eq!(A::migrate_keys::<Blake2_256, Twox128, _, _>(2, 20), Some(10));

src/storage/types/map.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ where
116116
<Self as crate::storage::StorageMap<Key, Value>>::get(key)
117117
}
118118

119+
/// Try to get the value for the given key from the map.
120+
///
121+
/// Returns `Ok` if it exists, `Err` if not.
122+
pub fn try_get<KeyArg: EncodeLike<Key>>(key: KeyArg) -> Result<Value, ()> {
123+
<Self as crate::storage::StorageMap<Key, Value>>::try_get(key)
124+
}
125+
119126
/// Swap the values of two keys.
120127
pub fn swap<KeyArg1: EncodeLike<Key>, KeyArg2: EncodeLike<Key>>(key1: KeyArg1, key2: KeyArg2) {
121128
<Self as crate::storage::StorageMap<Key, Value>>::swap(key1, key2)
@@ -352,12 +359,14 @@ mod test {
352359
A::insert(3, 10);
353360
assert_eq!(A::contains_key(3), true);
354361
assert_eq!(A::get(3), Some(10));
362+
assert_eq!(A::try_get(3), Ok(10));
355363
assert_eq!(AValueQueryWithAnOnEmpty::get(3), 10);
356364

357365
A::swap(3, 2);
358366
assert_eq!(A::contains_key(3), false);
359367
assert_eq!(A::contains_key(2), true);
360368
assert_eq!(A::get(3), None);
369+
assert_eq!(A::try_get(3), Err(()));
361370
assert_eq!(AValueQueryWithAnOnEmpty::get(3), 97);
362371
assert_eq!(A::get(2), Some(10));
363372
assert_eq!(AValueQueryWithAnOnEmpty::get(2), 10);

0 commit comments

Comments
 (0)