From d6578be1da1b891cff03cb33f88e7c7b85f73952 Mon Sep 17 00:00:00 2001 From: Justin Moon Date: Sat, 28 Nov 2020 17:56:46 -0600 Subject: [PATCH 1/3] Fix no_std Use `core::convert` instead of `std::convert` --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e87db33..a68561e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -168,14 +168,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 } From 1538e8bd0cc2adc7d47a3e86dc56bacefc5b5452 Mon Sep 17 00:00:00 2001 From: Justin Moon Date: Sat, 28 Nov 2020 22:08:03 -0600 Subject: [PATCH 2/3] Add bare-io feature to actually get this compiling on microcontroller --- Cargo.toml | 1 + src/hex.rs | 8 ++++---- src/{std_impls.rs => impls.rs} | 9 +++++++++ src/lib.rs | 9 ++++++--- src/util.rs | 28 ++++++++++++++-------------- 5 files changed, 34 insertions(+), 21 deletions(-) rename src/{std_impls.rs => impls.rs} (96%) diff --git a/Cargo.toml b/Cargo.toml index 0053eff..8800b91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = { version = "0.2", features = ["alloc"], optional = true } [dev-dependencies] serde_test = "1.0" diff --git a/src/hex.rs b/src/hex.rs index 34c4586..8b3e47d 100644 --- a/src/hex.rs +++ b/src/hex.rs @@ -16,6 +16,10 @@ //! use core::{fmt, str}; + +#[cfg(not(feature = "std"))] +use alloc::{vec::Vec, string::String}; + use Hash; /// Hex decoding error @@ -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; @@ -60,7 +63,6 @@ pub trait FromHex: Sized { } } -#[cfg(any(test, feature = "std"))] impl ToHex for T { /// Outputs the hash in hexadecimal form fn to_hex(&self) -> String { @@ -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; @@ -186,7 +187,6 @@ impl ToHex for [u8] { } } -#[cfg(any(test, feature = "std"))] impl FromHex for Vec { fn from_byte_iter(iter: I) -> Result where I: Iterator> + diff --git a/src/std_impls.rs b/src/impls.rs similarity index 96% rename from src/std_impls.rs rename to src/impls.rs index aa87c30..f8d3bd5 100644 --- a/src/std_impls.rs +++ b/src/impls.rs @@ -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" } @@ -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; diff --git a/src/lib.rs b/src/lib.rs index a68561e..e650581 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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; pub mod error; pub mod hex; pub mod hash160; @@ -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) } } diff --git a/src/util.rs b/src/util.rs index bb21109..0cfa21e 100644 --- a/src/util.rs +++ b/src/util.rs @@ -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) @@ -49,37 +49,37 @@ macro_rules! index_impl( index_impl!($ty, ); ); ($ty:ident, $($gen:ident: $gent:ident),*) => ( - impl<$($gen: $gent),*> $crate::core::ops::Index for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Index 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> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::Range> for $ty<$($gen),*> { type Output = [u8]; - fn index(&self, index: $crate::core::ops::Range) -> &[u8] { + fn index(&self, index: ::core::ops::Range) -> &[u8] { &self.0[index] } } - impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::RangeFrom> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::RangeFrom> for $ty<$($gen),*> { type Output = [u8]; - fn index(&self, index: $crate::core::ops::RangeFrom) -> &[u8] { + fn index(&self, index: ::core::ops::RangeFrom) -> &[u8] { &self.0[index] } } - impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::RangeTo> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::RangeTo> for $ty<$($gen),*> { type Output = [u8]; - fn index(&self, index: $crate::core::ops::RangeTo) -> &[u8] { + fn index(&self, index: ::core::ops::RangeTo) -> &[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] } } @@ -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 { From a65775fe7a0de2d658803e42f0405a64635767ee Mon Sep 17 00:00:00 2001 From: Justin Moon Date: Fri, 11 Dec 2020 20:49:48 -0600 Subject: [PATCH 3/3] Upgrade to bare-io fork which works on 1.36.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 8800b91..4612e77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +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 = { version = "0.2", features = ["alloc"], 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"