diff --git a/src/error.rs b/src/error.rs index 3541f60..956184d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -19,6 +19,7 @@ use core::fmt; /// Crate error type. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[non_exhaustive] pub enum Error { /// Tried to create a fixed-length hash from a slice with the wrong size (expected, got). InvalidLength(usize, usize), @@ -31,4 +32,3 @@ impl fmt::Display for Error { } } } - diff --git a/src/hex.rs b/src/hex.rs index 7ae2312..aeb6c6d 100644 --- a/src/hex.rs +++ b/src/hex.rs @@ -30,6 +30,7 @@ use crate::Hash; /// Hex decoding error. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[non_exhaustive] pub enum Error { /// Non-hexadecimal character. InvalidChar(u8), @@ -142,24 +143,6 @@ impl<'a> Iterator for HexIterator<'a> { } } -#[cfg(any(feature = "std", feature = "core2"))] -#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "core2"))))] -impl<'a> io::Read for HexIterator<'a> { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - let mut bytes_read = 0usize; - for dst in buf { - match self.next() { - Some(Ok(src)) => { - *dst = src; - bytes_read += 1; - }, - _ => break, - } - } - Ok(bytes_read) - } -} - impl<'a> DoubleEndedIterator for HexIterator<'a> { fn next_back(&mut self) -> Option> { let lo = self.iter.next_back()?; diff --git a/src/impls.rs b/src/impls.rs index b7a7ff7..60c269a 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -26,17 +26,39 @@ use core2::{error, io}; use crate::{Error, HashEngine, hex, sha1, sha256, sha512, ripemd160, siphash24, hmac}; impl error::Error for Error { - #[cfg(feature = "std")] - fn cause(&self) -> Option<&error::Error> { None } - #[cfg(feature = "std")] - fn description(&self) -> &str { "`std::error::description` is deprecated" } + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + use Error::*; + + match *self { + InvalidLength(_, _) => None + } + } } impl error::Error for hex::Error { - #[cfg(feature = "std")] - fn cause(&self) -> Option<&error::Error> { None } - #[cfg(feature = "std")] - fn description(&self) -> &str { "`std::error::description` is deprecated" } + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + use hex::Error::*; + + match *self { + InvalidChar(_) | OddLengthString(_) | InvalidLength(_, _) => None + } + } +} + +impl<'a> io::Read for hex::HexIterator<'a> { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let mut bytes_read = 0usize; + for dst in buf { + match self.next() { + Some(Ok(src)) => { + *dst = src; + bytes_read += 1; + }, + _ => break, + } + } + Ok(bytes_read) + } } impl io::Write for sha1::HashEngine {