Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit 0b73a07

Browse files
committed
f expose hex for no-std
1 parent b80cd7a commit 0b73a07

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ path = "src/lib.rs"
1717
[features]
1818
default = [ "std" ]
1919
std = []
20+
# The no-std feature enables core2 and alloc.
21+
# You can still just disable std by disabling default features, without enabling these two.
2022
no-std = ["core2/alloc"]
2123
serde-std = ["serde/std"]
2224
unstable = [] # for benchmarking

src/hex.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
//! # Hex encoding and decoding
1616
//!
1717
18+
#[cfg(feature = "no-std")]
19+
use alloc::{format, string::String, vec::Vec};
20+
1821
use core::{fmt, str};
1922
use Hash;
2023

@@ -40,7 +43,7 @@ impl fmt::Display for Error {
4043
}
4144

4245
/// Trait for objects that can be serialized as hex strings
43-
#[cfg(any(test, feature = "std"))]
46+
#[cfg(any(test, feature = "std", feature = "no-std"))]
4447
pub trait ToHex {
4548
/// Hex representation of the object
4649
fn to_hex(&self) -> String;
@@ -60,7 +63,7 @@ pub trait FromHex: Sized {
6063
}
6164
}
6265

63-
#[cfg(any(test, feature = "std"))]
66+
#[cfg(any(test, feature = "std", feature = "no-std"))]
6467
impl<T: fmt::LowerHex> ToHex for T {
6568
/// Outputs the hash in hexadecimal form
6669
fn to_hex(&self) -> String {
@@ -174,7 +177,7 @@ pub fn format_hex_reverse(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
174177
Ok(())
175178
}
176179

177-
#[cfg(any(test, feature = "std"))]
180+
#[cfg(any(test, feature = "std", feature = "no-std"))]
178181
impl ToHex for [u8] {
179182
fn to_hex(&self) -> String {
180183
use core::fmt::Write;
@@ -186,7 +189,7 @@ impl ToHex for [u8] {
186189
}
187190
}
188191

189-
#[cfg(any(test, feature = "std"))]
192+
#[cfg(any(test, feature = "std", feature = "no-std"))]
190193
impl FromHex for Vec<u8> {
191194
fn from_byte_iter<I>(iter: I) -> Result<Self, Error>
192195
where I: Iterator<Item=Result<u8, Error>> +

src/impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#[cfg(feature = "std")]
2020
use std::{error, io};
2121

22-
#[cfg(feature = "no-std")]
22+
#[cfg(not(feature = "std"))]
2323
use core2::{error, io};
2424

2525
use {hex, sha1, sha256, sha512, ripemd160, siphash24};

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
#[cfg(any(test, feature="std"))] extern crate core;
4141
#[cfg(feature="no-std")] extern crate core2;
42+
#[cfg(feature="no-std")] extern crate alloc;
4243
#[cfg(feature="serde")] pub extern crate serde;
4344
#[cfg(all(test,feature="serde"))] extern crate serde_test;
4445

@@ -54,6 +55,7 @@ pub mod _export {
5455

5556
#[macro_use] mod util;
5657
#[macro_use] pub mod serde_macros;
58+
#[cfg(any(feature = "std", feature = "no-std"))]
5759
mod impls;
5860
pub mod error;
5961
pub mod hex;

0 commit comments

Comments
 (0)