diff --git a/substrate/native-runtime/support/src/lib.rs b/substrate/native-runtime/support/src/lib.rs index a2fe13eb10..d929b699de 100644 --- a/substrate/native-runtime/support/src/lib.rs +++ b/substrate/native-runtime/support/src/lib.rs @@ -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, -} - -declare_generic!(ext : ExternalitiesHolder); +environmental!(ext : Externalities + 'static); pub fn storage(key: &[u8]) -> Vec { - 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(_key: &[u8]) -> Option { let size = size_of::(); - 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(_key: &[u8]) -> Option { } 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>(ext: &mut Externalities, f: F) -> R { - let mut h = ExternalitiesHolder { ext }; - ext::using(&mut h, f) +pub fn with_externalities R>(ext: &mut (Externalities + 'static), f: F) -> R { + ext::using(ext, f) } #[macro_export]