Derivable Encode & Decode (#509)

* Derive macro for simple structs.

* Derive Encode/Decode wherever we can.

* Derive for enums.

* Support discriminant.

* Get rid of some repetition.

* Support custom indices.

* Derive codec for enums wherever possible.

* Fix no_std

* WASM rebuilt

* Avoid excessive import.

* Fix compilation.

* Address review grumbles.
This commit is contained in:
Tomasz Drwięga
2018-08-08 17:47:22 +02:00
committed by Sergey Pepyakin
parent 66432490fa
commit 5fe73de49f
18 changed files with 79 additions and 437 deletions
+6 -33
View File
@@ -46,6 +46,9 @@
/// Re-export of substrate-codec.
pub extern crate substrate_codec as codec;
#[macro_use]
extern crate substrate_codec_derive;
#[cfg(not(feature = "std"))]
extern crate alloc;
@@ -61,14 +64,14 @@ extern crate error_chain;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use codec::{Encode, Decode, Input, Output};
use codec::{Encode, Decode};
#[cfg(feature = "std")]
pub mod wasm;
/// Validation parameters for evaluating the parachain validity function.
// TODO: consolidated ingress and balance downloads
#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct ValidationParams {
/// The collation body.
@@ -77,45 +80,15 @@ pub struct ValidationParams {
pub parent_head: Vec<u8>,
}
impl Encode for ValidationParams {
fn encode_to<T: Output>(&self, dest: &mut T) {
dest.push(&self.block_data);
dest.push(&self.parent_head);
}
}
impl Decode for ValidationParams {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(ValidationParams {
block_data: Decode::decode(input)?,
parent_head: Decode::decode(input)?,
})
}
}
/// The result of parachain validation.
// TODO: egress and balance uploads
#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct ValidationResult {
/// New head data that should be included in the relay chain state.
pub head_data: Vec<u8>
}
impl Encode for ValidationResult {
fn encode_to<T: Output>(&self, dest: &mut T) {
dest.push(&self.head_data);
}
}
impl Decode for ValidationResult {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Some(ValidationResult {
head_data: Decode::decode(input)?,
})
}
}
/// Load the validation params from memory when implementing a Rust parachain.
///
/// Offset and length must have been provided by the validation