Skip to content

Commit 3401582

Browse files
serde feature added (#745)
* serde feature added Support for serde derivations in no_std. Part of: paritytech/substrate#12994 * fixes * CI tests for serde, CHANGELOG updated * version changed to 0.1.7 * fix for ci command * Update bounded-collections/Cargo.toml Co-authored-by: Bastian Köcher <[email protected]> --------- Co-authored-by: Bastian Köcher <[email protected]>
1 parent 4bcc5b2 commit 3401582

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ jobs:
104104
command: test
105105
args: -p bounded-collections --no-default-features
106106

107+
- name: Test bounded-collections no_std,serde
108+
uses: actions-rs/cargo@v1
109+
with:
110+
command: test
111+
args: -p bounded-collections --no-default-features --features=serde
112+
107113
- name: Test bounded-collections all-features
108114
uses: actions-rs/cargo@v1
109115
with:

bounded-collections/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ The format is based on [Keep a Changelog].
44

55
[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/
66

7+
## [0.1.7] - 2023-05-05
8+
- Added `serde` feature, which can be enabled for no `std` deployments.
9+
710
## [0.1.6] - 2023-04-27
811
- Added `Clone` and `Default` derive to the `impl_const_get!` macro and thereby all `Const*` types.
912
- Fixed `Debug` impl for `impl_const_get!` and all `Const*` types to also print the value and not just the type name.

bounded-collections/Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bounded-collections"
3-
version = "0.1.6"
3+
version = "0.1.7"
44
authors = ["Parity Technologies <[email protected]>"]
55
license = "MIT OR Apache-2.0"
66
homepage = "https://github.com/paritytech/parity-common"
@@ -9,7 +9,7 @@ edition = "2021"
99
rust-version = "1.60.0"
1010

1111
[dependencies]
12-
serde = { version = "1.0.101", default-features = false, optional = true }
12+
serde = { version = "1.0.101", default-features = false, optional = true, features=["alloc", "derive"] }
1313
codec = { version = "3.3.0", default-features = false, features = ["max-encoded-len"], package = "parity-scale-codec" }
1414
scale-info = { version = ">=1.0, <3", features = ["derive"], default-features = false }
1515
log = { version = "0.4.17", default-features = false }
@@ -23,6 +23,5 @@ std = [
2323
"log/std",
2424
"codec/std",
2525
"scale-info/std",
26-
"serde",
27-
"serde/derive",
26+
"serde/std",
2827
]

bounded-collections/src/bounded_vec.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use core::{
2727
ops::{Deref, Index, IndexMut, RangeBounds},
2828
slice::SliceIndex,
2929
};
30-
#[cfg(feature = "std")]
30+
#[cfg(feature = "serde")]
3131
use serde::{
3232
de::{Error, SeqAccess, Visitor},
3333
Deserialize, Deserializer, Serialize,
@@ -40,18 +40,18 @@ use serde::{
4040
///
4141
/// As the name suggests, the length of the queue is always bounded. All internal operations ensure
4242
/// this bound is respected.
43-
#[cfg_attr(feature = "std", derive(Serialize), serde(transparent))]
43+
#[cfg_attr(feature = "serde", derive(Serialize), serde(transparent))]
4444
#[derive(Encode, scale_info::TypeInfo)]
4545
#[scale_info(skip_type_params(S))]
46-
pub struct BoundedVec<T, S>(pub(super) Vec<T>, #[cfg_attr(feature = "std", serde(skip_serializing))] PhantomData<S>);
46+
pub struct BoundedVec<T, S>(pub(super) Vec<T>, #[cfg_attr(feature = "serde", serde(skip_serializing))] PhantomData<S>);
4747

4848
/// Create an object through truncation.
4949
pub trait TruncateFrom<T> {
5050
/// Create an object through truncation.
5151
fn truncate_from(unbound: T) -> Self;
5252
}
5353

54-
#[cfg(feature = "std")]
54+
#[cfg(feature = "serde")]
5555
impl<'de, T, S: Get<u32>> Deserialize<'de> for BoundedVec<T, S>
5656
where
5757
T: Deserialize<'de>,
@@ -68,7 +68,7 @@ where
6868
{
6969
type Value = Vec<T>;
7070

71-
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
71+
fn expecting(&self, formatter: &mut alloc::fmt::Formatter) -> alloc::fmt::Result {
7272
formatter.write_str("a sequence")
7373
}
7474

bounded-collections/src/weak_bounded_vec.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use core::{
2727
ops::{Deref, Index, IndexMut},
2828
slice::SliceIndex,
2929
};
30-
#[cfg(feature = "std")]
30+
#[cfg(feature = "serde")]
3131
use serde::{
3232
de::{Error, SeqAccess, Visitor},
3333
Deserialize, Deserializer, Serialize,
@@ -40,15 +40,15 @@ use serde::{
4040
///
4141
/// The length of the vec is not strictly bounded. Decoding a vec with more element that the bound
4242
/// is accepted, and some method allow to bypass the restriction with warnings.
43-
#[cfg_attr(feature = "std", derive(Serialize), serde(transparent))]
43+
#[cfg_attr(feature = "serde", derive(Serialize), serde(transparent))]
4444
#[derive(Encode, scale_info::TypeInfo)]
4545
#[scale_info(skip_type_params(S))]
4646
pub struct WeakBoundedVec<T, S>(
4747
pub(super) Vec<T>,
48-
#[cfg_attr(feature = "std", serde(skip_serializing))] PhantomData<S>,
48+
#[cfg_attr(feature = "serde", serde(skip_serializing))] PhantomData<S>,
4949
);
5050

51-
#[cfg(feature = "std")]
51+
#[cfg(feature = "serde")]
5252
impl<'de, T, S: Get<u32>> Deserialize<'de> for WeakBoundedVec<T, S>
5353
where
5454
T: Deserialize<'de>,
@@ -65,7 +65,7 @@ where
6565
{
6666
type Value = Vec<T>;
6767

68-
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
68+
fn expecting(&self, formatter: &mut alloc::fmt::Formatter) -> alloc::fmt::Result {
6969
formatter.write_str("a sequence")
7070
}
7171

0 commit comments

Comments
 (0)