hashes: Duplicate impl_from_infallible

We don't want a dependency on `internals` for `hashes` unless its really
needed.

Duplicated the `impl_from_infallible` macro into `hashes::error` and
use it to implement from infallible for `FromSliceError`.
This commit is contained in:
Tobin C. Harding 2024-11-05 10:44:58 +11:00
parent 7652d0ddfc
commit fe8ca21ec2
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 20 additions and 0 deletions

View File

@ -8,6 +8,8 @@ use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct FromSliceError(pub(crate) FromSliceErrorInner); pub struct FromSliceError(pub(crate) FromSliceErrorInner);
impl_from_infallible!(FromSliceError);
impl FromSliceError { impl FromSliceError {
/// Returns the expected slice length. /// Returns the expected slice length.
pub fn expected_length(&self) -> usize { self.0.expected } pub fn expected_length(&self) -> usize { self.0.expected }
@ -23,6 +25,8 @@ pub(crate) struct FromSliceErrorInner {
pub(crate) got: usize, pub(crate) got: usize,
} }
impl_from_infallible!(FromSliceErrorInner);
impl fmt::Display for FromSliceError { impl fmt::Display for FromSliceError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid slice length {} (expected {})", self.0.got, self.0.expected) write!(f, "invalid slice length {} (expected {})", self.0.got, self.0.expected)
@ -31,3 +35,19 @@ impl fmt::Display for FromSliceError {
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl std::error::Error for FromSliceError {} 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;