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

Commit 5072646

Browse files
authored
Merge pull request #128 from devrandom/core2
Add core2 support
2 parents 235ff17 + 5ab0f4b commit 5072646

File tree

7 files changed

+68
-21
lines changed

7 files changed

+68
-21
lines changed

.github/workflows/rust.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,9 @@ jobs:
101101
RUSTFLAGS: "-C link-arg=-Tlink.x"
102102
CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
103103
run: cd embedded && cargo run --target thumbv7m-none-eabi
104-
104+
- name: Run with alloc
105+
env:
106+
RUSTFLAGS: "-C link-arg=-Tlink.x"
107+
CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
108+
run: cd embedded && cargo run --target thumbv7m-none-eabi --features=alloc
105109

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@ name = "bitcoin_hashes"
1515
path = "src/lib.rs"
1616

1717
[features]
18-
default = [ "std" ]
18+
default = ["std"]
1919
std = []
20+
# If you disable std, you can still use a Write trait via the core2 feature.
21+
# You can also use ToHex via the alloc feature, as it requires Vec/String.
22+
# And you can still just disable std by disabling default features, without enabling these two.
23+
alloc = ["core2/alloc"]
2024
serde-std = ["serde/std"]
2125
unstable = [] # for benchmarking
2226

2327
[dependencies]
2428
serde = { version = "1.0", default-features = false, optional = true }
2529
schemars = { version = "0.8.0", optional = true }
30+
core2 = { version = "0.3.0-alpha.1", optional = true, default_features = false }
2631

2732
[dev-dependencies]
2833
serde_test = "1.0"

embedded/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ readme = "README.md"
55
name = "embedded"
66
version = "0.1.0"
77

8+
[features]
9+
alloc = ["alloc-cortex-m", "bitcoin_hashes/alloc"]
10+
811
[dependencies]
912
cortex-m = "0.6.0"
1013
cortex-m-rt = "0.6.10"
1114
cortex-m-semihosting = "0.3.3"
1215
panic-halt = "0.2.0"
13-
alloc-cortex-m = "0.4.1"
14-
bitcoin_hashes = { path="../", default-features = false }
16+
alloc-cortex-m = { version = "0.4.1", optional = true }
17+
bitcoin_hashes = { path="../", default-features = false, features = ["core2"] }
18+
core2 = { version = "0.3.0-alpha.1", default_features = false }
1519

1620
[[bin]]
1721
name = "embedded"

embedded/src/main.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,69 @@
1-
#![feature(alloc_error_handler)]
1+
#![cfg_attr(feature = "alloc", feature(alloc_error_handler))]
22
#![no_std]
33
#![no_main]
44

55
#[macro_use]
66
extern crate bitcoin_hashes;
77

8-
extern crate alloc;
8+
#[cfg(feature = "alloc")] extern crate alloc;
9+
#[cfg(feature = "alloc")] use alloc_cortex_m::CortexMHeap;
10+
#[cfg(feature = "alloc")] use core::alloc::Layout;
11+
#[cfg(feature = "alloc")] use cortex_m::asm;
12+
#[cfg(feature = "alloc")] use bitcoin_hashes::hex::ToHex;
913

10-
use alloc_cortex_m::CortexMHeap;
1114
use bitcoin_hashes::{sha256, Hash, HashEngine};
12-
use core::alloc::Layout;
15+
use core2::io::Write;
1316
use core::str::FromStr;
14-
use cortex_m::asm;
1517
use cortex_m_rt::entry;
1618
use cortex_m_semihosting::{debug, hprintln};
1719
use panic_halt as _;
1820

1921
hash_newtype!(TestType, sha256::Hash, 32, doc = "test");
2022

2123
// this is the allocator the application will use
24+
#[cfg(feature = "alloc")]
2225
#[global_allocator]
2326
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
2427

28+
#[cfg(feature = "alloc")]
2529
const HEAP_SIZE: usize = 1024; // in bytes
2630

2731
#[entry]
2832
fn main() -> ! {
33+
#[cfg(feature = "alloc")]
2934
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) }
3035

