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

Commit

Permalink
Rename KeyGenerator::Arg to KeyGenerator::KArg
Browse files Browse the repository at this point in the history
  • Loading branch information
KiChjang committed Apr 23, 2021
1 parent 6b17d09 commit 09b34ab
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 51 deletions.
30 changes: 15 additions & 15 deletions frame/support/src/storage/generator/nmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub trait StorageNMap<K: KeyGenerator, V: FullCodec> {
fn storage_n_map_final_key<KG, KArg>(key: KArg) -> Vec<u8>
where
KG: KeyGenerator,
KArg: EncodeLike<KG::Arg> + TupleToEncodedIter,
KArg: EncodeLike<KG::KArg> + TupleToEncodedIter,
{
let module_prefix_hashed = Twox128::hash(Self::module_prefix());
let storage_prefix_hashed = Twox128::hash(Self::storage_prefix());
Expand All @@ -123,23 +123,23 @@ where
{
type Query = G::Query;

fn hashed_key_for<KArg: EncodeLike<K::Arg> + TupleToEncodedIter>(key: KArg) -> Vec<u8> {
fn hashed_key_for<KArg: EncodeLike<K::KArg> + TupleToEncodedIter>(key: KArg) -> Vec<u8> {
Self::storage_n_map_final_key::<K, _>(key)
}

fn contains_key<KArg: EncodeLike<K::Arg> + TupleToEncodedIter>(key: KArg) -> bool {
fn contains_key<KArg: EncodeLike<K::KArg> + TupleToEncodedIter>(key: KArg) -> bool {
unhashed::exists(&Self::storage_n_map_final_key::<K, _>(key))
}

fn get<KArg: EncodeLike<K::Arg> + TupleToEncodedIter>(key: KArg) -> Self::Query {
fn get<KArg: EncodeLike<K::KArg> + TupleToEncodedIter>(key: KArg) -> Self::Query {
G::from_optional_value_to_query(unhashed::get(&Self::storage_n_map_final_key::<K, _>(key)))
}

fn try_get<KArg: EncodeLike<K::Arg> + TupleToEncodedIter>(key: KArg) -> Result<V, ()> {
fn try_get<KArg: EncodeLike<K::KArg> + TupleToEncodedIter>(key: KArg) -> Result<V, ()> {
unhashed::get(&Self::storage_n_map_final_key::<K, _>(key)).ok_or(())
}

fn take<KArg: EncodeLike<K::Arg> + TupleToEncodedIter>(key: KArg) -> Self::Query {
fn take<KArg: EncodeLike<K::KArg> + TupleToEncodedIter>(key: KArg) -> Self::Query {
let final_key = Self::storage_n_map_final_key::<K, _>(key);

let value = unhashed::take(&final_key);
Expand All @@ -149,8 +149,8 @@ where
fn swap<KOther, KArg1, KArg2>(key1: KArg1, key2: KArg2)
where
KOther: KeyGenerator,
KArg1: EncodeLike<K::Arg> + TupleToEncodedIter,
KArg2: EncodeLike<KOther::Arg> + TupleToEncodedIter,
KArg1: EncodeLike<K::KArg> + TupleToEncodedIter,
KArg2: EncodeLike<KOther::KArg> + TupleToEncodedIter,
{
let final_x_key = Self::storage_n_map_final_key::<K, _>(key1);
let final_y_key = Self::storage_n_map_final_key::<KOther, _>(key2);
Expand All @@ -168,11 +168,11 @@ where
}
}

fn insert<KArg: EncodeLike<K::Arg> + TupleToEncodedIter, VArg: EncodeLike<V>>(key: KArg, val: VArg) {
fn insert<KArg: EncodeLike<K::KArg> + TupleToEncodedIter, VArg: EncodeLike<V>>(key: KArg, val: VArg) {
unhashed::put(&Self::storage_n_map_final_key::<K, _>(key), &val.borrow());
}

fn remove<KArg: EncodeLike<K::Arg> + TupleToEncodedIter>(key: KArg) {
fn remove<KArg: EncodeLike<K::KArg> + TupleToEncodedIter>(key: KArg) {
unhashed::kill(&Self::storage_n_map_final_key::<K, _>(key));
}

Expand All @@ -193,14 +193,14 @@ where
}
}

fn mutate<KArg: EncodeLike<K::Arg> + TupleToEncodedIter, R, F: FnOnce(&mut Self::Query) -> R>(
fn mutate<KArg: EncodeLike<K::KArg> + TupleToEncodedIter, R, F: FnOnce(&mut Self::Query) -> R>(
key: KArg,
f: F,
) -> R {
Self::try_mutate(key, |v| Ok::<R, Never>(f(v))).expect("`Never` can not be constructed; qed")
}

fn try_mutate<KArg: EncodeLike<K::Arg> + TupleToEncodedIter, R, E, F: FnOnce(&mut Self::Query) -> Result<R, E>>(
fn try_mutate<KArg: EncodeLike<K::KArg> + TupleToEncodedIter, R, E, F: FnOnce(&mut Self::Query) -> Result<R, E>>(
key: KArg,
f: F,
) -> Result<R, E> {
Expand All @@ -217,14 +217,14 @@ where
ret
}

fn mutate_exists<KArg: EncodeLike<K::Arg> + TupleToEncodedIter, R, F: FnOnce(&mut Option<V>) -> R>(
fn mutate_exists<KArg: EncodeLike<K::KArg> + TupleToEncodedIter, R, F: FnOnce(&mut Option<V>) -> R>(
key: KArg,
f: F,
) -> R {
Self::try_mutate_exists(key, |v| Ok::<R, Never>(f(v))).expect("`Never` can not be constructed; qed")
}

fn try_mutate_exists<KArg: EncodeLike<K::Arg> + TupleToEncodedIter, R, E, F: FnOnce(&mut Option<V>) -> Result<R, E>>(
fn try_mutate_exists<KArg: EncodeLike<K::KArg> + TupleToEncodedIter, R, E, F: FnOnce(&mut Option<V>) -> Result<R, E>>(
key: KArg,
f: F,
) -> Result<R, E> {
Expand All @@ -243,7 +243,7 @@ where

fn append<Item, EncodeLikeItem, KArg>(key: KArg, item: EncodeLikeItem)
where
KArg: EncodeLike<K::Arg> + TupleToEncodedIter,
KArg: EncodeLike<K::KArg> + TupleToEncodedIter,
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
V: StorageAppend<Item>
Expand Down
30 changes: 15 additions & 15 deletions frame/support/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,31 +552,31 @@ pub trait StorageNMap<K: KeyGenerator, V: FullCodec> {
type Query;

/// Get the storage key used to fetch a value corresponding to a specific key.
fn hashed_key_for<KArg: EncodeLike<K::Arg> + TupleToEncodedIter>(key: KArg) -> Vec<u8>;
fn hashed_key_for<KArg: EncodeLike<K::KArg> + TupleToEncodedIter>(key: KArg) -> Vec<u8>;

/// Does the value (explicitly) exist in storage?
fn contains_key<KArg: EncodeLike<K::Arg> + TupleToEncodedIter>(key: KArg) -> bool;
fn contains_key<KArg: EncodeLike<K::KArg> + TupleToEncodedIter>(key: KArg) -> bool;

/// Load the value associated with the given key from the map.
fn get<KArg: EncodeLike<K::Arg> + TupleToEncodedIter>(key: KArg) -> Self::Query;
fn get<KArg: EncodeLike<K::KArg> + TupleToEncodedIter>(key: KArg) -> Self::Query;

/// Try to get the value for the given key from the map.
///
/// Returns `Ok` if it exists, `Err` if not.
fn try_get<KArg: EncodeLike<K::Arg> + TupleToEncodedIter>(key: KArg) -> Result<V, ()>;
fn try_get<KArg: EncodeLike<K::KArg> + TupleToEncodedIter>(key: KArg) -> Result<V, ()>;

/// Swap the values of two keys.
fn swap<KOther, KArg1, KArg2>(key1: KArg1, key2: KArg2)
where
KOther: KeyGenerator,
KArg1: EncodeLike<K::Arg> + TupleToEncodedIter,
KArg2: EncodeLike<KOther::Arg> + TupleToEncodedIter;
KArg1: EncodeLike<K::KArg> + TupleToEncodedIter,
KArg2: EncodeLike<KOther::KArg> + TupleToEncodedIter;

/// Store a value to be associated with the given key from the map.
fn insert<KArg: EncodeLike<K::Arg> + TupleToEncodedIter, VArg: EncodeLike<V>>(key: KArg, val: VArg);
fn insert<KArg: EncodeLike<K::KArg> + TupleToEncodedIter, VArg: EncodeLike<V>>(key: KArg, val: VArg);

/// Remove the value under a key.
fn remove<KArg: EncodeLike<K::Arg> + TupleToEncodedIter>(key: KArg);
fn remove<KArg: EncodeLike<K::KArg> + TupleToEncodedIter>(key: KArg);

/// Remove all values under the partial prefix key.
fn remove_prefix<KP>(partial_key: KP) where K: HasKeyPrefix<KP>;
Expand All @@ -585,27 +585,27 @@ pub trait StorageNMap<K: KeyGenerator, V: FullCodec> {
fn iter_prefix_values<KP>(partial_key: KP) -> PrefixIterator<V> where K: HasKeyPrefix<KP>;

/// Mutate the value under a key.
fn mutate<KArg: EncodeLike<K::Arg> + TupleToEncodedIter, R, F: FnOnce(&mut Self::Query) -> R>(key: KArg, f: F) -> R;
fn mutate<KArg: EncodeLike<K::KArg> + TupleToEncodedIter, R, F: FnOnce(&mut Self::Query) -> R>(key: KArg, f: F) -> R;

/// Mutate the item, only if an `Ok` value is returned.
fn try_mutate<KArg: EncodeLike<K::Arg> + TupleToEncodedIter, R, E, F: FnOnce(&mut Self::Query) -> Result<R, E>>(
fn try_mutate<KArg: EncodeLike<K::KArg> + TupleToEncodedIter, R, E, F: FnOnce(&mut Self::Query) -> Result<R, E>>(
key: KArg,
f: F,
) -> Result<R, E>;

/// Mutate the value under a key.
///
/// Deletes the item if mutated to a `None`.
fn mutate_exists<KArg: EncodeLike<K::Arg> + TupleToEncodedIter, R, F: FnOnce(&mut Option<V>) -> R>(key: KArg, f: F) -> R;
fn mutate_exists<KArg: EncodeLike<K::KArg> + TupleToEncodedIter, R, F: FnOnce(&mut Option<V>) -> R>(key: KArg, f: F) -> R;

/// Mutate the item, only if an `Ok` value is returned. Deletes the item if mutated to a `None`.
fn try_mutate_exists<KArg: EncodeLike<K::Arg> + TupleToEncodedIter, R, E, F: FnOnce(&mut Option<V>) -> Result<R, E>>(
fn try_mutate_exists<KArg: EncodeLike<K::KArg> + TupleToEncodedIter, R, E, F: FnOnce(&mut Option<V>) -> Result<R, E>>(
key: KArg,
f: F,
) -> Result<R, E>;

/// Take the value under a key.
fn take<KArg: EncodeLike<K::Arg> + TupleToEncodedIter>(key: KArg) -> Self::Query;
fn take<KArg: EncodeLike<K::KArg> + TupleToEncodedIter>(key: KArg) -> Self::Query;

/// Append the given items to the value in the storage.
///
Expand All @@ -618,7 +618,7 @@ pub trait StorageNMap<K: KeyGenerator, V: FullCodec> {
/// on overwrite.
fn append<Item, EncodeLikeItem, KArg>(key: KArg, item: EncodeLikeItem)
where
KArg: EncodeLike<K::Arg> + TupleToEncodedIter,
KArg: EncodeLike<K::KArg> + TupleToEncodedIter,
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
V: StorageAppend<Item>;
Expand All @@ -635,7 +635,7 @@ pub trait StorageNMap<K: KeyGenerator, V: FullCodec> {
///
/// `None` does not mean that `get()` does not return a value. The default value is completly
/// ignored by this function.
fn decode_len<KArg: EncodeLike<K::Arg> + TupleToEncodedIter>(key: KArg) -> Option<usize>
fn decode_len<KArg: EncodeLike<K::KArg> + TupleToEncodedIter>(key: KArg) -> Option<usize>
where
V: StorageDecodeLength,
{
Expand Down
12 changes: 6 additions & 6 deletions frame/support/src/storage/types/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ pub struct Key<Hasher, KeyType>(
/// A trait that contains the current key as an associated type.
pub trait KeyGenerator {
type Key: EncodeLike<Self::Key>;
type Arg: Encode;
type KArg: Encode;

fn final_key<KArg: EncodeLike<Self::Arg> + TupleToEncodedIter>(key: KArg) -> Vec<u8>;
fn final_key<KArg: EncodeLike<Self::KArg> + TupleToEncodedIter>(key: KArg) -> Vec<u8>;
fn final_hash(encoded: &[u8]) -> Vec<u8>;
}

impl<H: StorageHasher, K: FullCodec> KeyGenerator for Key<H, K> {
type Key = K;
type Arg = (K,);
type KArg = (K,);

fn final_key<KArg: EncodeLike<Self::Arg> + TupleToEncodedIter>(key: KArg) -> Vec<u8> {
fn final_key<KArg: EncodeLike<Self::KArg> + TupleToEncodedIter>(key: KArg) -> Vec<u8> {
H::hash(&key.to_encoded_iter().next().expect("should have at least one element!")).as_ref().to_vec()
}

Expand All @@ -60,9 +60,9 @@ impl<H: StorageHasher, K: FullCodec> KeyGenerator for Key<H, K> {
#[impl_trait_for_tuples::impl_for_tuples(2, 18)]
impl KeyGenerator for Tuple {
for_tuples!( type Key = ( #(Tuple::Key),* ); );
for_tuples!( type Arg = ( #(Tuple::Key),* ); );
for_tuples!( type KArg = ( #(Tuple::Key),* ); );

fn final_key<KArg: EncodeLike<Self::Arg> + TupleToEncodedIter>(key: KArg) -> Vec<u8> {
fn final_key<KArg: EncodeLike<Self::KArg> + TupleToEncodedIter>(key: KArg) -> Vec<u8> {
let mut final_key = Vec::new();
let mut iter = key.to_encoded_iter();
for_tuples!(
Expand Down
30 changes: 15 additions & 15 deletions frame/support/src/storage/types/nmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,49 +103,49 @@ where
OnEmpty: crate::traits::Get<QueryKind::Query> + 'static,
{
/// Get the storage key used to fetch a value corresponding to a specific key.
pub fn hashed_key_for<KArg: EncodeLike<Key::Arg> + TupleToEncodedIter>(key: KArg) -> Vec<u8> {
pub fn hashed_key_for<KArg: EncodeLike<Key::KArg> + TupleToEncodedIter>(key: KArg) -> Vec<u8> {
<Self as crate::storage::StorageNMap<Key, Value>>::hashed_key_for(key)
}

/// Does the value (explicitly) exist in storage?
pub fn contains_key<KArg: EncodeLike<Key::Arg> + TupleToEncodedIter>(key: KArg) -> bool {
pub fn contains_key<KArg: EncodeLike<Key::KArg> + TupleToEncodedIter>(key: KArg) -> bool {
<Self as crate::storage::StorageNMap<Key, Value>>::contains_key(key)
}

/// Load the value associated with the given key from the map.
pub fn get<KArg: EncodeLike<Key::Arg> + TupleToEncodedIter>(key: KArg) -> QueryKind::Query {
pub fn get<KArg: EncodeLike<Key::KArg> + TupleToEncodedIter>(key: KArg) -> QueryKind::Query {
<Self as crate::storage::StorageNMap<Key, Value>>::get(key)
}

/// Try to get the value for the given key from the map.
///
/// Returns `Ok` if it exists, `Err` if not.
pub fn try_get<KArg: EncodeLike<Key::Arg> + TupleToEncodedIter>(key: KArg) -> Result<Value, ()> {
pub fn try_get<KArg: EncodeLike<Key::KArg> + TupleToEncodedIter>(key: KArg) -> Result<Value, ()> {
<Self as crate::storage::StorageNMap<Key, Value>>::try_get(key)
}

/// Take a value from storage, removing it afterwards.
pub fn take<KArg: EncodeLike<Key::Arg> + TupleToEncodedIter>(key: KArg) -> QueryKind::Query {
pub fn take<KArg: EncodeLike<Key::KArg> + TupleToEncodedIter>(key: KArg) -> QueryKind::Query {
<Self as crate::storage::StorageNMap<Key, Value>>::take(key)
}

/// Swap the values of two key-pairs.
pub fn swap<KOther, KArg1, KArg2>(key1: KArg1, key2: KArg2)
where
KOther: KeyGenerator,
KArg1: EncodeLike<Key::Arg> + TupleToEncodedIter,
KArg2: EncodeLike<KOther::Arg> + TupleToEncodedIter,
KArg1: EncodeLike<Key::KArg> + TupleToEncodedIter,
KArg2: EncodeLike<KOther::KArg> + TupleToEncodedIter,
{
<Self as crate::storage::StorageNMap<Key, Value>>::swap::<KOther, _, _>(key1, key2)
}

/// Store a value to be associated with the given keys from the map.
pub fn insert<KArg: EncodeLike<Key::Arg> + TupleToEncodedIter, VArg: EncodeLike<Value>>(key: KArg, val: VArg) {
pub fn insert<KArg: EncodeLike<Key::KArg> + TupleToEncodedIter, VArg: EncodeLike<Value>>(key: KArg, val: VArg) {
<Self as crate::storage::StorageNMap<Key, Value>>::insert(key, val)
}

/// Remove the value under the given keys.
pub fn remove<KArg: EncodeLike<Key::Arg> + TupleToEncodedIter>(key: KArg) {
pub fn remove<KArg: EncodeLike<Key::KArg> + TupleToEncodedIter>(key: KArg) {
<Self as crate::storage::StorageNMap<Key, Value>>::remove(key)
}

Expand All @@ -165,7 +165,7 @@ where
/// Mutate the value under the given keys.
pub fn mutate<KArg, R, F>(key: KArg, f: F) -> R
where
KArg: EncodeLike<Key::Arg> + TupleToEncodedIter,
KArg: EncodeLike<Key::KArg> + TupleToEncodedIter,
F: FnOnce(&mut QueryKind::Query) -> R,
{
<Self as crate::storage::StorageNMap<Key, Value>>::mutate(key, f)
Expand All @@ -174,7 +174,7 @@ where
/// Mutate the value under the given keys when the closure returns `Ok`.
pub fn try_mutate<KArg, R, E, F>(key: KArg, f: F) -> Result<R, E>
where
KArg: EncodeLike<Key::Arg> + TupleToEncodedIter,
KArg: EncodeLike<Key::KArg> + TupleToEncodedIter,
F: FnOnce(&mut QueryKind::Query) -> Result<R, E>,
{
<Self as crate::storage::StorageNMap<Key, Value>>::try_mutate(key, f)
Expand All @@ -183,7 +183,7 @@ where
/// Mutate the value under the given keys. Deletes the item if mutated to a `None`.
pub fn mutate_exists<KArg, R, F>(key: KArg, f: F) -> R
where
KArg: EncodeLike<Key::Arg> + TupleToEncodedIter,
KArg: EncodeLike<Key::KArg> + TupleToEncodedIter,
F: FnOnce(&mut Option<Value>) -> R,
{
<Self as crate::storage::StorageNMap<Key, Value>>::mutate_exists(key, f)
Expand All @@ -192,7 +192,7 @@ where
/// Mutate the item, only if an `Ok` value is returned. Deletes the item if mutated to a `None`.
pub fn try_mutate_exists<KArg, R, E, F>(key: KArg, f: F) -> Result<R, E>
where
KArg: EncodeLike<Key::Arg> + TupleToEncodedIter,
KArg: EncodeLike<Key::KArg> + TupleToEncodedIter,
F: FnOnce(&mut Option<Value>) -> Result<R, E>,
{
<Self as crate::storage::StorageNMap<Key, Value>>::try_mutate_exists(key, f)
Expand All @@ -209,7 +209,7 @@ where
/// on overwrite.
pub fn append<Item, EncodeLikeItem, KArg>(key: KArg, item: EncodeLikeItem)
where
KArg: EncodeLike<Key::Arg> + TupleToEncodedIter,
KArg: EncodeLike<Key::KArg> + TupleToEncodedIter,
Item: Encode,
EncodeLikeItem: EncodeLike<Item>,
Value: StorageAppend<Item>
Expand All @@ -229,7 +229,7 @@ where
///
/// `None` does not mean that `get()` does not return a value. The default value is completly
/// ignored by this function.
pub fn decode_len<KArg: EncodeLike<Key::Arg> + TupleToEncodedIter>(key: KArg) -> Option<usize>
pub fn decode_len<KArg: EncodeLike<Key::KArg> + TupleToEncodedIter>(key: KArg) -> Option<usize>
where
Value: StorageDecodeLength,
{
Expand Down

0 comments on commit 09b34ab

Please sign in to comment.