Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 6507a79

Browse files
haerdibdavxybkchr
authored
Add serde feature flag to primitives (#13027)
* add serde_full feature flag add serde_full to sp_runtime add space to toml add serde_full to application-crypto add serde_full to arithmetic fix arithmetic add serde full to beefy add serde full to consensus add serde_full to core add serdefull to finality grandpa add serde_full to several primitives crates make rpc no_std compatible add scale info to runtime make serializer no_std compatible add serde full to storage add full serde to version add serde full to weights add all serde_full features add . to comment add missing impl-serde fix no-std build fix build add full_crypto to serde_full serde_full also implements crypto full_serde does not work with full_crytpo. needs std no no_std serde impl possible also for crypto std is necessary no serde full for application crypto fix arithmetic fix tomls fix some things impl fmt for Signature add serialize to Public add impl_maybe_marker_serde_full fix sp-application-crypto toml add serde feature flag fix clippy fix toml grandpa fix grandpa rename if_std to if_serde keystore is not no_std compatible make keystore vrf no_std compatible fix nopos-elections fix rpc fix serializer fix test-primitives fix version add comment add serde full only import for format string remove all(serde_full and full_crypot) as serde_full enforces full_crypto make comment better readable even better comment clean up rpc toml clean up toml clean up serializer toml clean up storage toml fix std build update .lock fix sp-version move sp_std import test extern crate alloc replace sp_std with core add missing core sp_core: serde feature do not enforce full crypto application-crypto: serde feature do not enforce full crypto rename serde_full to serde add dep:serde and alloc to default feature add full_crypto and remove unnecessary debu/fmt impls for serde update comment remove obolsete change in display AccountId32 remove extra changes minimize diff revert keystore changes remove std from keystore remove full-crypto feature fix serde import fix comment fix feature = serde * rename serde_full to serde * move #[doc(hidden)] back * remove feature = full crypto require frm MultiSigner * reorder serde and scale_info import * fix bs58 missing alloc import in serde feature * add `from_string` to serde feature and add unimplemented * remove serde feature from fixed_point display * Remove serde/alloc Co-authored-by: Davide Galassi <davxy@datawok.net> * Update primitives/consensus/babe/Cargo.toml Co-authored-by: Bastian Köcher <git@kchr.de> * Update primitives/arithmetic/src/fixed_point.rs Co-authored-by: Bastian Köcher <git@kchr.de> * revert `from_string`fixed impl back to std only * remove duplicate runtime string impl * use sp_std::alloc * remove no_std compatible rpc * remove no_std compatibility from serializer * rename mpl_maybe_marker_serde to std_or_serde * update .lock * add sp-std to executor * fix sp-std import * fix sp_std::format import * use crate import * add serde feature * Update primitives/core/src/lib.rs --------- Co-authored-by: Davide Galassi <davxy@datawok.net> Co-authored-by: Bastian Köcher <git@kchr.de>
1 parent 43a130c commit 6507a79

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+406
-206
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

primitives/application-crypto/Cargo.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ targets = ["x86_64-unknown-linux-gnu"]
1818
sp-core = { version = "7.0.0", default-features = false, path = "../core" }
1919
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
2020
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
21-
serde = { version = "1.0.136", optional = true, features = ["derive"] }
21+
serde = { version = "1.0.136", default-features = false, optional = true, features = ["derive", "alloc"] }
2222
sp-std = { version = "5.0.0", default-features = false, path = "../std" }
2323
sp-io = { version = "7.0.0", default-features = false, path = "../io" }
2424

@@ -29,11 +29,18 @@ std = [
2929
"sp-core/std",
3030
"codec/std",
3131
"scale-info/std",
32-
"serde",
32+
"serde/std",
3333
"sp-std/std",
3434
"sp-io/std",
3535
]
3636

37+
# Serde support without relying on std features.
38+
serde = [
39+
"dep:serde",
40+
"sp-core/serde",
41+
"scale-info/serde",
42+
]
43+
3744
# This feature enables all crypto primitives for `no_std` builds like microcontrollers
3845
# or Intel SGX.
3946
# For the regular wasm runtime builds this should not be used.

primitives/application-crypto/src/lib.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,25 @@
2323
pub use sp_core::crypto::{key_types, CryptoTypeId, KeyTypeId};
2424
#[doc(hidden)]
2525
#[cfg(feature = "full_crypto")]
26-
pub use sp_core::crypto::{DeriveError, DeriveJunction, Pair, SecretStringError, Ss58Codec};
26+
pub use sp_core::crypto::{DeriveError, Pair, SecretStringError};
27+
#[cfg(any(feature = "full_crypto", feature = "serde"))]
28+
pub use sp_core::crypto::{DeriveJunction, Ss58Codec};
2729
#[doc(hidden)]
2830
pub use sp_core::{
2931
self,
3032
crypto::{ByteArray, CryptoType, Derive, IsWrappedBy, Public, UncheckedFrom, Wraps},
3133
RuntimeDebug,
3234
};
35+
#[doc(hidden)]
36+
#[cfg(all(not(feature = "std"), feature = "serde"))]
37+
pub use sp_std::alloc::{format, string::String};
3338

3439
#[doc(hidden)]
3540
pub use codec;
3641
#[doc(hidden)]
3742
pub use scale_info;
3843
#[doc(hidden)]
39-
#[cfg(feature = "std")]
44+
#[cfg(feature = "serde")]
4045
pub use serde;
4146
#[doc(hidden)]
4247
pub use sp_std::{ops::Deref, vec::Vec};
@@ -282,7 +287,7 @@ macro_rules! app_crypto_public_not_full_crypto {
282287
#[macro_export]
283288
macro_rules! app_crypto_public_common {
284289
($public:ty, $sig:ty, $key_type:expr, $crypto_type:expr) => {
285-
$crate::app_crypto_public_common_if_std!();
290+
$crate::app_crypto_public_common_if_serde!();
286291

287292
impl AsRef<[u8]> for Public {
288293
fn as_ref(&self) -> &[u8] {
@@ -323,11 +328,11 @@ macro_rules! app_crypto_public_common {
323328
};
324329
}
325330

326-
/// Implements traits for the public key type if `feature = "std"` is enabled.
327-
#[cfg(feature = "std")]
331+
/// Implements traits for the public key type if `feature = "serde"` is enabled.
332+
#[cfg(feature = "serde")]
328333
#[doc(hidden)]
329334
#[macro_export]
330-
macro_rules! app_crypto_public_common_if_std {
335+
macro_rules! app_crypto_public_common_if_serde {
331336
() => {
332337
impl $crate::Derive for Public {
333338
fn derive<Iter: Iterator<Item = $crate::DeriveJunction>>(
@@ -338,15 +343,15 @@ macro_rules! app_crypto_public_common_if_std {
338343
}
339344
}
340345

341-
impl std::fmt::Display for Public {
342-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
346+
impl core::fmt::Display for Public {
347+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
343348
use $crate::Ss58Codec;
344349
write!(f, "{}", self.0.to_ss58check())
345350
}
346351
}
347352

348353
impl $crate::serde::Serialize for Public {
349-
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
354+
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
350355
where
351356
S: $crate::serde::Serializer,
352357
{
@@ -356,22 +361,25 @@ macro_rules! app_crypto_public_common_if_std {
356361
}
357362

358363
impl<'de> $crate::serde::Deserialize<'de> for Public {
359-
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
364+
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
360365
where
361366
D: $crate::serde::Deserializer<'de>,
362367
{
363368
use $crate::Ss58Codec;
369+
#[cfg(all(not(feature = "std"), feature = "serde"))]
370+
use $crate::{format, String};
371+
364372
Public::from_ss58check(&String::deserialize(deserializer)?)
365373
.map_err(|e| $crate::serde::de::Error::custom(format!("{:?}", e)))
366374
}
367375
}
368376
};
369377
}
370378

371-
#[cfg(not(feature = "std"))]
379+
#[cfg(not(feature = "serde"))]
372380
#[doc(hidden)]
373381
#[macro_export]
374-
macro_rules! app_crypto_public_common_if_std {
382+
macro_rules! app_crypto_public_common_if_serde {
375383
() => {
376384
impl $crate::Derive for Public {}
377385
};

primitives/arithmetic/Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ codec = { package = "parity-scale-codec", version = "3.2.2", default-features =
2121
integer-sqrt = "0.1.2"
2222
num-traits = { version = "0.2.8", default-features = false }
2323
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
24-
serde = { version = "1.0.136", features = ["derive"], optional = true }
24+
serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"], optional = true }
2525
static_assertions = "1.1.0"
2626
sp-std = { version = "5.0.0", default-features = false, path = "../std" }
2727

@@ -37,9 +37,14 @@ std = [
3737
"codec/std",
3838
"num-traits/std",
3939
"scale-info/std",
40-
"serde",
40+
"serde/std",
4141
"sp-std/std",
4242
]
43+
# Serde support without relying on std features.
44+
serde = [
45+
"dep:serde",
46+
"scale-info/serde",
47+
]
4348

4449
[[bench]]
4550
name = "bench"

primitives/arithmetic/src/fixed_point.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ use sp_std::{
3232
prelude::*,
3333
};
3434

35-
#[cfg(feature = "std")]
35+
#[cfg(feature = "serde")]
3636
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
3737

38+
#[cfg(all(not(feature = "std"), feature = "serde"))]
39+
use sp_std::alloc::string::{String, ToString};
40+
3841
/// Integer types that can be used to interact with `FixedPointNumber` implementations.
3942
pub trait FixedPointOperand:
4043
Copy
@@ -928,14 +931,12 @@ macro_rules! implement_fixed {
928931
}
929932
}
930933

931-
#[cfg(feature = "std")]
932934
impl sp_std::fmt::Display for $name {
933935
fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
934936
write!(f, "{}", self.0)
935937
}
936938
}
937939

938-
#[cfg(feature = "std")]
939940
impl sp_std::str::FromStr for $name {
940941
type Err = &'static str;
941942

@@ -948,7 +949,7 @@ macro_rules! implement_fixed {
948949

949950
// Manual impl `Serialize` as serde_json does not support i128.
950951
// TODO: remove impl if issue https://github.com/serde-rs/json/issues/548 fixed.
951-
#[cfg(feature = "std")]
952+
#[cfg(feature = "serde")]
952953
impl Serialize for $name {
953954
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
954955
where
@@ -960,7 +961,7 @@ macro_rules! implement_fixed {
960961

961962
// Manual impl `Deserialize` as serde_json does not support i128.
962963
// TODO: remove impl if issue https://github.com/serde-rs/json/issues/548 fixed.
963-
#[cfg(feature = "std")]
964+
#[cfg(feature = "serde")]
964965
impl<'de> Deserialize<'de> for $name {
965966
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
966967
where

primitives/arithmetic/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ use traits::{BaseArithmetic, One, SaturatedConversion, Unsigned, Zero};
5353
use codec::{Decode, Encode, MaxEncodedLen};
5454
use scale_info::TypeInfo;
5555

56-
#[cfg(feature = "std")]
56+
#[cfg(feature = "serde")]
5757
use serde::{Deserialize, Serialize};
5858

5959
/// Arithmetic errors.
6060
#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
61-
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
61+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6262
pub enum ArithmeticError {
6363
/// Underflow.
6464
Underflow,

primitives/arithmetic/src/per_things.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
#[cfg(feature = "std")]
18+
#[cfg(feature = "serde")]
1919
use serde::{Deserialize, Serialize};
2020

2121
use crate::traits::{
@@ -556,7 +556,7 @@ macro_rules! implement_per_thing {
556556
/// A fixed point representation of a number in the range [0, 1].
557557
///
558558
#[doc = $title]
559-
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
559+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
560560
#[derive(Encode, Copy, Clone, PartialEq, Eq, codec::MaxEncodedLen, PartialOrd, Ord, scale_info::TypeInfo)]
561561
pub struct $name($type);
562562

primitives/consensus/babe/Cargo.toml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
1616
async-trait = { version = "0.1.57", optional = true }
1717
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false }
1818
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
19-
serde = { version = "1.0.136", features = ["derive"], optional = true }
19+
serde = { version = "1.0.136", default-features = false, features = ["derive", "alloc"], optional = true }
2020
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../api" }
2121
sp-application-crypto = { version = "7.0.0", default-features = false, path = "../../application-crypto" }
2222
sp-consensus = { version = "0.10.0-dev", optional = true, path = "../common" }
@@ -34,7 +34,7 @@ std = [
3434
"async-trait",
3535
"codec/std",
3636
"scale-info/std",
37-
"serde",
37+
"serde/std",
3838
"sp-api/std",
3939
"sp-application-crypto/std",
4040
"sp-consensus",
@@ -46,3 +46,13 @@ std = [
4646
"sp-std/std",
4747
"sp-timestamp",
4848
]
49+
50+
# Serde support without relying on std features.
51+
serde = [
52+
"dep:serde",
53+
"scale-info/serde",
54+
"sp-application-crypto/serde",
55+
"sp-consensus-slots/serde",
56+
"sp-core/serde",
57+
"sp-runtime/serde",
58+
]

primitives/consensus/babe/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub mod inherents;
2525

2626
use codec::{Decode, Encode, MaxEncodedLen};
2727
use scale_info::TypeInfo;
28-
#[cfg(feature = "std")]
28+
#[cfg(feature = "serde")]
2929
use serde::{Deserialize, Serialize};
3030
use sp_runtime::{traits::Header, ConsensusEngineId, RuntimeDebug};
3131
use sp_std::vec::Vec;
@@ -217,7 +217,7 @@ impl BabeConfiguration {
217217

218218
/// Types of allowed slots.
219219
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo)]
220-
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
220+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
221221
pub enum AllowedSlots {
222222
/// Only allow primary slots.
223223
PrimarySlots,
@@ -241,7 +241,7 @@ impl AllowedSlots {
241241

242242
/// Configuration data used by the BABE consensus engine that may change with epochs.
243243
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo)]
244-
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
244+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
245245
pub struct BabeEpochConfiguration {
246246
/// A constant value that is used in the threshold calculation formula.
247247
/// Expressed as a rational where the first member of the tuple is the

primitives/consensus/beefy/Cargo.toml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ targets = ["x86_64-unknown-linux-gnu"]
1313

1414
[dependencies]
1515
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
16-
serde = { version = "1.0.136", optional = true, features = ["derive"] }
1716
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
17+
serde = { version = "1.0.136", default-features = false, optional = true, features = ["derive", "alloc"] }
1818
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../api" }
1919
sp-application-crypto = { version = "7.0.0", default-features = false, path = "../../application-crypto" }
2020
sp-core = { version = "7.0.0", default-features = false, path = "../../core" }
@@ -34,7 +34,7 @@ default = ["std"]
3434
std = [
3535
"codec/std",
3636
"scale-info/std",
37-
"serde",
37+
"serde/std",
3838
"sp-api/std",
3939
"sp-application-crypto/std",
4040
"sp-core/std",
@@ -43,3 +43,12 @@ std = [
4343
"sp-runtime/std",
4444
"sp-std/std",
4545
]
46+
47+
# Serde support without relying on std features.
48+
serde = [
49+
"dep:serde",
50+
"scale-info/serde",
51+
"sp-application-crypto/serde",
52+
"sp-core/serde",
53+
"sp-runtime/serde",
54+
]

primitives/consensus/beefy/src/mmr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl MmrLeafVersion {
101101

102102
/// Details of a BEEFY authority set.
103103
#[derive(Debug, Default, PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen)]
104-
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
104+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
105105
pub struct BeefyAuthoritySet<MerkleRoot> {
106106
/// Id of the set.
107107
///

primitives/consensus/grandpa/Cargo.toml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ codec = { package = "parity-scale-codec", version = "3.2.2", default-features =
1818
grandpa = { package = "finality-grandpa", version = "0.16.2", default-features = false, features = ["derive-codec"] }
1919
log = { version = "0.4.17", default-features = false }
2020
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
21-
serde = { version = "1.0.136", features = ["derive"], optional = true }
21+
serde = { version = "1.0.136", features = ["derive", "alloc"], default-features = false, optional = true }
2222
sp-api = { version = "4.0.0-dev", default-features = false, path = "../../api" }
2323
sp-application-crypto = { version = "7.0.0", default-features = false, path = "../../application-crypto" }
2424
sp-core = { version = "7.0.0", default-features = false, path = "../../core" }
@@ -33,11 +33,20 @@ std = [
3333
"grandpa/std",
3434
"log/std",
3535
"scale-info/std",
36-
"serde",
36+
"serde/std",
3737
"sp-api/std",
3838
"sp-application-crypto/std",
3939
"sp-core/std",
4040
"sp-keystore",
4141
"sp-runtime/std",
4242
"sp-std/std",
4343
]
44+
45+
# Serde support without relying on std features.
46+
serde = [
47+
"dep:serde",
48+
"scale-info/serde",
49+
"sp-application-crypto/serde",
50+
"sp-core/serde",
51+
"sp-runtime/serde",
52+
]

0 commit comments

Comments
 (0)