nandi/rustnimpublic Fork 0
ff34e1b3229df6e21b0c5d77053bb9b7364db5cd
Commits
Clone
git clone https://git.rickub.com/nandi/rustnim.git
git clone ssh://git@rickub.com/nandi/rustnim.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Add display.rs and the alloc half: all of base16ct now goes through afb2a6e · on ff34e1b3229df6e21b0c5d77053bb9b7364db5cd · nandithebull · 18h ago
display.rs · 35 lines · 864 BRust Blame HistoryRaw
 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
use core::fmt;

/// `core::fmt` presenter for binary data encoded as hexadecimal (Base16).
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct HexDisplay<'a>(pub &'a [u8]);

impl fmt::Display for HexDisplay<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{self:X}")
    }
}

impl fmt::UpperHex for HexDisplay<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut hex = [0u8; 2];

        for &byte in self.0 {
            f.write_str(crate::upper::encode_str(&[byte], &mut hex)?)?;
        }

        Ok(())
    }
}

impl fmt::LowerHex for HexDisplay<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut hex = [0u8; 2];

        for &byte in self.0 {
            f.write_str(crate::lower::encode_str(&[byte], &mut hex)?)?;
        }

        Ok(())
    }
}