Skip to content

Commit 697abee

Browse files
committed
feat: simplify hex-display
1 parent 6576c83 commit 697abee

File tree

5 files changed

+33
-18
lines changed

5 files changed

+33
-18
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ jobs:
4343
- macos-latest
4444
- windows-latest
4545
rust:
46-
- "1.86"
47-
- "1.87"
4846
- "1.88"
4947
- "1.89"
5048
- "1.90"

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ readme = "README.md"
1515
keywords = ["git", "git-remote", "codecommit"]
1616
categories = ["command-line-utilities", "development-tools"]
1717
publish = true
18-
rust-version = "1.86"
18+
rust-version = "1.88"
1919

2020
[workspace.lints.rust]
2121
let_underscore_drop = "warn"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Actively used on Windows, macOS, and low-resource Linux environments.
3939

4040
### MSRV
4141

42-
The minimum supported Rust version is `1.86`.
42+
The minimum supported Rust version is `1.88`.
4343

4444
## Install
4545

clippy.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# See https://doc.rust-lang.org/clippy/lint_configuration.html
22
# for full configuration options.
33

4-
msrv = "1.86"
4+
msrv = "1.88"
Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
1-
pub struct HexDisplay<T>(T);
1+
pub struct HexDisplay([u8; 32]);
22

3-
impl<T: AsRef<[u8]>> core::fmt::Display for HexDisplay<T> {
3+
impl core::fmt::Debug for HexDisplay {
44
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
5-
for b in self.0.as_ref() {
6-
write!(f, "{b:02x}")?;
7-
}
8-
Ok(())
5+
std::fmt::Display::fmt(self, f)
96
}
107
}
118

12-
pub trait HexDisplayExt: AsRef<[u8]> {
13-
fn hex_display(self) -> HexDisplay<Self>
14-
where
15-
Self: Sized,
16-
{
17-
HexDisplay(self)
9+
impl core::fmt::Display for HexDisplay {
10+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
11+
const CHARSET: &[u8; 16] = b"0123456789abcdef";
12+
13+
let mut buf = [0u8; 64];
14+
// SAFETY: 64 is evenly divisible by 2
15+
unsafe { buf.as_chunks_unchecked_mut::<2>() }
16+
.iter_mut()
17+
.zip(self.0.as_ref())
18+
.for_each(|(slot, &byte)| {
19+
slot[0] = CHARSET[(byte >> 4) as usize];
20+
slot[1] = CHARSET[(byte & 0x0F) as usize];
21+
});
22+
23+
// SAFETY: buf only contains valid ASCII hex characters
24+
let buf = unsafe { core::str::from_utf8_unchecked(&buf) };
25+
26+
f.pad(buf)
1827
}
1928
}
2029

21-
impl<T: AsRef<[u8]>> HexDisplayExt for T {}
30+
pub trait HexDisplayExt {
31+
fn hex_display(self) -> HexDisplay;
32+
}
33+
34+
impl<T: Into<[u8; 32]>> HexDisplayExt for T {
35+
fn hex_display(self) -> HexDisplay {
36+
HexDisplay(self.into())
37+
}
38+
}

0 commit comments

Comments
 (0)