Function linera_base::hex_debug

source ·
pub fn hex_debug<T: AsRef<[u8]>>(bytes: &T, f: &mut Formatter<'_>) -> Result
Expand description

Formats a byte sequence as a hexadecimal string, and elides bytes in the middle if it is longer than 32 bytes.

This function is intended to be used with the #[debug(with = "hex_debug")] field annotation of custom_debug_derive::Debug.

§Examples

use custom_debug_derive::Debug;

#[derive(Debug)]
struct Message {
    #[debug(with = "hex_debug")]
    bytes: Vec<u8>,
}

let msg = Message {
    bytes: vec![0x12, 0x34, 0x56, 0x78],
};

assert_eq!(format!("{:?}", msg), "Message { bytes: 12345678 }");

let long_msg = Message {
    bytes: b"        10        20        30        40        50".to_vec(),
};

assert_eq!(
    format!("{:?}", long_msg),
    "Message { bytes: 20202020202020203130202020202020..20202020343020202020202020203530 }"
);