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

new literacy::{Read, Write} traits to handle std/no_std #126

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 2 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:
- 1.29.0
- beta
- stable
fail-fast: false
steps:
- name: Checkout Crate
uses: actions/checkout@v2
Expand All @@ -78,7 +79,7 @@ jobs:
- name: Running cargo
env:
DO_FEATURE_MATRIX: true
DO_SCHEMARS_TESTS: ${{matrix.rust != '1.29.0'}}
ON_1_29_0: ${{matrix.rust == '1.29.0'}}
run: ./contrib/test.sh

Embedded:
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ default = [ "std" ]
std = []
serde-std = ["serde/std"]
unstable = [] # for benchmarking
use-core2 = ["core2"]

[dependencies]
serde = { version = "1.0", default-features = false, optional = true }
schemars = { version = "0.8.0", optional = true }
core2 = { version="0.3.0-alpha.1", optional= true, default-features = false }

[dev-dependencies]
serde_test = "1.0"
Expand Down
55 changes: 24 additions & 31 deletions contrib/test.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/bin/sh -ex
#!/bin/bash -ex

FEATURES="serde serde-std"
# Combination of features to test, should be every features combination but std and core2 together
# note std has a comma in the end so that following regex avoid matching serde-std
FEATURES=("" "std," "use-core2" "std,serde-std" "use-core2,serde-std")

# Use toolchain if explicitly specified
if [ -n "$TOOLCHAIN" ]
then
if [[ -n "$TOOLCHAIN" ]]; then
alias cargo="cargo +$TOOLCHAIN"
fi

Expand All @@ -18,35 +19,27 @@ export CARGO_TERM_VERBOSE=true
cargo build --all
cargo test --all

if [ "$DO_FEATURE_MATRIX" = true ]; then
cargo build --all --no-default-features
cargo test --all --no-default-features

# All features
cargo build --all --no-default-features --features="$FEATURES"
cargo test --all --features="$FEATURES"
# Single features
for feature in ${FEATURES}
do
cargo build --all --no-default-features --features="$feature"
cargo test --all --features="$feature"
done

# Other combos
cargo test --all --features="serde-std"
if [[ "$DO_FEATURE_MATRIX" = true ]]; then
for feature in "${FEATURES[@]}"
do
# On rust 1.29.0 we are only testing with std lib
if [[ "$ON_1_29_0" = false && ${feature} =~ "std," ]]; then
echo "--------------$feature----------------"
cargo build --no-default-features --features="$feature"
if [[ ${feature} =~ "std," ]] ; then
cargo test --no-default-features --features="$feature"
fi
cargo doc --no-default-features --features="$feature"
fi
done
fi

if [ "$DO_SCHEMARS_TESTS" = true ]; then
if [[ "$ON_1_29_0" = false ]]; then
(cd extended_tests/schemars && cargo test)
fi

# Docs
if [ "$DO_DOCS" = true ]; then
cargo doc --all --features="$FEATURES"
fi

# Webassembly stuff
if [ "$DO_WASM" = true ]; then
if [[ "$DO_WASM" = true ]]; then
clang --version &&
CARGO_TARGET_DIR=wasm cargo install --force wasm-pack &&
printf '\n[lib]\ncrate-type = ["cdylib", "rlib"]\n' >> Cargo.toml &&
Expand All @@ -55,20 +48,20 @@ if [ "$DO_WASM" = true ]; then
fi

# Address Sanitizer
if [ "$DO_ASAN" = true ]; then
if [[ "$DO_ASAN" = true ]]; then
cargo clean
CC='clang -fsanitize=address -fno-omit-frame-pointer' \
RUSTFLAGS='-Zsanitizer=address -Clinker=clang -Cforce-frame-pointers=yes' \
ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' \
cargo test --lib --all --features="$FEATURES" -Zbuild-std --target x86_64-unknown-linux-gnu
cargo test --lib --all -Zbuild-std --target x86_64-unknown-linux-gnu
cargo clean
CC='clang -fsanitize=memory -fno-omit-frame-pointer' \
RUSTFLAGS='-Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes' \
cargo test --lib --all --features="$FEATURES" -Zbuild-std --target x86_64-unknown-linux-gnu
cargo test --lib --all -Zbuild-std --target x86_64-unknown-linux-gnu
fi

# Bench
if [ "$DO_BENCH" = true ]; then
if [[ "$DO_BENCH" = true ]]; then
cargo bench --all --features="unstable"
fi

