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

Commit 9dc6119

Browse files
committed
add no_std support
1 parent 8a61d12 commit 9dc6119

File tree

14 files changed

+51
-47
lines changed

14 files changed

+51
-47
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ name = "bitcoin_hashes"
1313
path = "src/lib.rs"
1414

1515
[features]
16-
default = []
16+
default = [ "std" ]
17+
std = []
1718
unstable = [] # for benchmarking
1819
fuzztarget = [] # used by other rust-bitcoin projects to make hashes almost-noops, DON'T USE THIS
1920

src/cmp.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ pub fn fixed_time_eq(a: &[u8], b: &[u8]) -> bool {
2020

2121
let mut r: u8 = 0;
2222
for i in 0..count {
23-
let mut rs = unsafe { ::std::ptr::read_volatile(&r) };
23+
let mut rs = unsafe { ::core::ptr::read_volatile(&r) };
2424
rs |= lhs[i] ^ rhs[i];
25-
unsafe { ::std::ptr::write_volatile(&mut r, rs); }
25+
unsafe { ::core::ptr::write_volatile(&mut r, rs); }
2626
}
2727
{
28-
let mut t = unsafe { ::std::ptr::read_volatile(&r) };
28+
let mut t = unsafe { ::core::ptr::read_volatile(&r) };
2929
t |= t >> 4;
30-
unsafe { ::std::ptr::write_volatile(&mut r, t); }
30+
unsafe { ::core::ptr::write_volatile(&mut r, t); }
3131
}
3232
{
33-
let mut t = unsafe { ::std::ptr::read_volatile(&r) };
33+
let mut t = unsafe { ::core::ptr::read_volatile(&r) };
3434
t |= t >> 2;
35-
unsafe { ::std::ptr::write_volatile(&mut r, t); }
35+
unsafe { ::core::ptr::write_volatile(&mut r, t); }
3636
}
3737
{
38-
let mut t = unsafe { ::std::ptr::read_volatile(&r) };
38+
let mut t = unsafe { ::core::ptr::read_volatile(&r) };
3939
t |= t >> 1;
40-
unsafe { ::std::ptr::write_volatile(&mut r, t); }
40+
unsafe { ::core::ptr::write_volatile(&mut r, t); }
4141
}
42-
unsafe { (::std::ptr::read_volatile(&r) & 1) == 0 }
42+
unsafe { (::core::ptr::read_volatile(&r) & 1) == 0 }
4343
}
4444

4545
#[test]

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! # Error Type
1616
//!
1717
18-
use std::fmt;
18+
use core::fmt;
1919

2020
/// Hex decoding error
2121
#[derive(Copy, Clone, PartialEq, Eq)]

src/hash160.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
//! # HASH160 (SHA256 then RIPEMD160)
2121
22-
use std::str;
22+
use core::str;
2323

2424
use sha256;
2525
use ripemd160;
@@ -109,12 +109,12 @@ mod tests {
109109
0xb8, 0x5d, 0xf1, 0x69, 0xc1, 0x8a, 0x3c, 0x69, 0x7f,
110110
0xbb, 0x2d, 0xc4, 0xec, 0xef, 0x94, 0xac, 0x55, 0xfe,
111111
0x81, 0x64, 0xcc, 0xf9, 0x82, 0xa1, 0x38, 0x69, 0x1a,
112-
0x55, 0x19,
112+
0x55, 0x19,
113113
],
114114
output: vec![
115115
0xda, 0x0b, 0x34, 0x52, 0xb0, 0x6f, 0xe3, 0x41,
116116
0x62, 0x6a, 0xd0, 0x94, 0x9c, 0x18, 0x3f, 0xbd,
117-
0xa5, 0x67, 0x68, 0x26,
117+
0xa5, 0x67, 0x68, 0x26,
118118
],
119119
output_str: "da0b3452b06fe341626ad0949c183fbda5676826",
120120
},

src/hex.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
//! # Hex encoding and decoding
1616
//!
1717
18-
use std::fmt;
19-
use std::str;
18+
use core::{fmt, str};
2019
use {Error, Hash};
2120

2221
/// Trait for objects that can be serialized as hex strings
22+
#[cfg(any(test, feature = "std"))]
2323
pub trait ToHex {
2424
/// Hex representation of the object
2525
fn to_hex(&self) -> String;
@@ -39,6 +39,7 @@ pub trait FromHex: Sized {
3939
}
4040
}
4141

