Skip to content

Commit

Permalink
Implement ToHexPrefixed for &[u8;N] (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
grtlr committed Mar 17, 2022
1 parent 05aa8d7 commit 62d8df7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
6 changes: 6 additions & 0 deletions prefix-hex/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## 0.2.0 - 2022-03-17

### Added

- Implementation of `ToHexPrefixed` for `&[u8;N]`;

## 0.1.0 - 2022-03-17

### Added
Expand Down
2 changes: 1 addition & 1 deletion prefix-hex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prefix-hex"
version = "0.1.0"
version = "0.2.0"
authors = [ "IOTA Stiftung" ]
edition = "2021"
description = "Encoding and decoding of hex strings with 0x prefix."
Expand Down
9 changes: 9 additions & 0 deletions prefix-hex/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ where
}
}

impl<const N: usize> ToHexPrefixed for &[u8; N]
where
[u8; N]: hex::ToHex,
{
fn to_hex_prefixed(self) -> String {
format!("0x{}", hex::encode(self))
}
}

impl ToHexPrefixed for &[u8] {
fn to_hex_prefixed(self) -> String {
format!("0x{}", hex::encode(self))
Expand Down
8 changes: 8 additions & 0 deletions prefix-hex/tests/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ fn array_encode() {
);
}

#[test]
fn array_reference_encode() {
assert_eq!(
prefix_hex::encode(&[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]),
"0x0123456789abcdef"
);
}

#[test]
fn vec_decode() {
assert_eq!(prefix_hex::decode::<Vec<u8>>("0x000102").unwrap(), [0x0, 0x1, 0x2]);
Expand Down

0 comments on commit 62d8df7

Please sign in to comment.