This commit is contained in:
Gav
2018-01-19 10:49:23 +01:00
parent 81cd156d2a
commit 63809d9e0c
11 changed files with 87 additions and 87 deletions
@@ -1,8 +1,7 @@
pub mod primitives;
pub mod function;
pub mod environment;
pub mod storage;
pub mod storagevec;
pub mod storable;
#[cfg(test)]
pub mod statichex;
@@ -0,0 +1,62 @@
use slicable::Slicable;
use endiansensitive::EndianSensitive;
use keyedvec::KeyedVec;
use runtime_support;
pub trait Storable {
fn lookup_default(key: &[u8]) -> Self where Self: Sized + Default { Self::lookup(key).unwrap_or_else(Default::default) }
fn lookup(_key: &[u8]) -> Option<Self> where Self: Sized { unimplemented!() }
fn store(&self, key: &[u8]);
}
pub fn kill(key: &[u8]) { runtime_support::set_storage(key, b""); }
impl<T: Default + Sized + EndianSensitive> Storable for T {
fn lookup(key: &[u8]) -> Option<Self> {
Slicable::set_as_slice(|out| runtime_support::read_storage(key, out) == out.len())
}
fn store(&self, key: &[u8]) {
self.as_slice_then(|slice| runtime_support::set_storage(key, slice));
}
}
impl Storable for [u8] {
fn store(&self, key: &[u8]) {
runtime_support::set_storage(key, self)
}
}
/// A trait to conveniently store a vector of storable data.
// TODO: add iterator support
pub trait StorageVec {
type Item: Default + Sized + Storable;
const PREFIX: &'static [u8];
/// Get the current set of items.
fn items() -> Vec<Self::Item> {
(0..Self::count()).into_iter().map(Self::item).collect()
}
/// Set the current set of items.
fn set_items(items: &[Self::Item]) {
Self::set_count(items.len() as u32);
items.iter().enumerate().for_each(|(v, ref i)| Self::set_item(v as u32, i));
}
fn set_item(index: u32, item: &Self::Item) {
item.store(&index.to_keyed_vec(Self::PREFIX));
}
fn item(index: u32) -> Self::Item {
Storable::lookup_default(&index.to_keyed_vec(Self::PREFIX))
}
fn set_count(count: u32) {
(count..Self::count()).for_each(|i| Self::set_item(i, &Self::Item::default()));
count.store(&b"len".to_keyed_vec(Self::PREFIX));
}
fn count() -> u32 {
Storable::lookup_default(&b"len".to_keyed_vec(Self::PREFIX))
}
}
@@ -1,24 +0,0 @@
use slicable::Slicable;
use endiansensitive::EndianSensitive;
use runtime_support;
pub trait Storage {
fn into(key: &[u8]) -> Self where Self: Sized + Default { Self::try_into(key).unwrap_or_else(Default::default) }
fn try_into(_key: &[u8]) -> Option<Self> where Self: Sized { unimplemented!() }
fn store(&self, key: &[u8]);
}
impl<T: Default + Sized + EndianSensitive> Storage for T {
fn try_into(key: &[u8]) -> Option<Self> {
Slicable::set_as_slice(|out| runtime_support::read_storage(key, out) == out.len())
}
fn store(&self, key: &[u8]) {
self.as_slice_then(|slice| runtime_support::set_storage(key, slice));
}
}
impl Storage for [u8] {
fn store(&self, key: &[u8]) {
runtime_support::set_storage(key, self)
}
}
@@ -1,38 +0,0 @@
use runtime_support::Vec;
use keyedvec::KeyedVec;
use storage::Storage;
/// A trait to conveniently store a vector of storable data.
// TODO: add iterator support
pub trait StorageVec {
type Item: Default + Sized + Storage;
const PREFIX: &'static [u8];
/// Get the current set of items.
fn items() -> Vec<Self::Item> {
(0..Self::count()).into_iter().map(Self::item).collect()
}
/// Set the current set of items.
fn set_items(items: &[Self::Item]) {
Self::set_count(items.len() as u32);
items.iter().enumerate().for_each(|(v, ref i)| Self::set_item(v as u32, i));
}
fn set_item(index: u32, item: &Self::Item) {
item.store(&index.to_keyed_vec(Self::PREFIX));
}
fn item(index: u32) -> Self::Item {
Storage::into(&index.to_keyed_vec(Self::PREFIX))
}
fn set_count(count: u32) {
(count..Self::count()).for_each(|i| Self::set_item(i, &Self::Item::default()));
count.store(&b"len".to_keyed_vec(Self::PREFIX));
}
fn count() -> u32 {
Storage::into(&b"len".to_keyed_vec(Self::PREFIX))
}
}