Skip to content

Commit

Permalink
add no_std support (#42)
Browse files Browse the repository at this point in the history
* Metadata supporting scale-info can be decoded and serialized in no_std (#2)

* Metadata supporting scale-info can be decoded and serialized in no_std (feature flag scale_info)

* Changes from review: rename feature flag into full_derive

* Changes from review: std implies full-derive

* Changes from review: optimization

Co-authored-by: echevrier <[email protected]>

* split full_derive into decode and sered_codec features.

(drop new Debug impls)

* cargo fmt

* rename serde_codec to serde_full

* Prior to v14 everything was Debug.

Are there any downsides? Unused debug impls will just get optimised away won't they?

* rust fmt

* Debug for the masses. Make it work for no_std

* check all combos of features

(if this plays well with ci then we should double up,
doing the same again but target wasm32)

* all wasm feature combos

Co-authored-by: echevrier <[email protected]>
Co-authored-by: echevrier <[email protected]>
  • Loading branch information
3 people authored Nov 9, 2022
1 parent d41046b commit 1ea3299
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 225 deletions.
47 changes: 19 additions & 28 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,20 @@ jobs:
- name: Add WASM Utilities
run: rustup target add wasm32-unknown-unknown --toolchain stable
## Check Stage
- name: Checking wasm32
- name: Checking wasm32 (v14)
uses: actions-rs/cargo@v1
uses: taiki-e/install-action@cargo-hack
with:
command: check
command: hack
toolchain: stable
args: --manifest-path ./frame-metadata/Cargo.toml --target wasm32-unknown-unknown --no-default-features
args: check --manifest-path ./frame-metadata/Cargo.toml --feature-powerset --no-dev-deps --features v14 --skip legacy,v8,v9,v10,v11,v12,v13 --depth 4 --target wasm32-unknown-unknown
- name: Checking wasm32 (all features)
uses: actions-rs/cargo@v1
uses: taiki-e/install-action@cargo-hack
with:
command: hack
toolchain: stable
args: check --manifest-path ./frame-metadata/Cargo.toml --feature-powerset --no-dev-deps --skip decode,serde_full --depth 4 --target wasm32-unknown-unknown

check-features:
name: Check Features
Expand All @@ -92,34 +100,17 @@ jobs:
fetch-depth: 5
submodules: recursive
## Check Stage
- name: Checking without any features
uses: actions-rs/cargo@v1
with:
command: check
toolchain: stable
args: --manifest-path ./frame-metadata/Cargo.toml --no-default-features
- name: Checking v12
- name: Checking v14 feature combinations (native)
uses: actions-rs/cargo@v1
uses: taiki-e/install-action@cargo-hack
with:
command: check
command: hack
toolchain: stable
args: --manifest-path ./frame-metadata/Cargo.toml --no-default-features --features v12
- name: Checking v13
args: check --manifest-path ./frame-metadata/Cargo.toml --feature-powerset --no-dev-deps --features v14 --skip legacy,v8,v9,v10,v11,v12,v13 --depth 4
- name: Checking feature combinations excluding decode/serde_full (native)
uses: actions-rs/cargo@v1
uses: taiki-e/install-action@cargo-hack
with:
command: check
command: hack
toolchain: stable
args: --manifest-path ./frame-metadata/Cargo.toml --no-default-features --features v13
- name: Checking v14
uses: actions-rs/cargo@v1
with:
command: check
toolchain: stable
args: --manifest-path ./frame-metadata/Cargo.toml --no-default-features --features v14
- name: Checking all versions
uses: actions-rs/cargo@v1
with:
command: check
toolchain: stable
args: --manifest-path ./frame-metadata/Cargo.toml --no-default-features --features v12,v13,v14

args: check --manifest-path ./frame-metadata/Cargo.toml --feature-powerset --no-dev-deps --skip decode,serde_full --depth 4
18 changes: 14 additions & 4 deletions frame-metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
cfg-if = "1.0.0"
scale-info = { version = "2.0.0", default-features = false, optional = true, features = ["derive"] }
serde = { version = "1.0.101", optional = true, features = ["derive"] }
serde = { version = "1.0.101", default-features = false, optional = true, features = ["derive"] }

[features]
default = ["std", "v14"]
Expand All @@ -28,10 +28,20 @@ v12 = []
v13 = []
legacy = ["v13", "v12", "v11", "v10", "v9", "v8"]
v14 = ["scale-info"]

# Serde support without relying on std features
serde_full = [
"scale-info/serde",
"serde",
"serde/alloc",
]
# Scale decode support without relying on std features
decode = ["scale-info/decode"]

std = [
"decode",
"serde_full",
"codec/std",
"scale-info/std",
"scale-info/serde",
"scale-info/decode",
"serde",
"serde/std",
]
49 changes: 33 additions & 16 deletions frame-metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,29 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]
#[cfg(all(
any(feature = "decode", feature = "serde_full"),
any(
feature = "v13",
feature = "v12",
feature = "v11",
feature = "v10",
feature = "v9",
feature = "v8",
feature = "legacy"
),
not(feature = "std")
))]
compile_error!("decode and serde_full features prior to v14 require std");

#[cfg(feature = "serde_full")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "decode")]
use codec::{Decode, Error, Input};

