Remove StorageList (#2725)

* Remove `StorageList`

`StorageList` is not used anymore by `decl_storage!` and thus, it can be
removed.

* Fixes tests
This commit is contained in:
Bastian Köcher
2019-05-31 09:28:31 +02:00
committed by Gavin Wood
parent 0d9fad431b
commit 80b18c8531
4 changed files with 1 additions and 210 deletions
@@ -207,36 +207,6 @@ pub trait StorageValue<T: codec::Codec> {
}
}
/// A strongly-typed list in storage.
pub trait StorageList<T: codec::Codec> {
/// Get the prefix key in storage.
fn prefix() -> &'static [u8];
/// Get the key used to put the length field.
fn len_key() -> Vec<u8>;
/// Get the storage key used to fetch a value at a given index.
fn key_for(index: u32) -> Vec<u8>;
/// Read out all the items.
fn items<S: HashedStorage<Twox128>>(storage: &S) -> Vec<T>;
/// Set the current set of items.
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: &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>;
/// Load the length of the list
fn len<S: HashedStorage<Twox128>>(storage: &S) -> u32;
/// Clear the list.
fn clear<S: HashedStorage<Twox128>>(storage: &mut S);
}
/// A strongly-typed map in storage.
pub trait StorageMap<K: codec::Codec, V: codec::Codec> {
/// The type that get/take returns.
-68
View File
@@ -196,74 +196,6 @@ impl<T: Codec, U> StorageValue<T> for U where U: hashed::generator::StorageValue
}
}
/// A strongly-typed list in storage.
pub trait StorageList<T: Codec> {
/// Get the prefix key in storage.
fn prefix() -> &'static [u8];
/// Get the key used to store the length field.
fn len_key() -> Vec<u8>;
/// Get the storage key used to fetch a value at a given index.
fn key_for(index: u32) -> Vec<u8>;
/// Read out all the items.
fn items() -> Vec<T>;
/// Set the current set of items.
fn set_items(items: &[T]);
/// Set the item at the given index.
fn set_item<Arg: Borrow<T>>(index: u32, val: Arg);
/// Load the value at given index. Returns `None` if the index is out-of-bounds.
fn get(index: u32) -> Option<T>;
/// Load the length of the list
fn len() -> u32;
/// Clear the list.
fn clear();
}
impl<T: Codec, U> StorageList<T> for U where U: hashed::generator::StorageList<T> {
fn prefix() -> &'static [u8] {
<U as hashed::generator::StorageList<T>>::prefix()
}
fn len_key() -> Vec<u8> {
<U as hashed::generator::StorageList<T>>::len_key()
}
fn key_for(index: u32) -> Vec<u8> {
<U as hashed::generator::StorageList<T>>::key_for(index)
}
fn items() -> Vec<T> {
U::items(&RuntimeStorage)
}
fn set_items(items: &[T]) {
U::set_items(items, &mut RuntimeStorage)
}
fn set_item<Arg: Borrow<T>>(index: u32, val: Arg) {
U::set_item(index, val.borrow(), &mut RuntimeStorage)
}
fn get(index: u32) -> Option<T> {
U::get(index, &RuntimeStorage)
}
fn len() -> u32 {
U::len(&RuntimeStorage)
}
fn clear() {
U::clear(&mut RuntimeStorage)
}
}
/// A strongly-typed map in storage.
pub trait StorageMap<K: Codec, V: Codec> {
/// The type that get/take return.
@@ -22,7 +22,6 @@
//! Three kinds of data types are currently supported:
//! - values
//! - maps
//! - lists
//!
//! # Examples:
//!
@@ -39,8 +38,6 @@
//! pub Value: b"putd_key" => SessionKey;
//! // private map.
//! Balances: b"private_map:" => map [AuthorityId => Balance];
//! // private list.
//! Authorities: b"auth:" => list [AuthorityId];
//! }
//!
//!# fn main() { }
@@ -159,16 +156,6 @@ macro_rules! storage_items {
storage_items!($($t)*);
};
// lists
($name:ident : $prefix:expr => list [$ty:ty]; $($t:tt)*) => {
$crate::__storage_items_internal!(() $name: $prefix => list [$ty]);
storage_items!($($t)*);
};
(pub $name:ident : $prefix:expr => list [$ty:ty]; $($t:tt)*) => {
$crate::__storage_items_internal!((pub) $name: $prefix => list [$ty]);
storage_items!($($t)*);
};
() => ()
}
@@ -282,84 +269,6 @@ macro_rules! __storage_items_internal {
}
}
};
// generator for lists.
(($($vis:tt)*) $name:ident : $prefix:expr => list [$ty:ty]) => {
$($vis)* struct $name;
impl $name {
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: &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);
}
}
impl $crate::storage::hashed::generator::StorageList<$ty> for $name {
/// Get the prefix key in storage.
fn prefix() -> &'static [u8] {
$prefix
}
/// Get the key used to put the length field.
fn len_key() -> $crate::rstd::vec::Vec<u8> {
let mut key = $prefix.to_vec();
key.extend(b"len");
key
}
/// Get the storage key used to fetch a value at a given index.
fn key_for(index: u32) -> $crate::rstd::vec::Vec<u8> {
let mut key = $prefix.to_vec();
$crate::codec::Encode::encode_to(&index, &mut key);
key
}
/// Read out all the items.
fn items<S: $crate::HashedStorage<$crate::Twox128>>(storage: &S) -> $crate::rstd::vec::Vec<$ty> {
(0..<$name as $crate::storage::hashed::generator::StorageList<$ty>>::len(storage))
.map(|i| <$name as $crate::storage::hashed::generator::StorageList<$ty>>::get(i, storage).expect("all items within length are set; qed"))
.collect()
}
/// Set the current set of items.
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: &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);
}
}
/// Load the value at given index. Returns `None` if the index is out-of-bounds.
fn get<S: $crate::HashedStorage<$crate::Twox128>>(index: u32, storage: &S) -> Option<$ty> {
storage.get(&<$name as $crate::storage::hashed::generator::StorageList<$ty>>::key_for(index)[..])
}
/// Load the length of the list.
fn len<S: $crate::HashedStorage<$crate::Twox128>>(storage: &S) -> u32 {
storage.get(&<$name as $crate::storage::hashed::generator::StorageList<$ty>>::len_key()).unwrap_or_default()
}
/// Clear the list.
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);
}
storage.kill(&<$name as $crate::storage::hashed::generator::StorageList<$ty>>::len_key()[..])
}
}
};
}
#[macro_export]
@@ -391,7 +300,6 @@ mod tests {
storage_items! {
Value: b"a" => u32;
List: b"b:" => list [u64];
Map: b"c:" => map [u32 => [u8; 32]];
}
@@ -405,25 +313,6 @@ mod tests {
assert!(Value::get(&storage).is_none());
}
#[test]
fn list() {
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], &mut storage);
assert_eq!(List::items(&storage), &[0, 2, 4, 6, 8]);
assert_eq!(List::len(&storage), 5);
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(&mut storage);
assert_eq!(List::len(&storage), 0);
assert!(List::items(&storage).is_empty());
}
#[test]
fn map() {
let mut storage = HashMap::new();