Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

primitive-types: add no_std support for serde feature #385

Merged
merged 3 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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