3 changes: 2 additions & 1 deletion embedded/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern crate alloc;

use alloc_cortex_m::CortexMHeap;
use bitcoin_hashes::{sha256, Hash, HashEngine};
use bitcoin_hashes::literacy::Write;
use core::alloc::Layout;
use core::str::FromStr;
use cortex_m::asm;
Expand All @@ -29,7 +30,7 @@ fn main() -> ! {
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) }

let mut engine = TestType::engine();
engine.input(b"abc");
engine.write(b"abc").unwrap();
let hash = TestType::from_engine(engine);

let hash_check =
Expand Down
50 changes: 24 additions & 26 deletions src/std_impls.rs → src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,71 +16,69 @@
//!
//! impls of traits defined in `std` and not `core`

use std::{error, io};
use {sha1, sha256, sha512, ripemd160, siphash24};
use ::{HashEngine, literacy};

use {hex, sha1, sha256, sha512, ripemd160, siphash24};
use HashEngine;
use Error;

impl error::Error for Error {
fn cause(&self) -> Option<&error::Error> { None }
#[cfg(any(test, feature = "std"))]
impl ::std::error::Error for ::Error {
fn cause(&self) -> Option<&::std::error::Error> { None }
fn description(&self) -> &str { "`std::error::description` is deprecated" }
}

impl error::Error for hex::Error {
fn cause(&self) -> Option<&error::Error> { None }
#[cfg(any(test, feature = "std"))]
impl ::std::error::Error for ::hex::Error {
fn cause(&self) -> Option<&::std::error::Error> { None }
fn description(&self) -> &str { "`std::error::description` is deprecated" }
}

impl io::Write for sha1::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
impl literacy::Write for sha1::HashEngine {
fn flush(&mut self) -> ::core::result::Result<(), literacy::Error> { Ok(()) }

fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, literacy::Error> {
self.input(buf);
Ok(buf.len())
}
}

impl io::Write for sha256::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
impl literacy::Write for sha256::HashEngine {
fn flush(&mut self) -> ::core::result::Result<(), literacy::Error> { Ok(()) }

fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, literacy::Error> {
self.input(buf);
Ok(buf.len())
}
}

impl io::Write for sha512::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
impl literacy::Write for sha512::HashEngine {
fn flush(&mut self) -> ::core::result::Result<(), literacy::Error> { Ok(()) }

fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, literacy::Error> {
self.input(buf);
Ok(buf.len())
}
}

impl io::Write for ripemd160::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
impl literacy::Write for ripemd160::HashEngine {
fn flush(&mut self) -> ::core::result::Result<(), literacy::Error> { Ok(()) }

fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, literacy::Error> {
self.input(buf);
Ok(buf.len())
}
}

impl io::Write for siphash24::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
impl literacy::Write for siphash24::HashEngine {
fn flush(&mut self) -> ::core::result::Result<(), literacy::Error> { Ok(()) }

fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
fn write(&mut self, buf: &[u8]) -> ::core::result::Result<usize, literacy::Error> {
self.input(buf);
Ok(buf.len())
}
}

#[cfg(test)]
mod tests {
use std::io::Write;

use ::literacy::Write;
use {sha1, sha256, sha256d, sha512, ripemd160, hash160, siphash24};
use Hash;

Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![deny(missing_docs)]
//#![deny(missing_docs)]

// In general, rust is absolutely horrid at supporting users doing things like,
// for example, compiling Rust code for real environments. Disable useless lints
Expand All @@ -40,6 +40,7 @@
#[cfg(any(test, feature="std"))] extern crate core;
#[cfg(feature="serde")] pub extern crate serde;
#[cfg(all(test,feature="serde"))] extern crate serde_test;
#[cfg(not(feature = "std"))] extern crate alloc;

#[doc(hidden)]
pub mod _export {
Expand All @@ -53,7 +54,7 @@ pub mod _export {

#[macro_use] mod util;
#[macro_use] pub mod serde_macros;
#[cfg(any(test, feature = "std"))] mod std_impls;
mod impls;
pub mod error;
pub mod hex;
pub mod hash160;
Expand All @@ -66,6 +67,7 @@ pub mod sha256t;
pub mod siphash24;
pub mod sha512;
pub mod cmp;
pub mod literacy;

use core::{borrow, fmt, hash, ops};

Expand Down
Loading