Skip to content
Snippets Groups Projects
Commit 3c0da110 authored by Bastian Köcher's avatar Bastian Köcher Committed by Gav Wood
Browse files

Bkchr impl macros scope (#774)

* Fixes compilation of sandbox tests

* Make `impl_outer_log!` scope independent
parent 1e31b8d8
Branches
No related merge requests found
...@@ -37,7 +37,8 @@ extern crate num_traits; ...@@ -37,7 +37,8 @@ extern crate num_traits;
extern crate integer_sqrt; extern crate integer_sqrt;
extern crate sr_std as rstd; extern crate sr_std as rstd;
extern crate sr_io as runtime_io; extern crate sr_io as runtime_io;
extern crate parity_codec as codec; #[doc(hidden)]
pub extern crate parity_codec as codec;
extern crate substrate_primitives; extern crate substrate_primitives;
#[cfg(test)] #[cfg(test)]
...@@ -250,8 +251,6 @@ macro_rules! impl_outer_config { ...@@ -250,8 +251,6 @@ macro_rules! impl_outer_config {
/// Generated enum is binary-compatible with and could be interpreted /// Generated enum is binary-compatible with and could be interpreted
/// as `generic::DigestItem`. /// as `generic::DigestItem`.
/// ///
/// Requires `use runtime_primitives::generic;` to be used.
///
/// Runtime requirements: /// Runtime requirements:
/// 1) binary representation of all supported 'system' log items should stay /// 1) binary representation of all supported 'system' log items should stay
/// the same. Otherwise, the native code will be unable to read log items /// the same. Otherwise, the native code will be unable to read log items
...@@ -293,53 +292,55 @@ macro_rules! impl_outer_log { ...@@ -293,53 +292,55 @@ macro_rules! impl_outer_log {
/// `self` is a 'system' log && it has been marked as 'system' in macro call. /// `self` is a 'system' log && it has been marked as 'system' in macro call.
/// Otherwise, None is returned. /// Otherwise, None is returned.
#[allow(unreachable_patterns)] #[allow(unreachable_patterns)]
fn dref<'a>(&'a self) -> Option<generic::DigestItemRef<'a, $($genarg),*>> { fn dref<'a>(&'a self) -> Option<$crate::generic::DigestItemRef<'a, $($genarg),*>> {
match self.0 { match self.0 {
$($( $($(
$internal::$module($module::RawLog::$item(ref v)) => $internal::$module($module::RawLog::$item(ref v)) =>
Some(generic::DigestItemRef::$item(v)), Some($crate::generic::DigestItemRef::$item(v)),
)*)* )*)*
_ => None, _ => None,
} }
} }
} }
impl From<generic::DigestItem<$($genarg),*>> for $name { impl From<$crate::generic::DigestItem<$($genarg),*>> for $name {
/// Converts `generic::DigestItem` into `$name`. If `generic::DigestItem` represents /// Converts `generic::DigestItem` into `$name`. If `generic::DigestItem` represents
/// a system item which is supported by the runtime, it is returned. /// a system item which is supported by the runtime, it is returned.
/// Otherwise we expect a `Other` log item. Trying to convert from anything other /// Otherwise we expect a `Other` log item. Trying to convert from anything other
/// will lead to panic in runtime, since the runtime does not supports this 'system' /// will lead to panic in runtime, since the runtime does not supports this 'system'
/// log item. /// log item.
#[allow(unreachable_patterns)] #[allow(unreachable_patterns)]
fn from(gen: generic::DigestItem<$($genarg),*>) -> Self { fn from(gen: $crate::generic::DigestItem<$($genarg),*>) -> Self {
match gen { match gen {
$($( $($(
generic::DigestItem::$item(value) => $crate::generic::DigestItem::$item(value) =>
$name($internal::$module($module::RawLog::$item(value))), $name($internal::$module($module::RawLog::$item(value))),
)*)* )*)*
_ => gen.as_other() _ => gen.as_other()
.and_then(|value| Decode::decode(&mut &value[..])) .and_then(|value| $crate::codec::Decode::decode(&mut &value[..]))
.map($name) .map($name)
.expect("not allowed to fail in runtime"), .expect("not allowed to fail in runtime"),
} }
} }
} }
impl Decode for $name { impl $crate::codec::Decode for $name {
/// `generic::DigestItem` binray compatible decode. /// `generic::DigestItem` binray compatible decode.
fn decode<I: Input>(input: &mut I) -> Option<Self> { fn decode<I: $crate::codec::Input>(input: &mut I) -> Option<Self> {
let gen: generic::DigestItem<$($genarg),*> = Decode::decode(input)?; let gen: $crate::generic::DigestItem<$($genarg),*> =
$crate::codec::Decode::decode(input)?;
Some($name::from(gen)) Some($name::from(gen))
} }
} }
impl Encode for $name { impl $crate::codec::Encode for $name {
/// `generic::DigestItem` binray compatible encode. /// `generic::DigestItem` binray compatible encode.
fn encode(&self) -> Vec<u8> { fn encode(&self) -> Vec<u8> {
match self.dref() { match self.dref() {
Some(dref) => dref.encode(), Some(dref) => dref.encode(),
None => { None => {
let gen: generic::DigestItem<$($genarg),*> = generic::DigestItem::Other(self.0.encode()); let gen: $crate::generic::DigestItem<$($genarg),*> =
$crate::generic::DigestItem::Other(self.0.encode());
gen.encode() gen.encode()
}, },
} }
...@@ -367,12 +368,12 @@ macro_rules! impl_outer_log { ...@@ -367,12 +368,12 @@ macro_rules! impl_outer_log {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use substrate_primitives::hash::H256; use substrate_primitives::hash::H256;
use codec::{Encode, Decode, Input}; use codec::{Encode as EncodeHidden, Decode as DecodeHidden};
pub trait RuntimeT { pub trait RuntimeT {
type AuthorityId; type AuthorityId;
} }
pub struct Runtime; pub struct Runtime;
impl RuntimeT for Runtime { impl RuntimeT for Runtime {
...@@ -397,8 +398,6 @@ mod tests { ...@@ -397,8 +398,6 @@ mod tests {
pub enum RawLog<AuthorityId> { B1(AuthorityId), B2(AuthorityId) } pub enum RawLog<AuthorityId> { B1(AuthorityId), B2(AuthorityId) }
} }
use super::generic; // required before macro invocation
// TODO try to avoid redundant brackets: a(AuthoritiesChange), b // TODO try to avoid redundant brackets: a(AuthoritiesChange), b
impl_outer_log! { impl_outer_log! {
pub enum Log(InternalLog: DigestItem<H256, u64>) for Runtime { pub enum Log(InternalLog: DigestItem<H256, u64>) for Runtime {
...@@ -409,26 +408,26 @@ mod tests { ...@@ -409,26 +408,26 @@ mod tests {
// encode/decode regular item // encode/decode regular item
let b1: Log = b::RawLog::B1::<u64>(777).into(); let b1: Log = b::RawLog::B1::<u64>(777).into();
let encoded_b1 = b1.encode(); let encoded_b1 = b1.encode();
let decoded_b1: Log = Decode::decode(&mut &encoded_b1[..]).unwrap(); let decoded_b1: Log = DecodeHidden::decode(&mut &encoded_b1[..]).unwrap();
assert_eq!(b1, decoded_b1); assert_eq!(b1, decoded_b1);
// encode/decode system item // encode/decode system item
let auth_change: Log = a::RawLog::AuthoritiesChange::<u64>(vec![100, 200, 300]).into(); let auth_change: Log = a::RawLog::AuthoritiesChange::<u64>(vec![100, 200, 300]).into();
let encoded_auth_change = auth_change.encode(); let encoded_auth_change = auth_change.encode();
let decoded_auth_change: Log = Decode::decode(&mut &encoded_auth_change[..]).unwrap(); let decoded_auth_change: Log = DecodeHidden::decode(&mut &encoded_auth_change[..]).unwrap();
assert_eq!(auth_change, decoded_auth_change); assert_eq!(auth_change, decoded_auth_change);
// interpret regular item using `generic::DigestItem` // interpret regular item using `generic::DigestItem`
let generic_b1: generic::DigestItem<H256, u64> = Decode::decode(&mut &encoded_b1[..]).unwrap(); let generic_b1: super::generic::DigestItem<H256, u64> = DecodeHidden::decode(&mut &encoded_b1[..]).unwrap();
match generic_b1 { match generic_b1 {
generic::DigestItem::Other(_) => (), super::generic::DigestItem::Other(_) => (),
_ => panic!("unexpected generic_b1: {:?}", generic_b1), _ => panic!("unexpected generic_b1: {:?}", generic_b1),
} }
// interpret system item using `generic::DigestItem` // interpret system item using `generic::DigestItem`
let generic_auth_change: generic::DigestItem<H256, u64> = Decode::decode(&mut &encoded_auth_change[..]).unwrap(); let generic_auth_change: super::generic::DigestItem<H256, u64> = DecodeHidden::decode(&mut &encoded_auth_change[..]).unwrap();
match generic_auth_change { match generic_auth_change {
generic::DigestItem::AuthoritiesChange::<H256, u64>(authorities) => assert_eq!(authorities, vec![100, 200, 300]), super::generic::DigestItem::AuthoritiesChange::<H256, u64>(authorities) => assert_eq!(authorities, vec![100, 200, 300]),
_ => panic!("unexpected generic_auth_change: {:?}", generic_auth_change), _ => panic!("unexpected generic_auth_change: {:?}", generic_auth_change),
} }
} }
......
...@@ -64,7 +64,6 @@ mod checked_block; ...@@ -64,7 +64,6 @@ mod checked_block;
use rstd::prelude::*; use rstd::prelude::*;
use substrate_primitives::u32_trait::{_2, _4}; use substrate_primitives::u32_trait::{_2, _4};
use codec::{Encode, Decode, Input};
use node_primitives::{AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, SessionKey, Signature, InherentData}; use node_primitives::{AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, SessionKey, Signature, InherentData};
use runtime_primitives::generic; use runtime_primitives::generic;
use runtime_primitives::traits::{Convert, BlakeTwo256, DigestItem}; use runtime_primitives::traits::{Convert, BlakeTwo256, DigestItem};
......
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