hashes: Implement std::error::Error

We have old Rust 1.29 error handling code still in `hashes`. Implement
`std::error::Error` for the `hex::Error` and `error::Error` types in
line with "modern" Rust 1.41.1 error handling.
This commit is contained in:
Tobin C. Harding 2023-01-20 11:10:51 +11:00
parent 5e3abc5e11
commit 7e85452cd9
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
3 changed files with 27 additions and 17 deletions

View File

@ -31,3 +31,15 @@ impl fmt::Display for Error {
}
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use self::Error::*;
match self {
InvalidLength(_, _) => None,
}
}
}

View File

@ -47,6 +47,18 @@ impl fmt::Display for Error {
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use self::Error::*;
match self {
InvalidChar(_) | OddLengthString(_) | InvalidLength(_, _) => None,
}
}
}
/// Trait for objects that can be deserialized from hex strings.
pub trait FromHex: Sized {
/// Produces an object from a byte iterator.

View File

@ -18,26 +18,12 @@
//!
#[cfg(feature = "std")]
use std::{error, io};
use std::io;
#[cfg(not(feature = "std"))]
use core2::{error, io};
use core2::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" }
}
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" }
}
use crate::{HashEngine, sha1, sha256, sha512, ripemd160, siphash24, hmac};
impl io::Write for sha1::HashEngine {
fn flush(&mut self) -> io::Result<()> { Ok(()) }