From 2b56f763d0259e1a9a53e73439eafe990d65db17 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Mon, 8 Jul 2024 14:36:37 +1000 Subject: [PATCH] hashes: Remove to/from/as_raw_hash In an effort to shrink the API of `hashes` remove the `from_raw_hash`, `to_raw_hash`, and `as_raw_hash` inherent functions from types created with the `hash_newtype` macro. There are a few reasons why this is favourable: - It allows stable crates to use the macro and not expose unstable `hashes` types in their API. - It makes types created with the macro less "general" in the sense that its more obscure to just hash any data into them. This allows us to write cleaner APIs in `rust-bitcoin`. --- hashes/src/lib.rs | 11 ----------- hashes/src/util.rs | 15 --------------- 2 files changed, 26 deletions(-) diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs index d54ccd564..d53eccf58 100644 --- a/hashes/src/lib.rs +++ b/hashes/src/lib.rs @@ -311,17 +311,6 @@ mod tests { 0x15, 0x26, 0x37, 0x48, 0x59, 0x6a, 0x7b, 0x8c, ]); - #[test] - fn convert_newtypes() { - let h1 = DUMMY; - let h2: TestNewtype2 = h1.to_raw_hash().into(); - assert_eq!(&h1[..], &h2[..]); - - let h = sha256d::Hash::hash(&[]); - let h2: TestNewtype = h.to_string().parse().unwrap(); - assert_eq!(h2.to_raw_hash(), h); - } - #[test] fn newtype_fmt_roundtrip() { let orig = DUMMY; diff --git a/hashes/src/util.rs b/hashes/src/util.rs index 746828b7c..eafea64cf 100644 --- a/hashes/src/util.rs +++ b/hashes/src/util.rs @@ -194,21 +194,6 @@ macro_rules! hash_newtype { #[allow(unused)] // Private wrapper types may not need all functions. impl $newtype { - /// Creates this wrapper type from the inner hash type. - pub const fn from_raw_hash(inner: $hash) -> $newtype { - $newtype(inner) - } - - /// Returns the inner hash (sha256, sh256d etc.). - pub const fn to_raw_hash(self) -> $hash { - self.0 - } - - /// Returns a reference to the inner hash (sha256, sh256d etc.). - pub const fn as_raw_hash(&self) -> &$hash { - &self.0 - } - /// Copies a byte slice into a hash object. pub fn from_slice(sl: &[u8]) -> $crate::_export::_core::result::Result<$newtype, $crate::FromSliceError> { Ok($newtype(<$hash as $crate::Hash>::from_slice(sl)?))