Skip to main content

fmt_hex_max

Macro fmt_hex_max 

Source
macro_rules! fmt_hex_max {
    ($formatter:expr, $len:expr, $bytes:expr, $case:expr) => { ... };
}
Expand description

Efficiently formats a sequence of bytes as a hexadecimal string with compile-time-known length.

Whenever the length of a sequence of bytes (usually an array, but it could be any iterator) is known at compile time, it is more efficient to use this macro because various length checks and loops are elided. The buffer is just filled once and then emitted to the formatter.

This supports all formatting options of formatter and may be faster than calling as_hex() on an arbitrary &[u8]. Note that the implementation intentionally keeps leading zeros even when not requested. This is designed to display values such as hashes and keys and removing leading zeros would be confusing.

Note that the bytes parameter is IntoIterator this means that if you would like to do some manipulation to the byte array before formatting then you can. For example bytes.iter().rev() to print the array backwards.

§Parameters

  • $formatter - a fmt::Formatter.
  • $len known length of $bytes, must be a const expression.
  • $bytes - bytes to be encoded, most likely a reference to an array.
  • $case - value of type Case determining whether to format as lower or upper case.

§Returns

Returns core::fmt::Result.

§Panics

This macro panics if the length of the encoded item is larger than $len.

§Static Assertions

The use of a macro instead of a function allows for compile-time length validation. This macro fails to compile if $len is more than half of usize::MAX. This prevents runtime panics or logic errors when formatting fixed-size primitives like Bitcoin hashes or public keys.

§Examples

use hex_conservative::{fmt_hex_max, Case};
use std::fmt;

struct MyHash([u8; 32]);

impl fmt::LowerHex for MyHash {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Explicitly use Lower case for {:x}
        fmt_hex_max!(f, 32, &self.0, Case::Lower)
    }
}

impl fmt::UpperHex for MyHash {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Explicitly use Upper case for {:X}
        fmt_hex_max!(f, 32, &self.0, Case::Upper)
    }
}