Skip to content

Commit 17a7981

Browse files
Merge remote-tracking branch 'haerdib/add-serde-feature-to-primitives' into gc-dev
2 parents 58be496 + ee4ce97 commit 17a7981

Some content is hidden

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

53 files changed

+455
-210
lines changed

Cargo.lock

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

primitives/application-crypto/Cargo.toml

+9-2
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

+19-12
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222

2323
pub use sp_core::crypto::{key_types, CryptoTypeId, KeyTypeId};
2424
#[doc(hidden)]
25+
#[cfg(any(feature = "full_crypto", feature = "serde"))]
26+
pub use sp_core::crypto::{DeriveJunction, Ss58Codec};
2527
#[cfg(feature = "full_crypto")]
26-
pub use sp_core::crypto::{DeriveError, DeriveJunction, Pair, SecretStringError, Ss58Codec};
28+
pub use sp_core::crypto::{DeriveError, Pair, SecretStringError};
2729
#[doc(hidden)]
2830
pub use sp_core::{
2931
self,
@@ -36,7 +38,7 @@ pub use codec;
3638
#[doc(hidden)]
3739
pub use scale_info;
3840
#[doc(hidden)]
39-
#[cfg(feature = "std")]
41+
#[cfg(feature = "serde")]
4042
pub use serde;
4143
#[doc(hidden)]
4244
pub use sp_std::{ops::Deref, vec::Vec};
@@ -278,7 +280,7 @@ macro_rules! app_crypto_public_not_full_crypto {
278280
#[macro_export]
279281
macro_rules! app_crypto_public_common {
280282
($public:ty, $sig:ty, $key_type:expr, $crypto_type:expr) => {
281-
$crate::app_crypto_public_common_if_std!();
283+
$crate::app_crypto_public_common_if_serde!();
282284

283285
impl AsRef<[u8]> for Public {
284286
fn as_ref(&self) -> &[u8] {
@@ -312,11 +314,11 @@ macro_rules! app_crypto_public_common {
312314
};
313315
}
314316

315-
/// Implements traits for the public key type if `feature = "std"` is enabled.
316-
#[cfg(feature = "std")]
317+
/// Implements traits for the public key type if `feature = "serde"` is enabled.
318+
#[cfg(feature = "serde")]
317319
#[doc(hidden)]
318320
#[macro_export]
319-
macro_rules! app_crypto_public_common_if_std {
321+
macro_rules! app_crypto_public_common_if_serde {
320322
() => {
321323
impl $crate::Derive for Public {
322324
fn derive<Iter: Iterator<Item = $crate::DeriveJunction>>(
@@ -327,15 +329,15 @@ macro_rules! app_crypto_public_common_if_std {
327329
}
328330
}
329331

330-
impl std::fmt::Display for Public {
331-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
332+
impl core::fmt::Display for Public {
333+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
332334
use $crate::Ss58Codec;
333335
write!(f, "{}", self.0.to_ss58check())
334336
}
335337
}
336338

337339
impl $crate::serde::Serialize for Public {
338-
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
340+
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
339341
where
340342
S: $crate::serde::Serializer,
341343
{
@@ -345,22 +347,27 @@ macro_rules! app_crypto_public_common_if_std {
345347
}
346348

347349
impl<'de> $crate::serde::Deserialize<'de> for Public {
348-
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
350+
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
349351
where
350352
D: $crate::serde::Deserializer<'de>,
351353
{
354+
#[cfg(not(feature = "std"))]
355+
extern crate alloc;
356+
#[cfg(not(feature = "std"))]
357+
use alloc::{format, string::String};
352358
use $crate::Ss58Codec;
359+
353360
Public::from_ss58check(&String::deserialize(deserializer)?)
354361
.map_err(|e| $crate::serde::de::Error::custom(format!("{:?}", e)))
355362
}
356363
}
357364
};
358365
}
359366

360-
#[cfg(not(feature = "std"))]
367+
#[cfg(not(feature = "serde"))]
361368
#[doc(hidden)]
362369
#[macro_export]
363-
macro_rules! app_crypto_public_common_if_std {
370+
macro_rules! app_crypto_public_common_if_serde {
364371
() => {
365372
impl $crate::Derive for Public {}
366373
};

primitives/arithmetic/Cargo.toml

+8-2
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,15 @@ 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+
"serde/alloc",
48+
]
4349

4450
[[bench]]
4551
name = "bench"

primitives/arithmetic/src/fixed_point.rs

+8-5
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, string::ToString};
40+
3841
/// Integer types that can be used to interact with `FixedPointNumber` implementations.
3942
pub trait FixedPointOperand:
4043
Copy
@@ -928,14 +931,14 @@ macro_rules! implement_fixed {
928931
}
929932
}
930933

931-
#[cfg(feature = "std")]
934+
#[cfg(feature = "serde")]
932935
impl sp_std::fmt::Display for $name {
933936
fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result {
934937
write!(f, "{}", self.0)
935938
}
936939
}
937940

938-
#[cfg(feature = "std")]
941+
#[cfg(feature = "serde")]
939942
impl sp_std::str::FromStr for $name {
940943
type Err = &'static str;
941944

@@ -948,7 +951,7 @@ macro_rules! implement_fixed {
948951

949952
// Manual impl `Serialize` as serde_json does not support i128.
950953
// TODO: remove impl if issue https://github.com/serde-rs/json/issues/548 fixed.
951-
#[cfg(feature = "std")]
954+
#[cfg(feature = "serde")]
952955
impl Serialize for $name {
953956
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
954957
where
@@ -960,7 +963,7 @@ macro_rules! implement_fixed {
960963

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

primitives/arithmetic/src/lib.rs

+2-2
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

+2-2
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

+12-2
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", features = ["derive", "alloc"], default-features = false, 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

+3-3
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;
@@ -210,7 +210,7 @@ impl BabeConfiguration {
210210

211211
/// Types of allowed slots.
212212
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo)]
213-
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
213+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
214214
pub enum AllowedSlots {
215215
/// Only allow primary slots.
216216
PrimarySlots,
@@ -234,7 +234,7 @@ impl AllowedSlots {
234234

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

primitives/consensus/beefy/Cargo.toml

+11-2
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", optional = true, default-features = false, 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

+1-1
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

+11-2
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)