diff --git a/.travis.yml b/.travis.yml index 53e552f6d..a79ed71d8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -55,6 +55,8 @@ script: - cd parity-util-mem/ && cargo test --features=jemalloc-global && cd .. - cd parity-util-mem/ && cargo test --features=mimalloc-global && cd .. - cd parity-util-mem/ && cargo test --no-default-features --features=dlmalloc-global && cd .. + - cd primitive-types/ && cargo test --all-features && cd .. + - cd primitive-types/ && cargo test --no-default-features --features=serde_no_std && cd .. - cd rlp/ && cargo test --no-default-features && cargo check --benches && cd .. - cd triehash/ && cargo check --benches && cd .. - cd kvdb-web/ && wasm-pack test --headless --firefox && cd .. diff --git a/primitive-types/CHANGELOG.md b/primitive-types/CHANGELOG.md index 8f5d60af2..018d16eda 100644 --- a/primitive-types/CHANGELOG.md +++ b/primitive-types/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog]. [Keep a Changelog]: http://keepachangelog.com/en/1.0.0/ ## [Unreleased] +- Added `no_std` support for `serde` feature. [#385](https://github.com/paritytech/parity-common/pull/385) ## [0.7.1] - 2020-04-27 - Added `arbitrary` feature. [#378](https://github.com/paritytech/parity-common/pull/378) diff --git a/primitive-types/Cargo.toml b/primitive-types/Cargo.toml index b64d2b35b..67622a480 100644 --- a/primitive-types/Cargo.toml +++ b/primitive-types/Cargo.toml @@ -19,7 +19,8 @@ default = ["std"] std = ["uint/std", "fixed-hash/std", "impl-codec/std"] byteorder = ["fixed-hash/byteorder"] rustc-hex = ["fixed-hash/rustc-hex"] -serde = ["std", "impl-serde"] +serde = ["std", "impl-serde", "impl-serde/std"] +serde_no_std = ["impl-serde"] codec = ["impl-codec"] rlp = ["impl-rlp"] arbitrary = ["fixed-hash/arbitrary", "uint/arbitrary"] diff --git a/primitive-types/impls/serde/Cargo.toml b/primitive-types/impls/serde/Cargo.toml index 78122b1ff..b89051f75 100644 --- a/primitive-types/impls/serde/Cargo.toml +++ b/primitive-types/impls/serde/Cargo.toml @@ -7,8 +7,12 @@ license = "MIT OR Apache-2.0" homepage = "https://github.com/paritytech/parity-common" description = "Serde serialization support for uint and fixed hash." +[features] +default = ["std"] +std = ["serde/std"] + [dependencies] -serde = "1.0.101" +serde = { version = "1.0.101", default-features = false, features = ["alloc"] } [dev-dependencies] criterion = "0.3.0" diff --git a/primitive-types/impls/serde/src/lib.rs b/primitive-types/impls/serde/src/lib.rs index 500a60cc4..63fe535cb 100644 --- a/primitive-types/impls/serde/src/lib.rs +++ b/primitive-types/impls/serde/src/lib.rs @@ -8,6 +8,14 @@ //! Serde serialization support for uint and fixed hash. +#![no_std] + +#[macro_use] +extern crate alloc; + +#[cfg(feature = "std")] +extern crate std; + #[doc(hidden)] pub use serde; diff --git a/primitive-types/impls/serde/src/serialize.rs b/primitive-types/impls/serde/src/serialize.rs index 542ac0dc8..90e42e2a6 100644 --- a/primitive-types/impls/serde/src/serialize.rs +++ b/primitive-types/impls/serde/src/serialize.rs @@ -6,8 +6,11 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use alloc::string::String; +use alloc::vec::Vec; +use core::fmt; +use core::result::Result; use serde::{de, Deserializer, Serializer}; -use std::fmt; static CHARS: &[u8] = b"0123456789abcdef"; @@ -58,7 +61,7 @@ fn to_hex_raw<'a>(v: &'a mut [u8], bytes: &[u8], skip_leading_zero: bool) -> &'a } // SAFETY: all characters come either from CHARS or "0x", therefore valid UTF8 - unsafe { std::str::from_utf8_unchecked(&v[0..idx]) } + unsafe { core::str::from_utf8_unchecked(&v[0..idx]) } } /// Decoding bytes from hex string error. @@ -75,6 +78,7 @@ pub enum FromHexError { }, } +#[cfg(feature = "std")] impl std::error::Error for FromHexError {} impl fmt::Display for FromHexError {