Merge rust-bitcoin/rust-bitcoin#1450: hashes: Do not implement `Deref`
b7a84d0c68
hashes: Do not implement Deref (Tobin C. Harding) Pull request description: Currently we implement `Deref` for hashes. From the docs [0] > Deref should only be implemented for smart pointers to avoid confusion Furthermore because we implement `Deref` as well as implement `internals::hex::display::DisplayHex` for slices hashes get coerced into slices and `to_lower_hex_string` can be called on them, this is incorrect because `DisplayHex` does not account for hashes that display backwards so we end up with the wrong string. This is an API breaking change, and I have not built any other crates in our stack to check if anything breaks. [0] https://doc.rust-lang.org/std/ops/trait.Deref.html ACKs for top commit: apoelstra: ACKb7a84d0c68
sanket1729: ACKb7a84d0c68
Tree-SHA512: 573169095dd9f9032e3f685e3b0af3b569f1cb5368e1b2f37940504fd887592f46488abcfbe84107baf9c5453c7db47bc342a6bc273ff98cb0570ffacb549c21
This commit is contained in:
commit
d581207cb8
|
@ -17,6 +17,7 @@ panic-halt = "0.2.0"
|
|||
alloc-cortex-m = "0.4.1"
|
||||
bitcoin = { path="../", default-features = false, features = ["no-std", "secp-lowmemory"] }
|
||||
|
||||
|
||||
[[bin]]
|
||||
name = "embedded"
|
||||
test = false
|
||||
|
|
|
@ -28,9 +28,9 @@ macro_rules! hex_fmt_impl(
|
|||
write!(f, "0x")?;
|
||||
}
|
||||
if $ty::<$($gen),*>::DISPLAY_BACKWARD {
|
||||
hex::format_hex_reverse(&self.0, f)
|
||||
hex::format_hex_reverse(self.as_ref(), f)
|
||||
} else {
|
||||
hex::format_hex(&self.0, f)
|
||||
hex::format_hex(self.as_ref(), f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -67,14 +67,6 @@ macro_rules! borrow_slice_impl(
|
|||
&self[..]
|
||||
}
|
||||
}
|
||||
|
||||
impl<$($gen: $gent),*> $crate::_export::_core::ops::Deref for $ty<$($gen),*> {
|
||||
type Target = [u8];
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
|
@ -240,10 +232,18 @@ mod test {
|
|||
use crate::{Hash, sha256};
|
||||
|
||||
#[test]
|
||||
fn borrow_slice_impl_to_vec() {
|
||||
// Test that the borrow_slice_impl macro gives to_vec.
|
||||
fn hash_as_ref() {
|
||||
let hash = sha256::Hash::hash(&[3, 50]);
|
||||
assert_eq!(hash.to_vec().len(), sha256::Hash::LEN);
|
||||
assert_eq!(hash.as_ref(), hash.as_inner());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_borrow() {
|
||||
use core::borrow::Borrow;
|
||||
|
||||
let hash = sha256::Hash::hash(&[3, 50]);
|
||||
let borrowed: &[u8] = hash.borrow();
|
||||
assert_eq!(borrowed, hash.as_inner());
|
||||
}
|
||||
|
||||
hash_newtype!(TestHash, crate::sha256d::Hash, 32, doc="Test hash.");
|
||||
|
|
Loading…
Reference in New Issue