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

new literacy::{Read, Write} traits to handle std/no_std (alternative) #127

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
8 changes: 4 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
rust:
- nightly
steps:
- name: Checkout Crate
- name: Checkout Crate
uses: actions/checkout@v2
- name: Checkout Toolchain
uses: actions-rs/toolchain@v1
Expand All @@ -29,7 +29,7 @@ jobs:
DO_BENCH: true
run: ./contrib/test.sh

wasm:
wasm:
name: Stable - Docs / WebAssembly Build
runs-on: ubuntu-latest
strategy:
Expand Down 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 All @@ -102,4 +103,3 @@ jobs:
CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
run: cd embedded && cargo run --target thumbv7m-none-eabi


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
56 changes: 24 additions & 32 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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we only test with std and just build the rest of the time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because tests have std enabled

#![cfg_attr(all(not(test), not(feature = "std")), no_std)]

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,19 @@ 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be --all-features here and above, waiting to find a solution for defaulting to core2 if enabled (without conflicting with std)

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

5 changes: 3 additions & 2 deletions embedded/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ extern crate bitcoin_hashes;
extern crate alloc;

use alloc_cortex_m::CortexMHeap;
use bitcoin_hashes::{sha256, Hash, HashEngine};
use bitcoin_hashes::{sha256, Hash};
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
85 changes: 59 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,104 @@
//!
//! 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 {
type Error = ();

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

fn write_all(&mut self, buf: &[u8]) -> ::core::result::Result<(), ()> {
self.write(buf)?;
Ok(())
}

fn flush(&mut self) -> ::core::result::Result<(), ()> { Ok(()) }
}

impl io::Write for sha256::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
impl literacy::Write for sha256::HashEngine {
type Error = ();

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

fn write_all(&mut self, buf: &[u8]) -> ::core::result::Result<(), ()> {
self.write(buf)?;
Ok(())
}

fn flush(&mut self) -> ::core::result::Result<(), ()> { Ok(()) }
}

impl io::Write for sha512::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
impl literacy::Write for sha512::HashEngine {
type Error = ();

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

fn write_all(&mut self, buf: &[u8]) -> ::core::result::Result<(), ()> {
self.write(buf)?;
Ok(())
}

fn flush(&mut self) -> ::core::result::Result<(), ()> { Ok(()) }
}

impl io::Write for ripemd160::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
impl literacy::Write for ripemd160::HashEngine {
type Error = ();

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

fn write_all(&mut self, buf: &[u8]) -> ::core::result::Result<(), ()> {
self.write(buf)?;
Ok(())
}

fn flush(&mut self) -> ::core::result::Result<(), ()> { Ok(()) }
}

impl io::Write for siphash24::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }
impl literacy::Write for siphash24::HashEngine {
type Error = ();

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

fn write_all(&mut self, buf: &[u8]) -> ::core::result::Result<(), ()> {
self.write(buf)?;
Ok(())
}

fn flush(&mut self) -> ::core::result::Result<(), ()> { Ok(()) }
}

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

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

Expand Down
8 changes: 6 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)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope you didn't intend to land this :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed, and added doc where missing in 8bf2f9e


// 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 @@ -51,9 +51,12 @@ pub mod _export {

#[cfg(feature = "schemars")] extern crate schemars;

#[cfg(all(not(feature = "use-core2"), not(feature = "std")))]
extern crate alloc;

#[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 +69,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