36+
let mut engine = TestType::engine();
37+
engine.write_all(b"abc").unwrap();
38+
check_result(engine);
39+
3140
let mut engine = TestType::engine();
3241
engine.input(b"abc");
42+
check_result(engine);
43+
44+
debug::exit(debug::EXIT_SUCCESS);
45+
loop {}
46+
}
47+
48+
fn check_result(engine: sha256::HashEngine) {
3349
let hash = TestType::from_engine(engine);
3450

3551
let hash_check =
3652
TestType::from_str("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
3753
.unwrap();
3854
hprintln!("hash:{} hash_check:{}", hash, hash_check).unwrap();
39-
if hash == hash_check {
40-
debug::exit(debug::EXIT_SUCCESS);
41-
} else {
55+
if hash != hash_check {
4256
debug::exit(debug::EXIT_FAILURE);
4357
}
4458

45-
loop {}
59+
#[cfg(feature = "alloc")]
60+
if hash.to_hex() != hash_check.to_hex() {
61+
debug::exit(debug::EXIT_FAILURE);
62+
}
4663
}
4764

4865
// define what happens in an Out Of Memory (OOM) condition
66+
#[cfg(feature = "alloc")]
4967
#[alloc_error_handler]
5068
fn alloc_error(_layout: Layout) -> ! {
5169
asm::bkpt();

src/hex.rs

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

@@ -40,7 +45,7 @@ impl fmt::Display for Error {
4045
}
4146

4247
/// Trait for objects that can be serialized as hex strings
43-
#[cfg(any(test, feature = "std"))]
48+
#[cfg(any(test, feature = "std", feature = "alloc"))]
4449
pub trait ToHex {
4550
/// Hex representation of the object
4651
fn to_hex(&self) -> String;
@@ -60,7 +65,7 @@ pub trait FromHex: Sized {
6065
}
6166
}
6267

63-
#[cfg(any(test, feature = "std"))]
68+
#[cfg(any(test, feature = "std", feature = "alloc"))]
6469
impl<T: fmt::LowerHex> ToHex for T {
6570
/// Outputs the hash in hexadecimal form
6671
fn to_hex(&self) -> String {
@@ -174,7 +179,7 @@ pub fn format_hex_reverse(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
174179
Ok(())
175180
}
176181

177-
#[cfg(any(test, feature = "std"))]
182+
#[cfg(any(test, feature = "alloc", feature = "std"))]
178183
impl ToHex for [u8] {
179184
fn to_hex(&self) -> String {
180185
use core::fmt::Write;
@@ -186,7 +191,7 @@ impl ToHex for [u8] {
186191
}
187192
}
188193

189-
#[cfg(any(test, feature = "std"))]
194+
#[cfg(any(test, feature = "std", feature = "alloc"))]
190195
impl FromHex for Vec<u8> {
191196
fn from_byte_iter<I>(iter: I) -> Result<Self, Error>
192197
where I: Iterator<Item=Result<u8, Error>> +

src/std_impls.rs renamed to src/impls.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,31 @@
1212
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1313
//
1414

15-
//! `std` Impls
15+
//! `std` / `core2` Impls
1616
//!
17-
//! impls of traits defined in `std` and not `core`
17+
//! impls of traits defined in `std` / `core2` and not in `core`
1818
19+
#[cfg(feature = "std")]
1920
use std::{error, io};
2021

22+
#[cfg(not(feature = "std"))]
23+
use core2::{error, io};
24+
2125
use {hex, sha1, sha256, sha512, ripemd160, siphash24};
2226
use HashEngine;
2327
use Error;
2428

2529
impl error::Error for Error {
30+
#[cfg(feature = "std")]
2631
fn cause(&self) -> Option<&error::Error> { None }
32+
#[cfg(feature = "std")]
2733
fn description(&self) -> &str { "`std::error::description` is deprecated" }
2834
}
2935

3036
impl error::Error for hex::Error {
37+
#[cfg(feature = "std")]
3138
fn cause(&self) -> Option<&error::Error> { None }
39+
#[cfg(feature = "std")]
3240
fn description(&self) -> &str { "`std::error::description` is deprecated" }
3341
}
3442

@@ -79,7 +87,7 @@ impl io::Write for siphash24::HashEngine {
7987

8088
#[cfg(test)]
8189
mod tests {
82-
use std::io::Write;
90+
use super::io::Write;
8391

8492
use {sha1, sha256, sha256d, sha512, ripemd160, hash160, siphash24};
8593
use Hash;

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
#[cfg(all(test, feature = "unstable"))] extern crate test;
3939

4040
#[cfg(any(test, feature="std"))] extern crate core;
41+
#[cfg(feature="core2")] extern crate core2;
42+
#[cfg(any(feature = "alloc"))] extern crate alloc;
43+
#[cfg(any(feature = "std"))] use std as alloc;
4144
#[cfg(feature="serde")] pub extern crate serde;
4245
#[cfg(all(test,feature="serde"))] extern crate serde_test;
4346

@@ -53,7 +56,7 @@ pub mod _export {
5356

5457
#[macro_use] mod util;
5558
#[macro_use] pub mod serde_macros;
56-
#[cfg(any(test, feature = "std"))] mod std_impls;
59+
#[cfg(any(feature = "std", feature = "core2"))] mod impls;
5760
pub mod error;
5861
pub mod hex;
5962
pub mod hash160;

0 commit comments

Comments
 (0)