hashes: Fix leaf error type

Leaf error types should typically have private fields, provide accessor
functions, and not use `non_exhaustive`.
This commit is contained in:
Tobin C. Harding 2024-03-01 16:22:05 +11:00
parent 162094322f
commit 2af764e859
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 9 additions and 4 deletions

View File

@ -234,12 +234,19 @@ pub trait Hash:
/// Attempted to create a hash from an invalid length slice. /// Attempted to create a hash from an invalid length slice.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct FromSliceError { pub struct FromSliceError {
expected: usize, expected: usize,
got: 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 { 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.got, self.expected) write!(f, "invalid slice length {} (expected {})", self.got, self.expected)
@ -247,9 +254,7 @@ impl fmt::Display for FromSliceError {
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl std::error::Error for FromSliceError { impl std::error::Error for FromSliceError {}
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {