get rid of refcell, use &mut storage (#2659)

This commit is contained in:
thiolliere
2019-05-23 11:08:20 +02:00
committed by Gavin Wood
parent d05f35f764
commit 1e35552c14
6 changed files with 114 additions and 119 deletions
@@ -116,13 +116,13 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
}
/// Take a value from storage, removing it afterwards.
fn take<S: #scrate::HashedStorage<#scrate::Twox128>>(storage: &S) -> Self::Query {
fn take<S: #scrate::HashedStorage<#scrate::Twox128>>(storage: &mut S) -> Self::Query {
storage.take(<Self as #scrate::storage::hashed::generator::StorageValue<#typ>>::key())
.#option_simple_1(|| #fielddefault)
}
/// Mutate the value under a key.
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: #scrate::HashedStorage<#scrate::Twox128>>(f: F, storage: &S) -> R {
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: #scrate::HashedStorage<#scrate::Twox128>>(f: F, storage: &mut S) -> R {
let mut val = <Self as #scrate::storage::hashed::generator::StorageValue<#typ>>::get(storage);
let ret = f(&mut val);
@@ -212,13 +212,13 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
}
/// Take the value, reading and removing it.
fn take<S: #scrate::HashedStorage<#scrate::#hasher>>(key: &#kty, storage: &S) -> Self::Query {
fn take<S: #scrate::HashedStorage<#scrate::#hasher>>(key: &#kty, storage: &mut S) -> Self::Query {
let key = #as_map::key_for(key);
storage.take(&key[..]).#option_simple_1(|| #fielddefault)
}
/// Mutate the value under a key
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: #scrate::HashedStorage<#scrate::#hasher>>(key: &#kty, f: F, storage: &S) -> R {
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: #scrate::HashedStorage<#scrate::#hasher>>(key: &#kty, f: F, storage: &mut S) -> R {
let mut val = #as_map::get(key, storage);
let ret = f(&mut val);
@@ -349,7 +349,7 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
///
/// Takes care of updating previous and next elements points
/// as well as updates head if the element is first or last.
fn remove_linkage<S: #scrate::HashedStorage<#scrate::#hasher>>(linkage: Linkage<#kty>, storage: &S);
fn remove_linkage<S: #scrate::HashedStorage<#scrate::#hasher>>(linkage: Linkage<#kty>, storage: &mut S);
/// Read the contained data and it's linkage.
fn read_with_linkage<S: #scrate::HashedStorage<#scrate::#hasher>>(storage: &S, key: &[u8]) -> Option<(#value_type, Linkage<#kty>)>;
@@ -358,7 +358,7 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
///
/// Takes care of updating head and previous head's pointer.
fn new_head_linkage<S: #scrate::HashedStorage<#scrate::#hasher>>(
storage: &S,
storage: &mut S,
key: &#kty,
) -> Linkage<#kty>;
@@ -368,7 +368,7 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
/// Overwrite current head pointer.
///
/// If `None` is given head is removed from storage.
fn write_head<S: #scrate::HashedStorage<#scrate::#hasher>>(storage: &S, head: Option<&#kty>);
fn write_head<S: #scrate::HashedStorage<#scrate::#hasher>>(storage: &mut S, head: Option<&#kty>);
}
}
};
@@ -380,7 +380,7 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
impl<#traitinstance: #traittype, #instance #bound_instantiable> self::#inner_module::Utils<#traitinstance, #instance> for #name<#traitinstance, #instance> {
fn remove_linkage<S: #scrate::HashedStorage<#scrate::#hasher>>(
linkage: self::#inner_module::Linkage<#kty>,
storage: &S,
storage: &mut S,
) {
use self::#inner_module::Utils;
@@ -415,7 +415,7 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
}
fn new_head_linkage<S: #scrate::HashedStorage<#scrate::#hasher>>(
storage: &S,
storage: &mut S,
key: &#kty,
) -> self::#inner_module::Linkage<#kty> {
use self::#inner_module::Utils;
@@ -450,7 +450,7 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
storage.get(#final_head_key)
}
fn write_head<S: #scrate::HashedStorage<#scrate::#hasher>>(storage: &S, head: Option<&#kty>) {
fn write_head<S: #scrate::HashedStorage<#scrate::#hasher>>(storage: &mut S, head: Option<&#kty>) {
match head {
Some(head) => storage.put(#final_head_key, head),
None => storage.kill(#final_head_key),
@@ -489,7 +489,7 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
}
/// Take the value, reading and removing it.
fn take<S: #scrate::HashedStorage<#scrate::#hasher>>(key: &#kty, storage: &S) -> Self::Query {
fn take<S: #scrate::HashedStorage<#scrate::#hasher>>(key: &#kty, storage: &mut S) -> Self::Query {
use self::#inner_module::Utils;
let res: Option<(#value_type, self::#inner_module::Linkage<#kty>)> = storage.take(&*#as_map::key_for(key));
@@ -503,12 +503,12 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
}
/// Remove the value under a key.
fn remove<S: #scrate::HashedStorage<#scrate::#hasher>>(key: &#kty, storage: &S) {
fn remove<S: #scrate::HashedStorage<#scrate::#hasher>>(key: &#kty, storage: &mut S) {
#as_map::take(key, storage);
}
/// Store a value to be associated with the given key from the map.
fn insert<S: #scrate::HashedStorage<#scrate::#hasher>>(key: &#kty, val: &#typ, storage: &S) {
fn insert<S: #scrate::HashedStorage<#scrate::#hasher>>(key: &#kty, val: &#typ, storage: &mut S) {
use self::#inner_module::Utils;
let key_for = &*#as_map::key_for(key);
@@ -522,7 +522,7 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
}
/// Mutate the value under a key
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: #scrate::HashedStorage<#scrate::#hasher>>(key: &#kty, f: F, storage: &S) -> R {
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: #scrate::HashedStorage<#scrate::#hasher>>(key: &#kty, f: F, storage: &mut S) -> R {
use self::#inner_module::Utils;
let key_for = &*#as_map::key_for(key);
@@ -643,12 +643,12 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
storage.get(&key).#option_simple_1(|| #fielddefault)
}
fn take<S: #scrate::UnhashedStorage>(key1: &#k1ty, key2: &#k2ty, storage: &S) -> Self::Query {
fn take<S: #scrate::UnhashedStorage>(key1: &#k1ty, key2: &#k2ty, storage: &mut S) -> Self::Query {
let key = #as_double_map::key_for(key1, key2);
storage.take(&key).#option_simple_1(|| #fielddefault)
}
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: #scrate::UnhashedStorage>(key1: &#k1ty, key2: &#k2ty, f: F, storage: &S) -> R {
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: #scrate::UnhashedStorage>(key1: &#k1ty, key2: &#k2ty, f: F, storage: &mut S) -> R {
let mut val = #as_double_map::get(key1, key2, storage);
let ret = f(&mut val);
@@ -295,7 +295,7 @@ fn decl_store_extra_genesis(
use #scrate::codec::{Encode, Decode};
let v = (#builder)(&self);
<#name<#traitinstance, #instance> as #scrate::storage::hashed::generator::StorageValue<#typ>>::put(&v, &storage);
<#name<#traitinstance, #instance> as #scrate::storage::hashed::generator::StorageValue<#typ>>::put(&v, storage);
}}
},
DeclStorageTypeInfosKind::Map { key_type, .. } => {
@@ -305,7 +305,7 @@ fn decl_store_extra_genesis(
let data = (#builder)(&self);
for (k, v) in data.into_iter() {
<#name<#traitinstance, #instance> as #scrate::storage::hashed::generator::StorageMap<#key_type, #typ>>::insert(&k, &v, &storage);
<#name<#traitinstance, #instance> as #scrate::storage::hashed::generator::StorageMap<#key_type, #typ>>::insert(&k, &v, storage);
}
}}
},
@@ -316,7 +316,7 @@ fn decl_store_extra_genesis(
let data = (#builder)(&self);
for (k1, k2, v) in data.into_iter() {
<#name<#traitinstance, #instance> as #scrate::storage::unhashed::generator::StorageDoubleMap<#key1_type, #key2_type, #typ>>::insert(&k1, &k2, &v, &storage);
<#name<#traitinstance, #instance> as #scrate::storage::unhashed::generator::StorageDoubleMap<#key1_type, #key2_type, #typ>>::insert(&k1, &k2, &v, storage);
}
}}
},
@@ -451,12 +451,11 @@ fn decl_store_extra_genesis(
#[cfg(feature = "std")]
impl#fparam_impl #scrate::runtime_primitives::BuildStorage for GenesisConfig#sparam {
fn assimilate_storage(self, r: &mut #scrate::runtime_primitives::StorageOverlay, c: &mut #scrate::runtime_primitives::ChildrenStorageOverlay) -> ::std::result::Result<(), String> {
use #scrate::rstd::cell::RefCell;
let storage = RefCell::new(r);
let storage = r;
#builders
let r = storage.into_inner();
let r = storage;
#scall(r, c, &self);
@@ -103,25 +103,25 @@ pub trait HashedStorage<H: StorageHasher> {
}
/// Put a value in under a key.
fn put<T: codec::Encode>(&self, key: &[u8], val: &T);
fn put<T: codec::Encode>(&mut self, key: &[u8], val: &T);
/// Remove the bytes of a key from storage.
fn kill(&self, key: &[u8]);
fn kill(&mut self, key: &[u8]);
/// Take a value from storage, deleting it after reading.
fn take<T: codec::Decode>(&self, key: &[u8]) -> Option<T> {
fn take<T: codec::Decode>(&mut self, key: &[u8]) -> Option<T> {
let value = self.get(key);
self.kill(key);
value
}
/// Take a value from storage, deleting it after reading.
fn take_or_panic<T: codec::Decode>(&self, key: &[u8]) -> T {
fn take_or_panic<T: codec::Decode>(&mut self, key: &[u8]) -> T {
self.take(key).expect("Required values must be in storage")
}
/// Take a value from storage, deleting it after reading.
fn take_or_default<T: codec::Decode + Default>(&self, key: &[u8]) -> T {
fn take_or_default<T: codec::Decode + Default>(&mut self, key: &[u8]) -> T {
self.take(key).unwrap_or_default()
}
@@ -129,12 +129,12 @@ pub trait HashedStorage<H: StorageHasher> {
fn get_raw(&self, key: &[u8]) -> Option<Vec<u8>>;
/// Put a raw byte slice into storage.
fn put_raw(&self, key: &[u8], value: &[u8]);
fn put_raw(&mut self, key: &[u8], value: &[u8]);
}
// We use a construct like this during when genesis storage is being built.
#[cfg(feature = "std")]
impl<H: StorageHasher> HashedStorage<H> for std::cell::RefCell<&mut sr_primitives::StorageOverlay> {
impl<H: StorageHasher> HashedStorage<H> for sr_primitives::StorageOverlay {
fn exists(&self, key: &[u8]) -> bool {
UnhashedStorage::exists(self, &H::hash(key).as_ref())
}
@@ -143,11 +143,11 @@ impl<H: StorageHasher> HashedStorage<H> for std::cell::RefCell<&mut sr_primitive
UnhashedStorage::get(self, &H::hash(key).as_ref())
}
fn put<T: codec::Encode>(&self, key: &[u8], val: &T) {
fn put<T: codec::Encode>(&mut self, key: &[u8], val: &T) {
UnhashedStorage::put(self, &H::hash(key).as_ref(), val)
}
fn kill(&self, key: &[u8]) {
fn kill(&mut self, key: &[u8]) {
UnhashedStorage::kill(self, &H::hash(key).as_ref())
}
@@ -155,7 +155,7 @@ impl<H: StorageHasher> HashedStorage<H> for std::cell::RefCell<&mut sr_primitive
UnhashedStorage::get_raw(self, &H::hash(key).as_ref())
}
fn put_raw(&self, key: &[u8], value: &[u8]) {
fn put_raw(&mut self, key: &[u8], value: &[u8]) {
UnhashedStorage::put_raw(self, &H::hash(key).as_ref(), value)
}
}
@@ -177,18 +177,18 @@ pub trait StorageValue<T: codec::Codec> {
fn get<S: HashedStorage<Twox128>>(storage: &S) -> Self::Query;
/// Take a value from storage, removing it afterwards.
fn take<S: HashedStorage<Twox128>>(storage: &S) -> Self::Query;
fn take<S: HashedStorage<Twox128>>(storage: &mut S) -> Self::Query;
/// Store a value under this key into the provided storage instance.
fn put<S: HashedStorage<Twox128>>(val: &T, storage: &S) {
fn put<S: HashedStorage<Twox128>>(val: &T, storage: &mut S) {
storage.put(Self::key(), val)
}
/// Mutate this value
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: HashedStorage<Twox128>>(f: F, storage: &S) -> R;
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: HashedStorage<Twox128>>(f: F, storage: &mut S) -> R;
/// Clear the storage value.
fn kill<S: HashedStorage<Twox128>>(storage: &S) {
fn kill<S: HashedStorage<Twox128>>(storage: &mut S) {
storage.kill(Self::key())
}
@@ -196,7 +196,7 @@ pub trait StorageValue<T: codec::Codec> {
///
/// `T` is required to implement `codec::EncodeAppend`.
fn append<S: HashedStorage<Twox128>, I: codec::Encode>(
items: &[I], storage: &S
items: &[I], storage: &mut S
) -> Result<(), &'static str> where T: codec::EncodeAppend<Item=I> {
let new_val = <T as codec::EncodeAppend>::append(
storage.get_raw(Self::key()).unwrap_or_default(),
@@ -222,10 +222,10 @@ pub trait StorageList<T: codec::Codec> {
fn items<S: HashedStorage<Twox128>>(storage: &S) -> Vec<T>;
/// Set the current set of items.
fn set_items<S: HashedStorage<Twox128>>(items: &[T], storage: &S);
fn set_items<S: HashedStorage<Twox128>>(items: &[T], storage: &mut S);
/// Set the item at the given index.
fn set_item<S: HashedStorage<Twox128>>(index: u32, item: &T, storage: &S);
fn set_item<S: HashedStorage<Twox128>>(index: u32, item: &T, storage: &mut S);
/// Load the value at given index. Returns `None` if the index is out-of-bounds.
fn get<S: HashedStorage<Twox128>>(index: u32, storage: &S) -> Option<T>;
@@ -234,7 +234,7 @@ pub trait StorageList<T: codec::Codec> {
fn len<S: HashedStorage<Twox128>>(storage: &S) -> u32;
/// Clear the list.
fn clear<S: HashedStorage<Twox128>>(storage: &S);
fn clear<S: HashedStorage<Twox128>>(storage: &mut S);
}
/// A strongly-typed map in storage.
@@ -259,20 +259,20 @@ pub trait StorageMap<K: codec::Codec, V: codec::Codec> {
fn get<S: HashedStorage<Self::Hasher>>(key: &K, storage: &S) -> Self::Query;
/// Take the value under a key.
fn take<S: HashedStorage<Self::Hasher>>(key: &K, storage: &S) -> Self::Query;
fn take<S: HashedStorage<Self::Hasher>>(key: &K, storage: &mut S) -> Self::Query;
/// Store a value to be associated with the given key from the map.
fn insert<S: HashedStorage<Self::Hasher>>(key: &K, val: &V, storage: &S) {
fn insert<S: HashedStorage<Self::Hasher>>(key: &K, val: &V, storage: &mut S) {
storage.put(&Self::key_for(key)[..], val);
}
/// Remove the value under a key.
fn remove<S: HashedStorage<Self::Hasher>>(key: &K, storage: &S) {
fn remove<S: HashedStorage<Self::Hasher>>(key: &K, storage: &mut S) {
storage.kill(&Self::key_for(key)[..]);
}
/// Mutate the value under a key.
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: HashedStorage<Self::Hasher>>(key: &K, f: F, storage: &S) -> R;
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: HashedStorage<Self::Hasher>>(key: &K, f: F, storage: &mut S) -> R;
}
/// A `StorageMap` with enumerable entries.
+27 -27
View File
@@ -70,17 +70,17 @@ impl<H: StorageHasher> HashedStorage<H> for RuntimeStorage {
}
/// Put a value in under a key.
fn put<T: Encode>(&self, key: &[u8], val: &T) {
fn put<T: Encode>(&mut self, key: &[u8], val: &T) {
hashed::put(&H::hash, key, val)
}
/// Remove the bytes of a key from storage.
fn kill(&self, key: &[u8]) {
fn kill(&mut self, key: &[u8]) {
hashed::kill(&H::hash, key)
}
/// Take a value from storage, deleting it after reading.
fn take<T: Decode>(&self, key: &[u8]) -> Option<T> {
fn take<T: Decode>(&mut self, key: &[u8]) -> Option<T> {
hashed::take(&H::hash, key)
}
@@ -88,7 +88,7 @@ impl<H: StorageHasher> HashedStorage<H> for RuntimeStorage {
hashed::get_raw(&H::hash, key)
}
fn put_raw(&self, key: &[u8], value: &[u8]) {
fn put_raw(&mut self, key: &[u8], value: &[u8]) {
hashed::put_raw(&H::hash, key, value)
}
}
@@ -104,22 +104,22 @@ impl UnhashedStorage for RuntimeStorage {
}
/// Put a value in under a key.
fn put<T: Encode>(&self, key: &[u8], val: &T) {
fn put<T: Encode>(&mut self, key: &[u8], val: &T) {
unhashed::put(key, val)
}
/// Remove the bytes of a key from storage.
fn kill(&self, key: &[u8]) {
fn kill(&mut self, key: &[u8]) {
unhashed::kill(key)
}
/// Remove the bytes of a key from storage.
fn kill_prefix(&self, prefix: &[u8]) {
fn kill_prefix(&mut self, prefix: &[u8]) {
unhashed::kill_prefix(prefix)
}
/// Take a value from storage, deleting it after reading.
fn take<T: Decode>(&self, key: &[u8]) -> Option<T> {
fn take<T: Decode>(&mut self, key: &[u8]) -> Option<T> {
unhashed::take(key)
}
@@ -127,7 +127,7 @@ impl UnhashedStorage for RuntimeStorage {
unhashed::get_raw(key)
}
fn put_raw(&self, key: &[u8], value: &[u8]) {
fn put_raw(&mut self, key: &[u8], value: &[u8]) {
unhashed::put_raw(key, value)
}
}
@@ -178,21 +178,21 @@ impl<T: Codec, U> StorageValue<T> for U where U: hashed::generator::StorageValue
U::get(&RuntimeStorage)
}
fn put<Arg: Borrow<T>>(val: Arg) {
U::put(val.borrow(), &RuntimeStorage)
U::put(val.borrow(), &mut RuntimeStorage)
}
fn mutate<R, F: FnOnce(&mut Self::Query) -> R>(f: F) -> R {
U::mutate(f, &RuntimeStorage)
U::mutate(f, &mut RuntimeStorage)
}
fn kill() {
U::kill(&RuntimeStorage)
U::kill(&mut RuntimeStorage)
}
fn take() -> Self::Query {
U::take(&RuntimeStorage)
U::take(&mut RuntimeStorage)
}
fn append<I: Encode>(items: &[I]) -> Result<(), &'static str>
where T: EncodeAppend<Item=I>
{
U::append(items, &RuntimeStorage)
U::append(items, &mut RuntimeStorage)
}
}
@@ -244,11 +244,11 @@ impl<T: Codec, U> StorageList<T> for U where U: hashed::generator::StorageList<T
}
fn set_items(items: &[T]) {
U::set_items(items, &RuntimeStorage)
U::set_items(items, &mut RuntimeStorage)
}
fn set_item<Arg: Borrow<T>>(index: u32, val: Arg) {
U::set_item(index, val.borrow(), &RuntimeStorage)
U::set_item(index, val.borrow(), &mut RuntimeStorage)
}
fn get(index: u32) -> Option<T> {
@@ -260,7 +260,7 @@ impl<T: Codec, U> StorageList<T> for U where U: hashed::generator::StorageList<T
}
fn clear() {
U::clear(&RuntimeStorage)
U::clear(&mut RuntimeStorage)
}
}
@@ -314,19 +314,19 @@ impl<K: Codec, V: Codec, U> StorageMap<K, V> for U where U: hashed::generator::S
}
fn insert<KeyArg: Borrow<K>, ValArg: Borrow<V>>(key: KeyArg, val: ValArg) {
U::insert(key.borrow(), val.borrow(), &RuntimeStorage)
U::insert(key.borrow(), val.borrow(), &mut RuntimeStorage)
}
fn remove<KeyArg: Borrow<K>>(key: KeyArg) {
U::remove(key.borrow(), &RuntimeStorage)
U::remove(key.borrow(), &mut RuntimeStorage)
}
fn mutate<KeyArg: Borrow<K>, R, F: FnOnce(&mut Self::Query) -> R>(key: KeyArg, f: F) -> R {
U::mutate(key.borrow(), f, &RuntimeStorage)
U::mutate(key.borrow(), f, &mut RuntimeStorage)
}
fn take<KeyArg: Borrow<K>>(key: KeyArg) -> Self::Query {
U::take(key.borrow(), &RuntimeStorage)
U::take(key.borrow(), &mut RuntimeStorage)
}
}
@@ -444,19 +444,19 @@ where
}
fn take<KArg1: Borrow<K1>, KArg2: Borrow<K2>>(k1: KArg1, k2: KArg2) -> Self::Query {
U::take(k1.borrow(), k2.borrow(), &RuntimeStorage)
U::take(k1.borrow(), k2.borrow(), &mut RuntimeStorage)
}
fn insert<KArg1: Borrow<K1>, KArg2: Borrow<K2>, VArg: Borrow<V>>(k1: KArg1, k2: KArg2, val: VArg) {
U::insert(k1.borrow(), k2.borrow(), val.borrow(), &RuntimeStorage)
U::insert(k1.borrow(), k2.borrow(), val.borrow(), &mut RuntimeStorage)
}
fn remove<KArg1: Borrow<K1>, KArg2: Borrow<K2>>(k1: KArg1, k2: KArg2) {
U::remove(k1.borrow(), k2.borrow(), &RuntimeStorage)
U::remove(k1.borrow(), k2.borrow(), &mut RuntimeStorage)
}
fn remove_prefix<KArg1: Borrow<K1>>(k1: KArg1) {
U::remove_prefix(k1.borrow(), &RuntimeStorage)
U::remove_prefix(k1.borrow(), &mut RuntimeStorage)
}
fn mutate<KArg1, KArg2, R, F>(k1: KArg1, k2: KArg2, f: F) -> R
@@ -465,7 +465,7 @@ where
KArg2: Borrow<K2>,
F: FnOnce(&mut Self::Query) -> R
{
U::mutate(k1.borrow(), k2.borrow(), f, &RuntimeStorage)
U::mutate(k1.borrow(), k2.borrow(), f, &mut RuntimeStorage)
}
fn append<KArg1, KArg2, I>(
@@ -479,7 +479,7 @@ where
I: codec::Encode,
V: EncodeAppend<Item=I>,
{
U::append(k1.borrow(), k2.borrow(), items, &RuntimeStorage)
U::append(k1.borrow(), k2.borrow(), items, &mut RuntimeStorage)
}
}
@@ -197,12 +197,12 @@ macro_rules! __storage_items_internal {
}
/// Take a value from storage, removing it afterwards.
fn take<S: $crate::HashedStorage<$crate::Twox128>>(storage: &S) -> Self::Query {
fn take<S: $crate::HashedStorage<$crate::Twox128>>(storage: &mut S) -> Self::Query {
storage.$taker($key)
}
/// Mutate this value.
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: $crate::HashedStorage<$crate::Twox128>>(f: F, storage: &S) -> R {
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: $crate::HashedStorage<$crate::Twox128>>(f: F, storage: &mut S) -> R {
let mut val = <Self as $crate::storage::hashed::generator::StorageValue<$ty>>::get(storage);
let ret = f(&mut val);
@@ -256,13 +256,13 @@ macro_rules! __storage_items_internal {
}
/// Take the value, reading and removing it.
fn take<S: $crate::HashedStorage<Self::Hasher>>(key: &$kty, storage: &S) -> Self::Query {
fn take<S: $crate::HashedStorage<Self::Hasher>>(key: &$kty, storage: &mut S) -> Self::Query {
let key = <$name as $crate::storage::hashed::generator::StorageMap<$kty, $ty>>::key_for(key);
storage.$taker(&key[..])
}
/// Mutate the value under a key.
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: $crate::HashedStorage<Self::Hasher>>(key: &$kty, f: F, storage: &S) -> R {
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: $crate::HashedStorage<Self::Hasher>>(key: &$kty, f: F, storage: &mut S) -> R {
let mut val = <Self as $crate::storage::hashed::generator::StorageMap<$kty, $ty>>::take(key, storage);
let ret = f(&mut val);
@@ -287,13 +287,13 @@ macro_rules! __storage_items_internal {
$($vis)* struct $name;
impl $name {
fn clear_item<S: $crate::HashedStorage<$crate::Twox128>>(index: u32, storage: &S) {
fn clear_item<S: $crate::HashedStorage<$crate::Twox128>>(index: u32, storage: &mut S) {
if index < <$name as $crate::storage::hashed::generator::StorageList<$ty>>::len(storage) {
storage.kill(&<$name as $crate::storage::hashed::generator::StorageList<$ty>>::key_for(index));
}
}
fn set_len<S: $crate::HashedStorage<$crate::Twox128>>(count: u32, storage: &S) {
fn set_len<S: $crate::HashedStorage<$crate::Twox128>>(count: u32, storage: &mut S) {
(count..<$name as $crate::storage::hashed::generator::StorageList<$ty>>::len(storage)).for_each(|i| $name::clear_item(i, storage));
storage.put(&<$name as $crate::storage::hashed::generator::StorageList<$ty>>::len_key(), &count);
}
@@ -327,14 +327,14 @@ macro_rules! __storage_items_internal {
}
/// Set the current set of items.
fn set_items<S: $crate::HashedStorage<$crate::Twox128>>(items: &[$ty], storage: &S) {
fn set_items<S: $crate::HashedStorage<$crate::Twox128>>(items: &[$ty], storage: &mut S) {
$name::set_len(items.len() as u32, storage);
items.iter()
.enumerate()
.for_each(|(i, item)| <$name as $crate::storage::hashed::generator::StorageList<$ty>>::set_item(i as u32, item, storage));
}
fn set_item<S: $crate::HashedStorage<$crate::Twox128>>(index: u32, item: &$ty, storage: &S) {
fn set_item<S: $crate::HashedStorage<$crate::Twox128>>(index: u32, item: &$ty, storage: &mut S) {
if index < <$name as $crate::storage::hashed::generator::StorageList<$ty>>::len(storage) {
storage.put(&<$name as $crate::storage::hashed::generator::StorageList<$ty>>::key_for(index)[..], item);
}
@@ -351,7 +351,7 @@ macro_rules! __storage_items_internal {
}
/// Clear the list.
fn clear<S: $crate::HashedStorage<$crate::Twox128>>(storage: &S) {
fn clear<S: $crate::HashedStorage<$crate::Twox128>>(storage: &mut S) {
for i in 0..<$name as $crate::storage::hashed::generator::StorageList<$ty>>::len(storage) {
$name::clear_item(i, storage);
}
@@ -383,7 +383,6 @@ macro_rules! __handle_wrap_internal {
#[allow(dead_code)]
mod tests {
use std::collections::HashMap;
use std::cell::RefCell;
use super::*;
use crate::metadata::*;
use crate::metadata::StorageHasher;
@@ -398,43 +397,40 @@ mod tests {
#[test]
fn value() {
let mut overlay = HashMap::new();
let storage = RefCell::new(&mut overlay);
let mut storage = HashMap::new();
assert!(Value::get(&storage).is_none());
Value::put(&100_000, &storage);
Value::put(&100_000, &mut storage);
assert_eq!(Value::get(&storage), Some(100_000));
Value::kill(&storage);
Value::kill(&mut storage);
assert!(Value::get(&storage).is_none());
}
#[test]
fn list() {
let mut overlay = HashMap::new();
let storage = RefCell::new(&mut overlay);
let mut storage = HashMap::new();
assert_eq!(List::len(&storage), 0);
assert!(List::items(&storage).is_empty());
List::set_items(&[0, 2, 4, 6, 8], &storage);
List::set_items(&[0, 2, 4, 6, 8], &mut storage);
assert_eq!(List::items(&storage), &[0, 2, 4, 6, 8]);
assert_eq!(List::len(&storage), 5);
List::set_item(2, &10, &storage);
List::set_item(2, &10, &mut storage);
assert_eq!(List::items(&storage), &[0, 2, 10, 6, 8]);
assert_eq!(List::len(&storage), 5);
List::clear(&storage);
List::clear(&mut storage);
assert_eq!(List::len(&storage), 0);
assert!(List::items(&storage).is_empty());
}
#[test]
fn map() {
let mut overlay = HashMap::new();
let storage = RefCell::new(&mut overlay);
let mut storage = HashMap::new();
assert!(Map::get(&5, &storage).is_none());
Map::insert(&5, &[1; 32], &storage);
Map::insert(&5, &[1; 32], &mut storage);
assert_eq!(Map::get(&5, &storage), Some([1; 32]));
assert_eq!(Map::take(&5, &storage), Some([1; 32]));
assert_eq!(Map::take(&5, &mut storage), Some([1; 32]));
assert!(Map::get(&5, &storage).is_none());
assert!(Map::get(&999, &storage).is_none());
}
@@ -34,66 +34,66 @@ pub trait UnhashedStorage {
fn get_or_default<T: codec::Decode + Default>(&self, key: &[u8]) -> T { self.get(key).unwrap_or_default() }
/// Put a value in under a key.
fn put<T: codec::Encode>(&self, key: &[u8], val: &T);
fn put<T: codec::Encode>(&mut self, key: &[u8], val: &T);
/// Remove the bytes of a key from storage.
fn kill(&self, key: &[u8]);
fn kill(&mut self, key: &[u8]);
/// Remove the bytes of a key from storage.
fn kill_prefix(&self, prefix: &[u8]);
fn kill_prefix(&mut self, prefix: &[u8]);
/// Take a value from storage, deleting it after reading.
fn take<T: codec::Decode>(&self, key: &[u8]) -> Option<T> {
fn take<T: codec::Decode>(&mut self, key: &[u8]) -> Option<T> {
let value = self.get(key);
self.kill(key);
value
}
/// Take a value from storage, deleting it after reading.
fn take_or_panic<T: codec::Decode>(&self, key: &[u8]) -> T { self.take(key).expect("Required values must be in storage") }
fn take_or_panic<T: codec::Decode>(&mut self, key: &[u8]) -> T { self.take(key).expect("Required values must be in storage") }
/// Take a value from storage, deleting it after reading.
fn take_or_default<T: codec::Decode + Default>(&self, key: &[u8]) -> T { self.take(key).unwrap_or_default() }
fn take_or_default<T: codec::Decode + Default>(&mut self, key: &[u8]) -> T { self.take(key).unwrap_or_default() }
/// Get a Vec of bytes from storage.
fn get_raw(&self, key: &[u8]) -> Option<Vec<u8>>;
/// Put a raw byte slice into storage.
fn put_raw(&self, key: &[u8], value: &[u8]);
fn put_raw(&mut self, key: &[u8], value: &[u8]);
}
// We use a construct like this during when genesis storage is being built.
#[cfg(feature = "std")]
impl UnhashedStorage for std::cell::RefCell<&mut sr_primitives::StorageOverlay> {
impl UnhashedStorage for sr_primitives::StorageOverlay {
fn exists(&self, key: &[u8]) -> bool {
self.borrow().contains_key(key)
self.contains_key(key)
}
fn get<T: codec::Decode>(&self, key: &[u8]) -> Option<T> {
self.borrow().get(key)
self.get(key)
.map(|x| codec::Decode::decode(&mut x.as_slice()).expect("Unable to decode expected type."))
}
fn put<T: codec::Encode>(&self, key: &[u8], val: &T) {
self.borrow_mut().insert(key.to_vec(), codec::Encode::encode(val));
fn put<T: codec::Encode>(&mut self, key: &[u8], val: &T) {
self.insert(key.to_vec(), codec::Encode::encode(val));
}
fn kill(&self, key: &[u8]) {
self.borrow_mut().remove(key);
fn kill(&mut self, key: &[u8]) {
self.remove(key);
}
fn kill_prefix(&self, prefix: &[u8]) {
self.borrow_mut().retain(|key, _| {
fn kill_prefix(&mut self, prefix: &[u8]) {
self.retain(|key, _| {
!key.starts_with(prefix)
})
}
fn get_raw(&self, key: &[u8]) -> Option<Vec<u8>> {
self.borrow().get(key).cloned()
self.get(key).cloned()
}
fn put_raw(&self, key: &[u8], value: &[u8]) {
self.borrow_mut().insert(key.to_vec(), value.to_vec());
fn put_raw(&mut self, key: &[u8], value: &[u8]) {
self.insert(key.to_vec(), value.to_vec());
}
}
@@ -131,32 +131,32 @@ pub trait StorageDoubleMap<K1: codec::Codec, K2: codec::Codec, V: codec::Codec>
fn get<S: UnhashedStorage>(k1: &K1, k2: &K2, storage: &S) -> Self::Query;
/// Take the value under a key.
fn take<S: UnhashedStorage>(k1: &K1, k2: &K2, storage: &S) -> Self::Query;
fn take<S: UnhashedStorage>(k1: &K1, k2: &K2, storage: &mut S) -> Self::Query;
/// Store a value to be associated with the given key from the map.
fn insert<S: UnhashedStorage>(k1: &K1, k2: &K2, val: &V, storage: &S) {
fn insert<S: UnhashedStorage>(k1: &K1, k2: &K2, val: &V, storage: &mut S) {
storage.put(&Self::key_for(k1, k2), val);
}
/// Remove the value under a key.
fn remove<S: UnhashedStorage>(k1: &K1, k2: &K2, storage: &S) {
fn remove<S: UnhashedStorage>(k1: &K1, k2: &K2, storage: &mut S) {
storage.kill(&Self::key_for(k1, k2));
}
/// Removes all entries that shares the `k1` as the first key.
fn remove_prefix<S: UnhashedStorage>(k1: &K1, storage: &S) {
fn remove_prefix<S: UnhashedStorage>(k1: &K1, storage: &mut S) {
storage.kill_prefix(&Self::prefix_for(k1));
}
/// Mutate the value under a key.
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: UnhashedStorage>(k1: &K1, k2: &K2, f: F, storage: &S) -> R;
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: UnhashedStorage>(k1: &K1, k2: &K2, f: F, storage: &mut S) -> R;
/// Append the given items to the value under the key specified.
fn append<I, S: UnhashedStorage>(
k1: &K1,
k2: &K2,
items: &[I],
storage: &S,
storage: &mut S,
) -> Result<(), &'static str>
where
I: codec::Encode,