Maps are appendable too (#2716)

* Maps are appendable too

* Update srml/support/src/storage/hashed/generator.rs

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Update srml/support/src/storage/mod.rs

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Fix docs

* Make Appendable public

* Bump runtime version
This commit is contained in:
Gavin Wood
2019-05-29 15:15:10 +02:00
committed by GitHub
parent 810d2712da
commit fccc55160a
7 changed files with 94 additions and 5 deletions
+22 -1
View File
@@ -330,6 +330,25 @@ impl<K: Codec, V: Codec, U> StorageMap<K, V> for U where U: hashed::generator::S
}
}
/// A storage map with values that can be appended to.
pub trait AppendableStorageMap<K: Codec, V: Codec>: StorageMap<K, V> {
/// Append the given item to the value in the storage.
///
/// `T` is required to implement `codec::EncodeAppend`.
fn append<KeyArg: Borrow<K>, I: Encode>(key: KeyArg, items: &[I]) -> Result<(), &'static str>
where V: EncodeAppend<Item=I>;
}
impl<K: Codec, V: Codec, U> AppendableStorageMap<K, V> for U
where U: hashed::generator::AppendableStorageMap<K, V>
{
fn append<KeyArg: Borrow<K>, I: Encode>(key: KeyArg, items: &[I]) -> Result<(), &'static str>
where V: EncodeAppend<Item=I>
{
U::append(key.borrow(), items, &mut RuntimeStorage)
}
}
/// A storage map that can be enumerated.
///
/// Primarily useful for off-chain computations.
@@ -342,7 +361,9 @@ pub trait EnumerableStorageMap<K: Codec, V: Codec>: StorageMap<K, V> {
fn enumerate() -> Box<dyn Iterator<Item = (K, V)>> where K: 'static, V: 'static;
}
impl<K: Codec, V: Codec, U> EnumerableStorageMap<K, V> for U where U: hashed::generator::EnumerableStorageMap<K, V> {
impl<K: Codec, V: Codec, U> EnumerableStorageMap<K, V> for U
where U: hashed::generator::EnumerableStorageMap<K, V>
{
fn head() -> Option<K> {
<U as hashed::generator::EnumerableStorageMap<K, V>>::head(&RuntimeStorage)
}