mutate returns a value (#945)

* mutate returns a value

* code comment fixes

* fix the compile
This commit is contained in:
guanqun
2018-10-26 17:13:16 +08:00
committed by Gav Wood
parent 10210b9520
commit b1976c9014
3 changed files with 32 additions and 19 deletions
+22 -14
View File
@@ -119,7 +119,7 @@ pub trait StorageValue<T: codec::Codec> {
}
/// Mutate this value
fn mutate<F: FnOnce(&mut Self::Query), S: Storage>(f: F, storage: &S);
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: Storage>(f: F, storage: &S) -> R;
/// Clear the storage value.
fn kill<S: Storage>(storage: &S) {
@@ -190,7 +190,7 @@ pub trait StorageMap<K: codec::Codec, V: codec::Codec> {
}
/// Mutate the value under a key.
fn mutate<F: FnOnce(&mut Self::Query), S: Storage>(key: &K, f: F, storage: &S);
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: Storage>(key: &K, f: F, storage: &S) -> R;
}
// TODO: Remove this in favour of `decl_storage` macro.
@@ -342,10 +342,10 @@ macro_rules! __storage_items_internal {
}
/// Mutate this value.
fn mutate<F: FnOnce(&mut Self::Query), S: $crate::GenericStorage>(f: F, storage: &S) {
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: $crate::GenericStorage>(f: F, storage: &S) -> R {
let mut val = <Self as $crate::storage::generator::StorageValue<$ty>>::get(storage);
f(&mut val);
let ret = f(&mut val);
__handle_wrap_internal!($wraptype {
// raw type case
@@ -353,10 +353,12 @@ macro_rules! __storage_items_internal {
} {
// Option<> type case
match val {
Some(val) => <Self as $crate::storage::generator::StorageValue<$ty>>::put(&val, storage),
Some(ref val) => <Self as $crate::storage::generator::StorageValue<$ty>>::put(&val, storage),
None => <Self as $crate::storage::generator::StorageValue<$ty>>::kill(storage),
}
});
ret
}
}
};
@@ -398,10 +400,10 @@ macro_rules! __storage_items_internal {
}
/// Mutate the value under a key.
fn mutate<F: FnOnce(&mut Self::Query), S: $crate::GenericStorage>(key: &$kty, f: F, storage: &S) {
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: $crate::GenericStorage>(key: &$kty, f: F, storage: &S) -> R {
let mut val = <Self as $crate::storage::generator::StorageMap<$kty, $ty>>::take(key, storage);
f(&mut val);
let ret = f(&mut val);
__handle_wrap_internal!($wraptype {
// raw type case
@@ -409,10 +411,12 @@ macro_rules! __storage_items_internal {
} {
// Option<> type case
match val {
Some(val) => <Self as $crate::storage::generator::StorageMap<$kty, $ty>>::insert(key, &val, storage),
Some(ref val) => <Self as $crate::storage::generator::StorageMap<$kty, $ty>>::insert(key, &val, storage),
None => <Self as $crate::storage::generator::StorageMap<$kty, $ty>>::remove(key, storage),
}
});
ret
}
}
};
@@ -1900,10 +1904,10 @@ macro_rules! __decl_storage_item {
}
/// Mutate the value under a key
fn mutate<F: FnOnce(&mut Self::Query), S: $crate::GenericStorage>(key: &$kty, f: F, storage: &S) {
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: $crate::GenericStorage>(key: &$kty, f: F, storage: &S) -> R {
let mut val = <Self as $crate::storage::generator::StorageMap<$kty, $ty>>::take(key, storage);
f(&mut val);
let ret = f(&mut val);
__handle_wrap_internal!($wraptype {
// raw type case
@@ -1911,10 +1915,12 @@ macro_rules! __decl_storage_item {
} {
// Option<> type case
match val {
Some(val) => <Self as $crate::storage::generator::StorageMap<$kty, $ty>>::insert(key, &val, storage),
Some(ref val) => <Self as $crate::storage::generator::StorageMap<$kty, $ty>>::insert(key, &val, storage),
None => <Self as $crate::storage::generator::StorageMap<$kty, $ty>>::remove(key, storage),
}
});
ret
}
}
};
@@ -1959,10 +1965,10 @@ macro_rules! __decl_storage_item {
}
/// Mutate the value under a key.
fn mutate<F: FnOnce(&mut Self::Query), S: $crate::GenericStorage>(f: F, storage: &S) {
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: $crate::GenericStorage>(f: F, storage: &S) -> R {
let mut val = <Self as $crate::storage::generator::StorageValue<$ty>>::get(storage);
f(&mut val);
let ret = f(&mut val);
__handle_wrap_internal!($wraptype {
// raw type case
@@ -1970,10 +1976,12 @@ macro_rules! __decl_storage_item {
} {
// Option<> type case
match val {
Some(val) => <Self as $crate::storage::generator::StorageValue<$ty>>::put(&val, storage),
Some(ref val) => <Self as $crate::storage::generator::StorageValue<$ty>>::put(&val, storage),
None => <Self as $crate::storage::generator::StorageValue<$ty>>::kill(storage),
}
});
ret
}
}
};
+4 -4
View File
@@ -169,7 +169,7 @@ pub trait StorageValue<T: Codec> {
fn put<Arg: Borrow<T>>(val: Arg);
/// Mutate the value
fn mutate<F: FnOnce(&mut Self::Query)>(f: F);
fn mutate<R, F: FnOnce(&mut Self::Query) -> R>(f: F) -> R;
/// Clear the storage value.
fn kill();
@@ -193,7 +193,7 @@ impl<T: Codec, U> StorageValue<T> for U where U: generator::StorageValue<T> {
fn put<Arg: Borrow<T>>(val: Arg) {
U::put(val.borrow(), &RuntimeStorage)
}
fn mutate<F: FnOnce(&mut Self::Query)>(f: F) {
fn mutate<R, F: FnOnce(&mut Self::Query) -> R>(f: F) -> R {
U::mutate(f, &RuntimeStorage)
}
fn kill() {
@@ -296,7 +296,7 @@ pub trait StorageMap<K: Codec, V: Codec> {
fn remove<KeyArg: Borrow<K>>(key: KeyArg);
/// Mutate the value under a key.
fn mutate<KeyArg: Borrow<K>, F: FnOnce(&mut Self::Query)>(key: KeyArg, f: F);
fn mutate<KeyArg: Borrow<K>, R, F: FnOnce(&mut Self::Query) -> R>(key: KeyArg, f: F) -> R;
/// Take the value under a key.
fn take<KeyArg: Borrow<K>>(key: KeyArg) -> Self::Query;
@@ -329,7 +329,7 @@ impl<K: Codec, V: Codec, U> StorageMap<K, V> for U where U: generator::StorageMa
U::remove(key.borrow(), &RuntimeStorage)
}
fn mutate<KeyArg: Borrow<K>, F: FnOnce(&mut Self::Query)>(key: KeyArg, f: F) {
fn mutate<KeyArg: Borrow<K>, R, F: FnOnce(&mut Self::Query) -> R>(key: KeyArg, f: F) -> R {
U::mutate(key.borrow(), f, &RuntimeStorage)
}