pub trait DisplayHex: Copy + IsRef {
    type Display: Display + Debug + LowerHex + UpperHex;

    // Required method
    fn as_hex(self) -> Self::Display;

    // Provided methods
    fn to_lower_hex_string(self) -> String { ... }
    fn to_upper_hex_string(self) -> String { ... }
    fn to_hex_string(self, case: Case) -> String { ... }
    fn append_hex_to_string(self, case: Case, string: &mut String) { ... }
    fn hex_reserve_suggestion(self) -> usize { ... }
}
Expand description

Extension trait for types that can be displayed as hex.

Types that have a single, obvious text representation being hex should not implement this trait and simply implement Display instead.

This trait should be generally implemented for references only. We would prefer to use GAT but that is beyond our MSRV. As a lint we require the IsRef trait which is implemented for all references.

Required Associated Types§

source

type Display: Display + Debug + LowerHex + UpperHex

The type providing fmt::Display implementation.

This is usually a wrapper type holding a reference to Self.

Required Methods§

source

fn as_hex(self) -> Self::Display

Display Self as a continuous sequence of ASCII hex chars.

Provided Methods§

source

fn to_lower_hex_string(self) -> String

Create a lower-hex-encoded string.

A shorthand for to_hex_string(Case::Lower), so that Case doesn’t need to be imported.

This may be faster than .display_hex().to_string() because it uses reserve_suggestion.

Examples found in repository?
examples/custom.rs (line 21)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fn main() {
    let s = "deadbeefcafebabedeadbeefcafebabedeadbeefcafebabedeadbeefcafebabe";
    println!("Parse from hex: {}", s);

    let hexy = ALittleBitHexy::from_hex(s).expect("the correct number of valid hex digits");
    println!("Display ALittleBitHexy as string: {}", hexy);
    println!("Display ALittleBitHexy as a hex: {:x}", hexy.as_hex());

    #[cfg(feature = "alloc")]
    {
        let hex = hexy.to_lower_hex_string();
        let from_hex = ALittleBitHexy::from_hex(&hex).expect("failed to parse hex");
        assert_eq!(from_hex, hexy);
    }
}
More examples
Hide additional examples
examples/wrap_array_display_hex_trait.rs (line 39)
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
36
37
38
39
40
41
42
43
fn main() {
    let hex = "00000000cafebabedeadbeefcafebabedeadbeefcafebabedeadbeefcafebabe";
    println!("\nParse from hex: {}\n", hex);

    let array = <[u8; 32] as FromHex>::from_hex(hex).expect("failed to parse array");
    let wrap = Wrap::from_hex(hex).expect("failed to parse wrapped array");

    println!("Print an array using traits from the standard libraries `fmt` module along with the provided implementation of `DisplayHex`:\n");
    println!("LowerHex: {:x}", array.as_hex());
    println!("UpperHex: {:X}", array.as_hex());
    println!("Display: {}", array.as_hex());
    println!("Debug: {:?}", array.as_hex());
    println!("Debug pretty: {:#?}", array.as_hex());

    println!("\n");

    println!(
        "Print the wrapped array directly using traits from the standard libraries `fmt` module:\n"
    );
    println!("LowerHex: {:x}", wrap.as_hex());
    println!("UpperHex: {:X}", wrap.as_hex());
    println!("Display: {}", wrap.as_hex());
    println!("Debug: {:?}", wrap.as_hex());
    println!("Debug pretty: {:#?}", wrap.as_hex());

    #[cfg(feature = "alloc")]
    {
        // We cannot call `to_string` on the wrapped type to allocate a string, if you wish to
        // use that trait method see `./wrap_array_fmt_traits.rs`.
        let array_hex = array.to_lower_hex_string();
        let wrap_hex = wrap.to_lower_hex_string();
        assert_eq!(array_hex, wrap_hex);
    }
}
source

fn to_upper_hex_string(self) -> String

Create an upper-hex-encoded string.

A shorthand for to_hex_string(Case::Upper), so that Case doesn’t need to be imported.

This may be faster than .display_hex().to_string() because it uses reserve_suggestion.

source

fn to_hex_string(self, case: Case) -> String

Create a hex-encoded string.

This may be faster than .display_hex().to_string() because it uses reserve_suggestion.

source

fn append_hex_to_string(self, case: Case, string: &mut String)

Appends hex-encoded content to an existing String.

This may be faster than write!(string, "{:x}", self.as_hex()) because it uses hex_reserve_sugggestion.

source

fn hex_reserve_suggestion(self) -> usize

Hints how much bytes to reserve when creating a String.

Implementors that know the number of produced bytes upfront should override this. Defaults to 0.

Implementations on Foreign Types§

source§

impl<'a> DisplayHex for &'a [u8; 9]

source§

impl<'a> DisplayHex for &'a [u8; 5]

source§

impl<'a> DisplayHex for &'a [u8; 1024]

source§

impl<'a> DisplayHex for &'a Vec<u8>

source§

impl<'a> DisplayHex for &'a [u8; 128]

source§

impl<'a> DisplayHex for &'a [u8; 11]

source§

impl<'a> DisplayHex for &'a [u8; 16]

source§

impl<'a> DisplayHex for &'a [u8; 20]

source§

impl<'a> DisplayHex for &'a [u8; 6]

source§

impl<'a> DisplayHex for &'a [u8; 8]

source§

impl<'a> DisplayHex for &'a [u8; 14]

source§

impl<'a> DisplayHex for &'a [u8; 4096]

source§

impl<'a> DisplayHex for &'a [u8; 2048]

source§

impl<'a> DisplayHex for &'a [u8; 12]

source§

impl<'a> DisplayHex for &'a [u8; 33]

source§

impl<'a> DisplayHex for &'a [u8; 4]

source§

impl<'a> DisplayHex for &'a [u8; 256]

source§

impl<'a> DisplayHex for &'a [u8; 15]

source§

impl<'a> DisplayHex for &'a [u8; 2]

source§

impl<'a> DisplayHex for &'a [u8]

source§

impl<'a> DisplayHex for &'a [u8; 10]

source§

impl<'a> DisplayHex for &'a [u8; 7]

source§

impl<'a> DisplayHex for &'a [u8; 3]

source§

impl<'a> DisplayHex for &'a [u8; 1]

source§

impl<'a> DisplayHex for &'a [u8; 13]

source§

impl<'a> DisplayHex for &'a [u8; 512]

source§

impl<'a> DisplayHex for &'a [u8; 32]

source§

impl<'a> DisplayHex for &'a [u8; 65]

source§

impl<'a> DisplayHex for &'a [u8; 64]

Implementors§