Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add no_std support #42

Merged
merged 9 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,9 @@ jobs:
command: check
toolchain: stable
args: --manifest-path ./frame-metadata/Cargo.toml --no-default-features --features v12,v13,v14

- name: Checking fully featured wasm
uses: actions-rs/cargo@v1
with:
command: check
toolchain: stable
args: --manifest-path ./frame-metadata/Cargo.toml --no-default-features --features decode,serde_full,v14
Copy link
Contributor

@jsdw jsdw Sep 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also worth adding a specific check for just serde_full and just decode features being enabled? (it's so easy to miss compile errors that don't occur when both are enabled but do occur when just one is)

Copy link
Member

@niklasad1 niklasad1 Sep 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that we try to use cargo hack check --each-feature here if it's possible instead doing this manually it is so easy to forget some combination of feature flags.

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