Skip to content

Commit

Permalink
primitive-types: add no_std support for serde feature (#385)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
xu-cheng authored May 4, 2020
1 parent 990d45d commit 371f17f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ..
Expand Down
1 change: 1 addition & 0 deletions primitive-types/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion primitive-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
6 changes: 5 additions & 1 deletion primitive-types/impls/serde/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions primitive-types/impls/serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 6 additions & 2 deletions primitive-types/impls/serde/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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.
Expand All @@ -75,6 +78,7 @@ pub enum FromHexError {
},
}

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

impl fmt::Display for FromHexError {
Expand Down

0 comments on commit 371f17f

Please sign in to comment.