Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Update error implementations for the new MSRV #155

Merged
merged 4 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -31,4 +32,3 @@ impl fmt::Display for Error {
}
}
}

19 changes: 1 addition & 18 deletions src/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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<usize> {
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<Result<u8, Error>> {
let lo = self.iter.next_back()?;
Expand Down
38 changes: 30 additions & 8 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> {
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 {
Expand Down