Merge rust-bitcoin/rust-bitcoin#1570: hashes: Improve feature gating

9ae3285882 hashes: Improve feature gating (Tobin C. Harding)

Pull request description:

  Currently we have a few things mixed up in the feature gating of `hashes`.

  Observe that:
  - `io::Write` and `io::Read` is not related to allocation but rather provided by `core2`
  - "std" should be able to enable "alloc".

  Improve feature gating by doing:

  - Enable "alloc" from "std" and simplify `cfg` codebase wide.
  - Enable "internals/alloc" from "alloc".
  - Fix feature gating to use the minimal requirement i.e., "alloc".

  Please note one anomaly, `internals` does not set "std" as the default feature but `hashes` does.

ACKs for top commit:
  Kixunil:
    ACK 9ae3285882
  apoelstra:
    ACK 9ae3285882

Tree-SHA512: 9d84eb3e2e5fbf80d912193bfa8210a4763388ae246a36cb409416ebbc7e1397b7bc894ba21ec01c416369ecddf1c6d93062c10397f6cff47bd7964f546a8709
This commit is contained in:
Andrew Poelstra 2023-02-13 22:53:29 +00:00
commit 7c5319826b
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
12 changed files with 19 additions and 19 deletions

View File

@ -14,7 +14,7 @@ exclude = ["tests", "contrib"]
[features] [features]
default = ["std"] default = ["std"]
std = ["internals/std"] std = ["alloc", "internals/std"]
alloc = ["internals/alloc"] alloc = ["internals/alloc"]
schemars = ["actual-schemars", "dyn-clone"] schemars = ["actual-schemars", "dyn-clone"]
serde-std = ["serde/std"] serde-std = ["serde/std"]

View File

@ -49,12 +49,12 @@ fn from_engine(e: HashEngine) -> Hash {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(feature = "alloc")]
fn test() { fn test() {
use crate::{hash160, Hash, HashEngine}; use crate::{hash160, Hash, HashEngine};
#[derive(Clone)] #[derive(Clone)]
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(feature = "alloc")]
struct Test { struct Test {
input: Vec<u8>, input: Vec<u8>,
output: Vec<u8>, output: Vec<u8>,

View File

@ -15,12 +15,13 @@
//! Hex encoding and decoding. //! Hex encoding and decoding.
//! //!
#[cfg(any(feature = "std", feature = "alloc"))]
#[cfg(all(feature = "alloc", not(feature = "std")))]
use crate::alloc::vec::Vec; use crate::alloc::vec::Vec;
#[cfg(any(test, feature = "std"))] #[cfg(feature = "std")]
use std::io; use std::io;
#[cfg(all(not(test), not(feature = "std"), feature = "core2"))] #[cfg(all(feature = "core2", not(feature = "std")))]
use core2::io; use core2::io;
use core::{fmt, str}; use core::{fmt, str};
@ -201,7 +202,7 @@ impl_fromhex_array!(384);
impl_fromhex_array!(512); impl_fromhex_array!(512);
#[cfg(test)] #[cfg(test)]
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(feature = "alloc")]
mod tests { mod tests {
use super::*; use super::*;
use internals::hex::exts::DisplayHex; use internals::hex::exts::DisplayHex;

View File

@ -245,7 +245,7 @@ impl<'de, T: Hash + Deserialize<'de>> Deserialize<'de> for Hmac<T> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(feature = "alloc")]
fn test() { fn test() {
use crate::{sha256, HashEngine, HmacEngine, Hash, Hmac}; use crate::{sha256, HashEngine, HmacEngine, Hash, Hmac};

View File

@ -102,8 +102,7 @@
#[cfg(bench)] extern crate test; #[cfg(bench)] extern crate test;
#[cfg(any(test, feature = "std"))] extern crate core; #[cfg(any(test, feature = "std"))] extern crate core;
#[cfg(feature = "core2")] extern crate core2; #[cfg(feature = "core2")] extern crate core2;
#[cfg(feature = "alloc")] extern crate alloc; #[cfg(all(feature = "alloc", not(feature = "std")))] extern crate alloc;
#[cfg(all(not(feature = "alloc"), feature = "std"))] use std as alloc;
#[cfg(feature = "serde")] pub extern crate serde; #[cfg(feature = "serde")] pub extern crate serde;
#[cfg(all(test,feature = "serde"))] extern crate serde_test; #[cfg(all(test,feature = "serde"))] extern crate serde_test;

View File

@ -405,7 +405,7 @@ impl HashEngine {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(feature = "alloc")]
fn test() { fn test() {
use crate::{Hash, HashEngine, ripemd160}; use crate::{Hash, HashEngine, ripemd160};

View File

@ -143,7 +143,7 @@ impl HashEngine {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(feature = "alloc")]
fn test() { fn test() {
use crate::{sha1, Hash, HashEngine}; use crate::{sha1, Hash, HashEngine};

View File

@ -324,7 +324,7 @@ mod tests {
use crate::{Hash, HashEngine, sha256}; use crate::{Hash, HashEngine, sha256};
#[test] #[test]
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(feature = "alloc")]
fn test() { fn test() {
#[derive(Clone)] #[derive(Clone)]
struct Test { struct Test {

View File

@ -44,7 +44,7 @@ fn from_engine(e: sha256::HashEngine) -> Hash {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(feature = "alloc")]
fn test() { fn test() {
use crate::{sha256, sha256d, Hash, HashEngine}; use crate::{sha256, sha256d, Hash, HashEngine};

View File

@ -122,7 +122,7 @@ macro_rules! sha256t_hash_newtype {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{sha256, sha256t}; use crate::{sha256, sha256t};
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(feature = "alloc")]
use crate::Hash; use crate::Hash;
const TEST_MIDSTATE: [u8; 32] = [ const TEST_MIDSTATE: [u8; 32] = [
@ -143,13 +143,13 @@ mod tests {
} }
/// A hash tagged with `$name`. /// A hash tagged with `$name`.
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(feature = "alloc")]
pub type TestHash = sha256t::Hash<TestHashTag>; pub type TestHash = sha256t::Hash<TestHashTag>;
sha256t_hash_newtype!(NewTypeHash, NewTypeTag, TEST_MIDSTATE, 64, doc="test hash", true); sha256t_hash_newtype!(NewTypeHash, NewTypeTag, TEST_MIDSTATE, 64, doc="test hash", true);
#[test] #[test]
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(feature = "alloc")]
fn test_sha256t() { fn test_sha256t() {
assert_eq!( assert_eq!(
TestHash::hash(&[0]).to_string(), TestHash::hash(&[0]).to_string(),

View File

@ -308,7 +308,7 @@ impl HashEngine {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(feature = "alloc")]
fn test() { fn test() {
use crate::{sha512, Hash, HashEngine}; use crate::{sha512, Hash, HashEngine};

View File

@ -86,7 +86,7 @@ fn from_engine(e: HashEngine) -> Hash {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(feature = "alloc")]
fn test() { fn test() {
use crate::{sha512_256, Hash, HashEngine}; use crate::{sha512_256, Hash, HashEngine};