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 1 commit
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
15 changes: 11 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,17 @@ v12 = []
v13 = []
legacy = ["v13", "v12", "v11", "v10", "v9", "v8"]
v14 = ["scale-info"]
std = [
"codec/std",
"scale-info/std",

full_derive = [
gilescope marked this conversation as resolved.
Show resolved Hide resolved
"scale-info/serde",
"scale-info/decode",
"serde",
"serde/alloc",
]

std = [
"full_derive",
"codec/std",
"scale-info/std",
"serde/std",
]
32 changes: 25 additions & 7 deletions frame-metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,33 @@

#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]
#[cfg(all(
feature = "full_derive",
any(
feature = "v13",
feature = "v12",
feature = "v11",
feature = "v10",
feature = "v9",
feature = "v8",
feature = "legacy"
),
not(feature = "std")
))]
compile_error!("full_derive for metadata prior to v14 requires std!");

cfg_if::cfg_if! {
if #[cfg(feature = "std")] {
if #[cfg(feature = "full_derive")] {
use codec::{Decode, Error, Input};
use serde::{
Deserialize,
Serialize,
};
} else {
}
}

cfg_if::cfg_if! {
if #[cfg(not(feature = "std"))] {
extern crate alloc;
use alloc::vec::Vec;
}
Expand Down Expand Up @@ -83,7 +101,7 @@ pub use self::v14::*;

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

impl Into<Vec<u8>> for RuntimeMetadataPrefixed {
Expand All @@ -96,7 +114,7 @@ impl Into<Vec<u8>> for RuntimeMetadataPrefixed {
/// The version ID encoded/decoded through
/// the enum nature of `RuntimeMetadata`.
#[derive(Eq, Encode, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "full_derive", derive(Decode, Serialize, Debug))]
pub enum RuntimeMetadata {
/// Unused; enum filler.
V0(RuntimeMetadataDeprecated),
Expand Down Expand Up @@ -183,12 +201,12 @@ impl RuntimeMetadata {

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

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

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

impl codec::EncodeLike for RuntimeMetadataDeprecated {}

#[cfg(feature = "std")]
#[cfg(feature = "full_derive")]
impl Decode for RuntimeMetadataDeprecated {
fn decode<I: Input>(_input: &mut I) -> Result<Self, Error> {
Err("Decoding is not supported".into())
Expand Down
49 changes: 26 additions & 23 deletions frame-metadata/src/v14.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// limitations under the License.

cfg_if::cfg_if! {
if #[cfg(feature = "std")] {
if #[cfg(feature = "full_derive")] {
use codec::Decode;
use serde::Serialize;
}
Expand Down Expand Up @@ -44,7 +44,7 @@ impl From<RuntimeMetadataLastVersion> for super::RuntimeMetadataPrefixed {

/// The metadata of a runtime.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "full_derive", derive(Decode, Serialize, Debug))]
pub struct RuntimeMetadataV14 {
/// Type registry containing all types used in the metadata.
pub types: PortableRegistry,
Expand Down Expand Up @@ -78,9 +78,9 @@ impl RuntimeMetadataV14 {

/// Metadata of the extrinsic used by the runtime.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "full_derive", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
feature = "full_derive",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct ExtrinsicMetadata<T: Form = MetaForm> {
Expand All @@ -106,9 +106,9 @@ impl IntoPortable for ExtrinsicMetadata {

/// Metadata of an extrinsic's signed extension.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "full_derive", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
feature = "full_derive",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct SignedExtensionMetadata<T: Form = MetaForm> {
Expand All @@ -134,9 +134,9 @@ impl IntoPortable for SignedExtensionMetadata {

/// All metadata about an runtime pallet.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "full_derive", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
feature = "full_derive",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct PalletMetadata<T: Form = MetaForm> {
Expand Down Expand Up @@ -175,9 +175,9 @@ impl IntoPortable for PalletMetadata {

/// All metadata of the pallet's storage.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "full_derive", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
feature = "full_derive",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct PalletStorageMetadata<T: Form = MetaForm> {
Expand All @@ -200,9 +200,9 @@ impl IntoPortable for PalletStorageMetadata {

/// Metadata about one storage entry.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "full_derive", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
feature = "full_derive",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct StorageEntryMetadata<T: Form = MetaForm> {
Expand Down Expand Up @@ -238,7 +238,7 @@ impl IntoPortable for StorageEntryMetadata {
/// `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))]
#[cfg_attr(feature = "full_derive", derive(Decode, Serialize, Debug))]
pub enum StorageEntryModifier {
/// The storage entry returns an `Option<T>`, with `None` if the key is not present.
Optional,
Expand All @@ -248,7 +248,7 @@ pub enum StorageEntryModifier {

/// Hasher used by storage maps
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "full_derive", derive(Decode, Serialize, Debug))]
pub enum StorageHasher {
/// 128-bit Blake2 hash.
Blake2_128,
Expand All @@ -268,9 +268,9 @@ pub enum StorageHasher {

/// A type of storage value.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "full_derive", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
feature = "full_derive",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub enum StorageEntryType<T: Form = MetaForm> {
Expand Down Expand Up @@ -308,9 +308,9 @@ impl IntoPortable for StorageEntryType {

/// Metadata for all calls in a pallet
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "full_derive", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
feature = "full_derive",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct PalletCallMetadata<T: Form = MetaForm> {
Expand All @@ -336,7 +336,7 @@ impl From<MetaType> for PalletCallMetadata {

/// Metadata about the pallet Event type.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "full_derive", derive(Decode, Serialize, Debug))]
pub struct PalletEventMetadata<T: Form = MetaForm> {
/// The Event type.
pub ty: T::Type,
Expand All @@ -360,9 +360,9 @@ impl From<MetaType> for PalletEventMetadata {

/// Metadata about one pallet constant.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "full_derive", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "std",
feature = "full_derive",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct PalletConstantMetadata<T: Form = MetaForm> {
Expand Down Expand Up @@ -391,8 +391,11 @@ impl IntoPortable for PalletConstantMetadata {

/// Metadata about a pallet error.
#[derive(Clone, PartialEq, Eq, Encode)]
#[cfg_attr(feature = "std", derive(Decode, Serialize, Debug))]
#[cfg_attr(feature = "std", serde(bound(serialize = "T::Type: Serialize")))]
#[cfg_attr(feature = "full_derive", derive(Decode, Serialize, Debug))]
#[cfg_attr(
feature = "full_derive",
serde(bound(serialize = "T::Type: Serialize"))
)]
pub struct PalletErrorMetadata<T: Form = MetaForm> {
/// The error type information.
pub ty: T::Type,
Expand Down