hashes: Hide innards of FromSliceError
In preparation for releasing 1.0 we want to hide all error internals. Do so for the single error type in the `hashes` crate.
This commit is contained in:
parent
5232bba62b
commit
7652d0ddfc
|
@ -6,22 +6,26 @@ use core::fmt;
|
||||||
|
|
||||||
/// 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)]
|
||||||
pub struct FromSliceError {
|
pub struct FromSliceError(pub(crate) FromSliceErrorInner);
|
||||||
|
|
||||||
|
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) expected: usize,
|
||||||
pub(crate) got: usize,
|
pub(crate) 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.0.got, self.0.expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue