Skip to content

Commit 371f17f

Browse files
authored
primitive-types: add no_std support for serde feature (#385)
* primitive-types: add no_std support for serde feature This adds no_std support to primitive-types with serde. Due to rust-lang/cargo#3494, a separate new feature `serde_no_std` is created. * primitive-types: update changelog * travis: add tests for primitive-types
1 parent 990d45d commit 371f17f

File tree

6 files changed

+24
-4
lines changed

6 files changed

+24
-4
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ script:
5555
- cd parity-util-mem/ && cargo test --features=jemalloc-global && cd ..
5656
- cd parity-util-mem/ && cargo test --features=mimalloc-global && cd ..
5757
- cd parity-util-mem/ && cargo test --no-default-features --features=dlmalloc-global && cd ..
58+
- cd primitive-types/ && cargo test --all-features && cd ..
59+
- cd primitive-types/ && cargo test --no-default-features --features=serde_no_std && cd ..
5860
- cd rlp/ && cargo test --no-default-features && cargo check --benches && cd ..
5961
- cd triehash/ && cargo check --benches && cd ..
6062
- cd kvdb-web/ && wasm-pack test --headless --firefox && cd ..

primitive-types/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog].
55
[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/
66

77
## [Unreleased]
8+
- Added `no_std` support for `serde` feature. [#385](https://github.com/paritytech/parity-common/pull/385)
89

910
## [0.7.1] - 2020-04-27
1011
- Added `arbitrary` feature. [#378](https://github.com/paritytech/parity-common/pull/378)

primitive-types/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ default = ["std"]
1919
std = ["uint/std", "fixed-hash/std", "impl-codec/std"]
2020
byteorder = ["fixed-hash/byteorder"]
2121
rustc-hex = ["fixed-hash/rustc-hex"]
22-
serde = ["std", "impl-serde"]
22+
serde = ["std", "impl-serde", "impl-serde/std"]
23+
serde_no_std = ["impl-serde"]
2324
codec = ["impl-codec"]
2425
rlp = ["impl-rlp"]
2526
arbitrary = ["fixed-hash/arbitrary", "uint/arbitrary"]

primitive-types/impls/serde/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ license = "MIT OR Apache-2.0"
77
homepage = "https://github.com/paritytech/parity-common"
88
description = "Serde serialization support for uint and fixed hash."
99

10+
[features]
11+
default = ["std"]
12+
std = ["serde/std"]
13+
1014
[dependencies]
11-
serde = "1.0.101"
15+
serde = { version = "1.0.101", default-features = false, features = ["alloc"] }
1216

1317
[dev-dependencies]
1418
criterion = "0.3.0"

primitive-types/impls/serde/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88

99
//! Serde serialization support for uint and fixed hash.
1010
11+
#![no_std]
12+
13+
#[macro_use]
14+
extern crate alloc;
15+
16+
#[cfg(feature = "std")]
17+
extern crate std;
18+
1119
#[doc(hidden)]
1220
pub use serde;
1321

primitive-types/impls/serde/src/serialize.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9+
use alloc::string::String;
10+
use alloc::vec::Vec;
11+
use core::fmt;
12+
use core::result::Result;
913
use serde::{de, Deserializer, Serializer};
10-
use std::fmt;
1114

1215
static CHARS: &[u8] = b"0123456789abcdef";
1316

@@ -58,7 +61,7 @@ fn to_hex_raw<'a>(v: &'a mut [u8], bytes: &[u8], skip_leading_zero: bool) -> &'a
5861
}
5962

6063
// SAFETY: all characters come either from CHARS or "0x", therefore valid UTF8
61-
unsafe { std::str::from_utf8_unchecked(&v[0..idx]) }
64+
unsafe { core::str::from_utf8_unchecked(&v[0..idx]) }
6265
}
6366

6467
/// Decoding bytes from hex string error.
@@ -75,6 +78,7 @@ pub enum FromHexError {
7578
},
7679
}
7780

81+
#[cfg(feature = "std")]
7882
impl std::error::Error for FromHexError {}
7983

8084
impl fmt::Display for FromHexError {

0 commit comments

Comments
 (0)