Skip to content
Snippets Groups Projects
Commit adc2e52a authored by Gav's avatar Gav
Browse files

Native storage support API.

parent 391fb191
No related merge requests found
......@@ -425,6 +425,11 @@ name = "lazy_static"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazy_static"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazycell"
version = "0.5.1"
......@@ -568,6 +573,15 @@ dependencies = [
"parking_lot_core 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parking_lot"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot_core 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parking_lot_core"
version = "0.2.6"
......@@ -818,6 +832,10 @@ dependencies = [
[[package]]
name = "runtime-support"
version = "0.1.0"
dependencies = [
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rustc-demangle"
......@@ -1189,6 +1207,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a"
"checksum lazy_static 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c9e5e58fa1a4c3b915a561a78a22ee0cac6ab97dca2504428bc1cb074375f8d5"
"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d"
"checksum lazycell 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3b585b7a6811fb03aa10e74b278a0f00f8dd9b45dc681f148bb29fa5cb61859b"
"checksum libc 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "5ba3df4dcb460b9dfbd070d41c94c19209620c191b0340b929ce748a2bcd42d2"
"checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b"
......@@ -1205,6 +1224,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"
"checksum parity-wasm 0.15.4 (registry+https://github.com/rust-lang/crates.io-index)" = "235801e9531998c4bb307f4ea6833c9f40a4cf132895219ac8c2cd25a9b310f7"
"checksum parking_lot 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "149d8f5b97f3c1133e3cfcd8886449959e856b557ff281e292b733d7c69e005e"
"checksum parking_lot 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3e7f7c9857874e54afeb950eebeae662b1e51a2493666d2ea4c0a5d91dcf0412"
"checksum parking_lot_core 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "4f610cb9664da38e417ea3225f23051f589851999535290e077939838ab7a595"
"checksum patricia-trie 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f1e2f638d79aba5c4a71a4f373df6e3cd702250a53b7f0ed4da1e2a7be9737ae"
"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831"
......
......@@ -5,3 +5,7 @@ authors = ["Parity Technologies <admin@parity.io>"]
[features]
strict = []
[dependencies]
lazy_static = "1.0.0"
parking_lot = "0.5"
#[macro_use]
extern crate lazy_static;
extern crate parking_lot;
pub use std::vec::Vec;
pub use std::rc::Rc;
pub use std::cell::RefCell;
pub use std::boxed::Box;
pub use std::mem::{size_of, transmute};
pub fn storage(_key: &[u8]) -> Vec<u8> { vec![] }
pub fn storage_into<T: Sized>(_key: &[u8]) -> Option<T> { None }
pub fn set_storage(_key: &[u8], _value: &[u8]) {}
use std::collections::HashMap;
use parking_lot::Mutex;
lazy_static! {
static ref HASHMAP: Mutex<HashMap<Vec<u8>, Vec<u8>>> = Mutex::new(HashMap::new());
}
pub fn storage(_key: &[u8]) -> Vec<u8> {
HASHMAP.lock().get(_key).cloned().unwrap_or_else(Vec::new)
}
pub fn storage_into<T: Sized>(_key: &[u8]) -> Option<T> {
let size = size_of::<T>();
if let Some(value) = HASHMAP.lock().get(_key) {
if value.len() == size {
unsafe {
let mut result: T = std::mem::uninitialized();
std::slice::from_raw_parts_mut(transmute::<*mut T, *mut u8>(&mut result), size)
.copy_from_slice(&value);
return Some(result);
}
}
}
None
}
pub fn set_storage(_key: &[u8], _value: &[u8]) {
HASHMAP.lock().insert(_key.to_vec(), _value.to_vec());
}
pub fn init_storage(new: HashMap<Vec<u8>, Vec<u8>>) {
*HASHMAP.lock() = new;
}
#[macro_export]
macro_rules! impl_stubs {
($( $name:ident ),*) => {}
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! map {
($( $name:expr => $value:expr ),*) => (
vec![ $( ( $name, $value ) ),* ].into_iter().collect()
)
}
#[test]
fn storage_works() {
assert_eq!(storage(b"hello"), vec![]);
set_storage(b"hello", b"world");
assert_eq!(storage(b"hello"), b"world".to_vec());
assert_eq!(storage(b"foo"), vec![]);
set_storage(b"foo", &[1, 2, 3][..]);
assert_eq!(storage_into::<[u8; 3]>(b"foo"), Some([1, 2, 3]));
assert_eq!(storage_into::<[u8; 3]>(b"hello"), None);
init_storage(map![b"foo".to_vec() => b"bar".to_vec()]);
assert_eq!(storage(b"hello"), vec![]);
assert_eq!(storage(b"foo"), b"bar".to_vec());
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment