1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use alloc::{boxed::Box, format, string::String, vec::Vec};
use crate::{strip_prefix, Error, FromHexPrefixed, ToHexPrefixed};
impl FromHexPrefixed for Vec<u8> {
fn from_hex_prefixed(hex: &str) -> Result<Self, Error> {
let hex = strip_prefix(hex)?;
hex::decode(hex).map_err(Into::into)
}
}
impl ToHexPrefixed for Vec<u8> {
fn to_hex_prefixed(self) -> String {
format!("0x{}", hex::encode(self))
}
}
impl<const N: usize> FromHexPrefixed for [u8; N]
where
Self: hex::FromHex,
{
fn from_hex_prefixed(hex: &str) -> Result<Self, Error> {
let hex = strip_prefix(hex)?;
let mut buffer = [0; N];
hex::decode_to_slice(hex, &mut buffer).map_err(|e| match e {
hex::FromHexError::InvalidStringLength | hex::FromHexError::OddLength => Error::InvalidStringLengthSlice {
expected: N * 2,
actual: hex.len(),
},
_ => e.into(),
})?;
Ok(buffer)
}
}
impl<const N: usize> ToHexPrefixed for [u8; N]
where
Self: hex::ToHex,
{
fn to_hex_prefixed(self) -> String {
format!("0x{}", hex::encode(self))
}
}
impl<const N: usize> ToHexPrefixed for &[u8; N]
where
[u8; N]: hex::ToHex,
{
fn to_hex_prefixed(self) -> String {
format!("0x{}", hex::encode(self))
}
}
macro_rules! impl_for_as_ref_type {
($type:ty) => {
impl ToHexPrefixed for $type {
fn to_hex_prefixed(self) -> String {
format!("0x{}", hex::encode(self))
}
}
};
}
impl_for_as_ref_type!(Box<[u8]>);
impl_for_as_ref_type!(&Box<[u8]>);
impl_for_as_ref_type!(&[u8]);