Skip to content
Snippets Groups Projects
Commit 3ed67849 authored by asynchronous rob's avatar asynchronous rob
Browse files

use new environmental API

parent 21ab1935
No related merge requests found
......@@ -21,21 +21,17 @@ impl fmt::Display for NoError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "") }
}
pub struct ExternalitiesHolder<'a> {
ext: &'a mut Externalities<Error=NoError>,
}
declare_generic!(ext : ExternalitiesHolder);
environmental!(ext : Externalities<Error=NoError> + 'static);
pub fn storage(key: &[u8]) -> Vec<u8> {
ext::with(|holder| holder.ext.storage(key).ok().map(|s| s.to_vec()))
ext::with(|ext| ext.storage(key).ok().map(|s| s.to_vec()))
.unwrap_or(None)
.unwrap_or_else(|| vec![])
}
pub fn read_storage(key: &[u8], value_out: &mut [u8]) -> usize {
ext::with(|holder| {
if let Ok(value) = holder.ext.storage(key) {
ext::with(|ext| {
if let Ok(value) = ext.storage(key) {
let written = ::std::cmp::min(value.len(), value_out.len());
value_out[0..written].copy_from_slice(&value[0..written]);
value.len()
......@@ -48,8 +44,8 @@ pub fn read_storage(key: &[u8], value_out: &mut [u8]) -> usize {
pub fn storage_into<T: Sized>(_key: &[u8]) -> Option<T> {
let size = size_of::<T>();
ext::with(|holder| {
if let Ok(value) = holder.ext.storage(_key) {
ext::with(|ext| {
if let Ok(value) = ext.storage(_key) {
if value.len() == size {
unsafe {
let mut result: T = std::mem::uninitialized();
......@@ -64,15 +60,15 @@ pub fn storage_into<T: Sized>(_key: &[u8]) -> Option<T> {
}
pub fn set_storage(_key: &[u8], _value: &[u8]) {
ext::with(|holder|
holder.ext.set_storage(_key.to_vec(), _value.to_vec())
ext::with(|ext|
ext.set_storage(_key.to_vec(), _value.to_vec())
);
}
/// The current relay chain identifier.
pub fn chain_id() -> u64 {
ext::with(|holder|
holder.ext.chain_id()
ext::with(|ext|
ext.chain_id()
).unwrap_or(0)
}
......@@ -81,9 +77,8 @@ pub use tiny_keccak::keccak256;
/// Execute the given closure with global function available whose functionality routes into the
/// externalities `ext`. Forwards the value that the closure returns.
pub fn with_externalities<R, F: FnOnce() -> R>(ext: &mut Externalities<Error=NoError>, f: F) -> R {
let mut h = ExternalitiesHolder { ext };
ext::using(&mut h, f)
pub fn with_externalities<R, F: FnOnce() -> R>(ext: &mut (Externalities<Error=NoError> + 'static), f: F) -> R {
ext::using(ext, f)
}
#[macro_export]
......
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