pub use self::FromHexError::*;
-use std::fmt;
use std::error;
+use std::fmt;
/// A trait for converting a value to hexadecimal encoding
pub trait ToHex {
v.push(CHARS[(byte & 0xf) as usize]);
}
- unsafe {
- String::from_utf8_unchecked(v)
- }
+ unsafe { String::from_utf8_unchecked(v) }
}
}
impl fmt::Display for FromHexError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
- InvalidHexCharacter(ch, idx) =>
- write!(f, "Invalid character '{}' at position {}", ch, idx),
+ InvalidHexCharacter(ch, idx) => {
+ write!(f, "Invalid character '{}' at position {}", ch, idx)
+ }
InvalidHexLength => write!(f, "Invalid input length"),
}
}
}
-impl error::Error for FromHexError {
- fn description(&self) -> &str {
- match *self {
- InvalidHexCharacter(..) => "invalid character",
- InvalidHexLength => "invalid length",
- }
- }
-}
-
+impl error::Error for FromHexError {}
impl FromHex for str {
/// Converts any hexadecimal encoded string (literal, `@`, `&`, or `~`)
b'A'..=b'F' => buf |= byte - b'A' + 10,
b'a'..=b'f' => buf |= byte - b'a' + 10,
b'0'..=b'9' => buf |= byte - b'0',
- b' '|b'\r'|b'\n'|b'\t' => {
+ b' ' | b'\r' | b'\n' | b'\t' => {
buf >>= 4;
- continue
+ continue;
}
_ => {
let ch = self[idx..].chars().next().unwrap();
- return Err(InvalidHexCharacter(ch, idx))
+ return Err(InvalidHexCharacter(ch, idx));
}
}