Merge rust-bitcoin/rust-bitcoin#3579: hashes: Hide error internals

fe8ca21ec2 hashes: Duplicate impl_from_infallible (Tobin C. Harding)
7652d0ddfc hashes: Hide innards of FromSliceError (Tobin C. Harding)
5232bba62b hashes: Move FromSliceError to submodule (Tobin C. Harding)

Pull request description:

  Hide the internals of the `hashes::FromSliceError`.

ACKs for top commit:
  apoelstra:
    ACK fe8ca21ec282cfe50e3f39b7f86c619d30d7f542; successfully ran local tests

Tree-SHA512: dac33777353dd81c8c86de331b2ab30d0a5268f2be7685f85405d29809ec36eeab31b0e71c9f09e820e06a93c3f05b7d675e5e729b780e8600b960cad4a02c77
This commit is contained in:
merge-script 2024-11-06 15:33:27 +00:00
commit b09ce778f8
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
4 changed files with 62 additions and 26 deletions

53
hashes/src/error.rs Normal file
View File

@ -0,0 +1,53 @@
// SPDX-License-Identifier: CC0-1.0
//! Error code for the `hashes` crate.
use core::fmt;
/// Attempted to create a hash from an invalid length slice.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FromSliceError(pub(crate) FromSliceErrorInner);
impl_from_infallible!(FromSliceError);
impl FromSliceError {
/// Returns the expected slice length.
pub fn expected_length(&self) -> usize { self.0.expected }
/// Returns the invalid slice length.
pub fn invalid_length(&self) -> usize { self.0.got }
}
/// Attempted to create a hash from an invalid length slice.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct FromSliceErrorInner {
pub(crate) expected: usize,
pub(crate) got: usize,
}
impl_from_infallible!(FromSliceErrorInner);
impl fmt::Display for FromSliceError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid slice length {} (expected {})", self.0.got, self.0.expected)
}
}
#[cfg(feature = "std")]
impl std::error::Error for FromSliceError {}
/// Derives `From<core::convert::Infallible>` for the given type.
// This is a duplicate of `internals::impl_from_infallible`, see there for complete docs.
#[doc(hidden)]
macro_rules! impl_from_infallible {
( $name:ident $(< $( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? ) => {
impl $(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)?
From<core::convert::Infallible>
for $name
$(< $( $lt ),+ >)?
{
fn from(never: core::convert::Infallible) -> Self { match never {} }
}
}
}
pub(crate) use impl_from_infallible;

View File

@ -129,7 +129,10 @@ macro_rules! hash_type_no_default {
sl: &[u8], sl: &[u8],
) -> $crate::_export::_core::result::Result<Hash, $crate::FromSliceError> { ) -> $crate::_export::_core::result::Result<Hash, $crate::FromSliceError> {
if sl.len() != $bits / 8 { if sl.len() != $bits / 8 {
Err($crate::FromSliceError { expected: $bits / 8, got: sl.len() }) Err($crate::FromSliceError($crate::error::FromSliceErrorInner {
expected: $bits / 8,
got: sl.len(),
}))
} else { } else {
let mut ret = [0; $bits / 8]; let mut ret = [0; $bits / 8];
ret.copy_from_slice(sl); ret.copy_from_slice(sl);

View File

@ -99,6 +99,7 @@ pub mod _export {
mod internal_macros; mod internal_macros;
pub mod cmp; pub mod cmp;
pub mod error;
pub mod hash160; pub mod hash160;
pub mod hkdf; pub mod hkdf;
pub mod hmac; pub mod hmac;
@ -130,6 +131,7 @@ use core::{convert, fmt, hash};
#[rustfmt::skip] // Keep public re-exports separate. #[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)] #[doc(inline)]
pub use self::{ pub use self::{
error::FromSliceError,
hkdf::Hkdf, hkdf::Hkdf,
hmac::{Hmac, HmacEngine}, hmac::{Hmac, HmacEngine},
}; };
@ -320,30 +322,6 @@ fn incomplete_block_len<H: HashEngine>(eng: &H) -> usize {
(eng.n_bytes_hashed() % block_size) as usize (eng.n_bytes_hashed() % block_size) as usize
} }
/// Attempted to create a hash from an invalid length slice.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FromSliceError {
expected: usize,
got: usize,
}
impl FromSliceError {
/// Returns the expected slice length.
pub fn expected_length(&self) -> usize { self.expected }
/// Returns the invalid slice length.
pub fn invalid_length(&self) -> usize { self.got }
}
impl fmt::Display for FromSliceError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid slice length {} (expected {})", self.got, self.expected)
}
}
#[cfg(feature = "std")]
impl std::error::Error for FromSliceError {}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -45,8 +45,10 @@ where
/// Copies a byte slice into a hash object. /// Copies a byte slice into a hash object.
#[deprecated(since = "0.15.0", note = "use `from_byte_array` instead")] #[deprecated(since = "0.15.0", note = "use `from_byte_array` instead")]
pub fn from_slice(sl: &[u8]) -> Result<Hash<T>, FromSliceError> { pub fn from_slice(sl: &[u8]) -> Result<Hash<T>, FromSliceError> {
use crate::error::FromSliceErrorInner;
if sl.len() != 32 { if sl.len() != 32 {
Err(FromSliceError { expected: 32, got: sl.len() }) Err(FromSliceError(FromSliceErrorInner { expected: 32, got: sl.len() }))
} else { } else {
let mut ret = [0; 32]; let mut ret = [0; 32];
ret.copy_from_slice(sl); ret.copy_from_slice(sl);