Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fuzztarget = [] # used by other rust-bitcoin projects to make hashes almost-noop
[dependencies]
serde = { version = "1.0", default-features = false, optional = true }
schemars = { version = "0.8.0", optional = true }
bare-io = { git = "https://github.com/justinmoon/bare-io.git", branch = "ancient", features = ["alloc", "ancient"], optional = true }

[dev-dependencies]
serde_test = "1.0"
Expand Down
8 changes: 4 additions & 4 deletions src/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
//!

use core::{fmt, str};

#[cfg(not(feature = "std"))]
use alloc::{vec::Vec, string::String};

use Hash;

/// Hex decoding error
Expand All @@ -40,7 +44,6 @@ impl fmt::Display for Error {
}

/// Trait for objects that can be serialized as hex strings
#[cfg(any(test, feature = "std"))]
pub trait ToHex {
/// Hex representation of the object
fn to_hex(&self) -> String;
Expand All @@ -60,7 +63,6 @@ pub trait FromHex: Sized {
}
}

#[cfg(any(test, feature = "std"))]
impl<T: fmt::LowerHex> ToHex for T {
/// Outputs the hash in hexadecimal form
fn to_hex(&self) -> String {
Expand Down Expand Up @@ -174,7 +176,6 @@ pub fn format_hex_reverse(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
Ok(())
}

#[cfg(any(test, feature = "std"))]
impl ToHex for [u8] {
fn to_hex(&self) -> String {
use core::fmt::Write;
Expand All @@ -186,7 +187,6 @@ impl ToHex for [u8] {
}
}

#[cfg(any(test, feature = "std"))]
impl FromHex for Vec<u8> {
fn from_byte_iter<I>(iter: I) -> Result<Self, Error>
where I: Iterator<Item=Result<u8, Error>> +
Expand Down
9 changes: 9 additions & 0 deletions src/std_impls.rs → src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@
//!
//! impls of traits defined in `std` and not `core`

#[cfg(feature="std")]
use std::{error, io};
#[cfg(feature="bare-io")]
use bare_io as io;

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


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

#[cfg(feature="std")]
impl error::Error for hex::Error {
fn cause(&self) -> Option<&error::Error> { None }
fn description(&self) -> &str { "`std::error::description` is deprecated" }
Expand Down Expand Up @@ -79,7 +85,10 @@ impl io::Write for siphash24::HashEngine {

#[cfg(test)]
mod tests {
#[cfg(feature="std")]
use std::io::Write;
#[cfg(feature="bare-io")]
use bare_io::Write;

use {sha1, sha256, sha256d, sha512, ripemd160, hash160, siphash24};
use Hash;
Expand Down
13 changes: 8 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
#![cfg_attr(all(test, feature = "unstable"), feature(test))]
#[cfg(all(test, feature = "unstable"))] extern crate test;

#[macro_use] pub extern crate alloc;
#[cfg(feature="bare-io")] extern crate bare_io;

#[cfg(any(test, feature="std"))] pub extern crate core;
#[cfg(feature="serde")] pub extern crate serde;
#[cfg(all(test,feature="serde"))] extern crate serde_test;
Expand All @@ -45,7 +48,7 @@

#[macro_use] mod util;
#[macro_use] pub mod serde_macros;
#[cfg(any(test, feature = "std"))] mod std_impls;
pub mod impls;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why we need this to be pub? It does not define any types to export

pub mod error;
pub mod hex;
pub mod hash160;
Expand Down Expand Up @@ -168,14 +171,14 @@ macro_rules! hash_newtype {
}
}

impl ::std::convert::From<$hash> for $newtype {
impl ::core::convert::From<$hash> for $newtype {
fn from(inner: $hash) -> $newtype {
// Due to rust 1.22 we have to use this instead of simple `Self(inner)`
Self { 0: inner }
}
}

impl ::std::convert::From<$newtype> for $hash {
impl ::core::convert::From<$newtype> for $hash {
fn from(hashtype: $newtype) -> $hash {
hashtype.0
}
Expand Down Expand Up @@ -217,9 +220,9 @@ macro_rules! hash_newtype {
}
}

impl ::std::str::FromStr for $newtype {
impl ::core::str::FromStr for $newtype {
type Err = $crate::hex::Error;
fn from_str(s: &str) -> ::std::result::Result<$newtype, Self::Err> {
fn from_str(s: &str) -> ::core::result::Result<$newtype, Self::Err> {
$crate::hex::FromHex::from_hex(s)
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ macro_rules! hex_fmt_impl(
hex_fmt_impl!($imp, $ty, );
);
($imp:ident, $ty:ident, $($gen:ident: $gent:ident),*) => (
impl<$($gen: $gent),*> $crate::core::fmt::$imp for $ty<$($gen),*> {
fn fmt(&self, f: &mut $crate::core::fmt::Formatter) -> $crate::core::fmt::Result {
impl<$($gen: $gent),*> ::core::fmt::$imp for $ty<$($gen),*> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
use $crate::hex::{format_hex, format_hex_reverse};
if $ty::<$($gen),*>::DISPLAY_BACKWARD {
format_hex_reverse(&self.0, f)
Expand All @@ -49,37 +49,37 @@ macro_rules! index_impl(
index_impl!($ty, );
);
($ty:ident, $($gen:ident: $gent:ident),*) => (
impl<$($gen: $gent),*> $crate::core::ops::Index<usize> for $ty<$($gen),*> {
impl<$($gen: $gent),*> ::core::ops::Index<usize> for $ty<$($gen),*> {
type Output = u8;
fn index(&self, index: usize) -> &u8 {
&self.0[index]
}
}

impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::Range<usize>> for $ty<$($gen),*> {
impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::Range<usize>> for $ty<$($gen),*> {
type Output = [u8];
fn index(&self, index: $crate::core::ops::Range<usize>) -> &[u8] {
fn index(&self, index: ::core::ops::Range<usize>) -> &[u8] {
&self.0[index]
}
}

impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::RangeFrom<usize>> for $ty<$($gen),*> {
impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::RangeFrom<usize>> for $ty<$($gen),*> {
type Output = [u8];
fn index(&self, index: $crate::core::ops::RangeFrom<usize>) -> &[u8] {
fn index(&self, index: ::core::ops::RangeFrom<usize>) -> &[u8] {
&self.0[index]
}
}

impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::RangeTo<usize>> for $ty<$($gen),*> {
impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::RangeTo<usize>> for $ty<$($gen),*> {
type Output = [u8];
fn index(&self, index: $crate::core::ops::RangeTo<usize>) -> &[u8] {
fn index(&self, index: ::core::ops::RangeTo<usize>) -> &[u8] {
&self.0[index]
}
}

impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::RangeFull> for $ty<$($gen),*> {
impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::RangeFull> for $ty<$($gen),*> {
type Output = [u8];
fn index(&self, index: $crate::core::ops::RangeFull) -> &[u8] {
fn index(&self, index: ::core::ops::RangeFull) -> &[u8] {
&self.0[index]
}
}
Expand All @@ -93,19 +93,19 @@ macro_rules! borrow_slice_impl(
borrow_slice_impl!($ty, );
);
($ty:ident, $($gen:ident: $gent:ident),*) => (
impl<$($gen: $gent),*> $crate::core::borrow::Borrow<[u8]> for $ty<$($gen),*> {
impl<$($gen: $gent),*> ::core::borrow::Borrow<[u8]> for $ty<$($gen),*> {
fn borrow(&self) -> &[u8] {
&self[..]
}
}

impl<$($gen: $gent),*> $crate::core::convert::AsRef<[u8]> for $ty<$($gen),*> {
impl<$($gen: $gent),*> ::core::convert::AsRef<[u8]> for $ty<$($gen),*> {
fn as_ref(&self) -> &[u8] {
&self[..]
}
}

impl<$($gen: $gent),*> $crate::core::ops::Deref for $ty<$($gen),*> {
impl<$($gen: $gent),*> ::core::ops::Deref for $ty<$($gen),*> {
type Target = [u8];

fn deref(&self) -> &Self::Target {
Expand Down