cfg_if::cfg_if! {
if #[cfg(feature = "std")] {
use codec::{Decode, Error, Input};
use serde::{
Deserialize,
Serialize,
};
} else {
if #[cfg(not(feature = "std"))] {
extern crate alloc;
use alloc::vec::Vec;
}
Expand Down Expand Up @@ -82,8 +96,9 @@ pub mod v14;
pub use self::v14::*;

/// Metadata prefixed by a u32 for reserved usage
#[derive(Eq, Encode, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Eq, Encode, PartialEq, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
pub struct RuntimeMetadataPrefixed(pub u32, pub RuntimeMetadata);

impl Into<Vec<u8>> for RuntimeMetadataPrefixed {
Expand All @@ -95,8 +110,9 @@ impl Into<Vec<u8>> for RuntimeMetadataPrefixed {
/// The metadata of a runtime.
/// The version ID encoded/decoded through
/// the enum nature of `RuntimeMetadata`.
#[derive(Eq, Encode, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Eq, Encode, PartialEq, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
pub enum RuntimeMetadata {
/// Unused; enum filler.
V0(RuntimeMetadataDeprecated),
Expand Down Expand Up @@ -182,13 +198,14 @@ impl RuntimeMetadata {
}

/// Stores the encoded `RuntimeMetadata` as raw bytes.
#[derive(Encode, Eq, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Deserialize, Debug))]
#[derive(Encode, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
pub struct OpaqueMetadata(pub Vec<u8>);

/// Enum that should fail.
#[derive(Eq, PartialEq)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(Eq, PartialEq, Debug)]
#[cfg_attr(feature = "serde_full", derive(Serialize, Deserialize))]
pub enum RuntimeMetadataDeprecated {}

impl Encode for RuntimeMetadataDeprecated {
Expand All @@ -197,7 +214,7 @@ impl Encode for RuntimeMetadataDeprecated {

impl codec::EncodeLike for RuntimeMetadataDeprecated {}

#[cfg(feature = "std")]
#[cfg(feature = "decode")]
impl Decode for RuntimeMetadataDeprecated {
fn decode<I: Input>(_input: &mut I) -> Result<Self, Error> {
Err("Decoding is not supported".into())
Expand Down
52 changes: 26 additions & 26 deletions frame-metadata/src/v10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,25 @@ cfg_if::cfg_if! {
pub const META_RESERVED: u32 = 0x6174656d; // 'meta' warn endianness

/// All the metadata about a function.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub struct FunctionMetadata {
pub name: DecodeDifferentStr,
pub arguments: DecodeDifferentArray<FunctionArgumentMetadata>,
pub documentation: DecodeDifferentArray<&'static str, StringBuf>,
}

/// All the metadata about a function argument.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub struct FunctionArgumentMetadata {
pub name: DecodeDifferentStr,
pub ty: DecodeDifferentStr,
}

/// All the metadata about an outer event.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub struct OuterEventMetadata {
pub name: DecodeDifferentStr,
pub events: DecodeDifferentArray<
Expand All @@ -71,17 +71,17 @@ pub struct OuterEventMetadata {
}

/// All the metadata about an event.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub struct EventMetadata {
pub name: DecodeDifferentStr,
pub arguments: DecodeDifferentArray<&'static str, StringBuf>,
pub documentation: DecodeDifferentArray<&'static str, StringBuf>,
}

/// All the metadata about one storage entry.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub struct StorageEntryMetadata {
pub name: DecodeDifferentStr,
pub modifier: StorageEntryModifier,
Expand All @@ -91,8 +91,8 @@ pub struct StorageEntryMetadata {
}

/// All the metadata about one module constant.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub struct ModuleConstantMetadata {
pub name: DecodeDifferentStr,
pub ty: DecodeDifferentStr,
Expand All @@ -101,8 +101,8 @@ pub struct ModuleConstantMetadata {
}

/// All the metadata about a module error.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub struct ErrorMetadata {
pub name: DecodeDifferentStr,
pub documentation: DecodeDifferentArray<&'static str, StringBuf>,
Expand Down Expand Up @@ -166,8 +166,8 @@ impl core::fmt::Debug for DefaultByteGetter {
}

/// Hasher used by storage maps
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub enum StorageHasher {
Blake2_128,
Blake2_256,
Expand All @@ -178,8 +178,8 @@ pub enum StorageHasher {
}

/// A storage entry type.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub enum StorageEntryType {
Plain(DecodeDifferentStr),
Map {
Expand All @@ -202,32 +202,32 @@ pub enum StorageEntryType {
///
/// `Optional` means you should expect an `Option<T>`, with `None` returned if the key is not present.
/// `Default` means you should expect a `T` with the default value of default if the key is not present.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub enum StorageEntryModifier {
Optional,
Default,
}

/// All metadata of the storage.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub struct StorageMetadata {
/// The common prefix used by all storage entries.
pub prefix: DecodeDifferent<&'static str, StringBuf>,
pub entries: DecodeDifferent<&'static [StorageEntryMetadata], Vec<StorageEntryMetadata>>,
}

/// The metadata of a runtime.
#[derive(Eq, Encode, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Eq, Encode, PartialEq, Debug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub struct RuntimeMetadataV10 {
pub modules: DecodeDifferentArray<ModuleMetadata>,
}

/// All metadata about an runtime module.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub struct ModuleMetadata {
pub name: DecodeDifferentStr,
pub storage: Option<DecodeDifferent<FnEncode<StorageMetadata>, StorageMetadata>>,
Expand Down
Loading

0 comments on commit 1ea3299

Please sign in to comment.