Skip to main content

HexDisplayExt

Trait HexDisplayExt 

Source
pub trait HexDisplayExt: HexSealed {
    // Required method
    fn hex(&self) -> Hex<'_>;

    // Provided methods
    fn to_upper_hex_string(&self) -> String { ... }
    fn to_hex_string(&self) -> String { ... }
    fn to_upper_hex_dump(&self) -> String { ... }
    fn to_hex_dump(&self) -> String { ... }
}
Expand description

An extension trait that allows for more easily constructing Hex values

use hex_display::HexDisplayExt;
assert_eq!(
    format!("{}", [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef].hex()),
    "0123456789abcdef"
);
assert_eq!(
    format!("{:X}", [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef].hex()),
    "0123456789ABCDEF"
);

The formatter’s width/alignment/fill are honored, so the output can be padded like any other Display value:

use hex_display::HexDisplayExt;
assert_eq!(format!("{:>8}", [0x01, 0x23].hex()), "    0123");
assert_eq!(format!("{:*^8}", [0x01, 0x23].hex()), "**0123**");

Use the alternate form (#) to emit a multiline hexdump -C-style dump with an offset column and ASCII gutter:

use hex_display::HexDisplayExt;
assert_eq!(
    format!("{:#}", b"Hello, world!\n".hex()),
    "00000000  48 65 6c 6c 6f 2c 20 77  6f 72 6c 64 21 0a        |Hello, world!.|"
);

See the documentation for Hex for more details.

This trait is sealed: downstream crates cannot implement it, since the blanket impl for T: AsRef<[u8]> + ?Sized already covers every reasonable input.

Required Methods§

Source

fn hex(&self) -> Hex<'_>

Display as a hexdump

Provided Methods§

Source

fn to_upper_hex_string(&self) -> String

Convert to a upper-case hex string

Only present when built with alloc support.

Source

fn to_hex_string(&self) -> String

Convert to a lower-case hex string

Only present when built with alloc support.

Source

fn to_upper_hex_dump(&self) -> String

Convert to a upper-case hexdump.

Only present when built with alloc support.

Source

fn to_hex_dump(&self) -> String

Convert to a lower-case hexdump.

Only present when built with alloc support.

Implementors§

Source§

impl<T: AsRef<[u8]> + ?Sized> HexDisplayExt for T