42+
#[cfg(any(test, feature = "std"))]
4243
impl<T: fmt::LowerHex> ToHex for T {
4344
/// Outputs the hash in hexadecimal form
4445
fn to_hex(&self) -> String {
@@ -152,9 +153,10 @@ pub fn format_hex_reverse(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
152153
Ok(())
153154
}
154155

156+
#[cfg(any(test, feature = "std"))]
155157
impl ToHex for [u8] {
156158
fn to_hex(&self) -> String {
157-
use std::fmt::Write;
159+
use core::fmt::Write;
158160
let mut ret = String::with_capacity(2 * self.len());
159161
for ch in self {
160162
write!(ret, "{:02x}", ch).expect("writing to string");
@@ -163,6 +165,7 @@ impl ToHex for [u8] {
163165
}
164166
}
165167

168+
#[cfg(any(test, feature = "std"))]
166169
impl FromHex for Vec<u8> {
167170
fn from_byte_iter<I>(iter: I) -> Result<Self, Error>
168171
where I: Iterator<Item=Result<u8, Error>> +
@@ -217,9 +220,9 @@ impl_fromhex_array!(512);
217220

218221
#[cfg(test)]
219222
mod tests {
220-
use std::fmt;
223+
use core::fmt;
221224

222-
use super::{format_hex, format_hex_reverse, ToHex, FromHex};
225+
use super::{format_hex, format_hex_reverse, FromHex, ToHex};
223226
use Error;
224227

225228
#[test]

src/hmac.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
//! # HMAC support
2121
22-
use std::{borrow, fmt, ops, str};
22+
use core::{borrow, fmt, ops, str};
2323
#[cfg(feature="serde")]
2424
use serde::{Serialize, Serializer, Deserialize, Deserializer};
2525

src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@
2727
#![deny(unused_mut)]
2828
#![deny(missing_docs)]
2929

30+
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
3031
#![cfg_attr(all(test, feature = "unstable"), feature(test))]
3132
#[cfg(all(test, feature = "unstable"))] extern crate test;
3233

34+
#[cfg(any(test, feature="std"))] extern crate core;
3335
#[cfg(feature="serde")] extern crate serde;
3436
#[cfg(all(test,feature="serde"))] extern crate serde_test;
3537
extern crate byteorder;
3638

3739
#[macro_use] mod util;
3840
#[macro_use] mod serde_macros;
39-
mod std_impls;
41+
#[cfg(any(test, feature = "std"))] mod std_impls;
4042
pub mod error;
4143
pub mod hex;
4244
pub mod hash160;
@@ -49,7 +51,7 @@ pub mod sha256d;
4951
pub mod siphash24;
5052
pub mod cmp;
5153

52-
use std::{borrow, fmt, hash, ops};
54+
use core::{borrow, fmt, hash, ops};
5355

5456
pub use hmac::{Hmac, HmacEngine};
5557
pub use error::Error;
@@ -80,7 +82,7 @@ pub trait Hash: Copy + Clone + PartialEq + Eq + Default + PartialOrd + Ord +
8082
ops::Index<ops::RangeTo<usize>, Output = [u8]> +
8183
ops::Index<ops::Range<usize>, Output = [u8]> +
8284
ops::Index<usize, Output = u8> +
83-
hex::ToHex + borrow::Borrow<[u8]>
85+
borrow::Borrow<[u8]>
8486
{
8587
/// A hashing engine which bytes can be serialized into. It is expected
8688
/// to implement the `io::Write` trait, and to never return errors under

src/ripemd160.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! # RIPEMD160
2121
2222
use byteorder::{ByteOrder, LittleEndian};
23-
use std::{cmp, str};
23+
use core::{cmp, str};
2424

2525
use HashEngine as EngineTrait;
2626
use Hash as HashTrait;

src/sha1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! # SHA1
1616
1717
use byteorder::{ByteOrder, BigEndian};
18-
use std::{cmp, str};
18+
use core::{cmp, str};
1919

2020
use HashEngine as EngineTrait;
2121
use Hash as HashTrait;

src/sha256.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! # SHA256
1616
1717
use byteorder::{ByteOrder, BigEndian};
18-
use std::{cmp, str};
18+
use core::{cmp, str};
1919

2020
use hex;
2121
use HashEngine as EngineTrait;

0 commit comments

Comments
 (0)