From 2af764e8594e99f3cbebc70cfcf058fe4a24d8bc Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 1 Mar 2024 16:22:05 +1100 Subject: [PATCH] hashes: Fix leaf error type Leaf error types should typically have private fields, provide accessor functions, and not use `non_exhaustive`. --- hashes/src/lib.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs index d2e7b850..e1381919 100644 --- a/hashes/src/lib.rs +++ b/hashes/src/lib.rs @@ -234,12 +234,19 @@ pub trait Hash: /// Attempted to create a hash from an invalid length slice. #[derive(Debug, Clone, PartialEq, Eq)] -#[non_exhaustive] 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) @@ -247,9 +254,7 @@ impl fmt::Display for FromSliceError { } #[cfg(feature = "std")] -impl std::error::Error for FromSliceError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } -} +impl std::error::Error for FromSliceError {} #[cfg(test)] mod tests {