Skip to main content

DisplayHex

Trait DisplayHex 

Source
pub trait DisplayHex {
    type Display<'a>: Display + Debug + LowerHex + UpperHex
       where Self: 'a;

    // Required methods
    fn as_hex<'a>(&'a self) -> Self::Display<'a>;
    fn hex_reserve_suggestion(&self) -> usize;

    // 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<'a>(&'a self, case: Case, string: &mut String) { ... }
}
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.

Required Associated Types§

Source

type Display<'a>: Display + Debug + LowerHex + UpperHex where Self: 'a

The type providing fmt::Display implementation.

This is a wrapper type holding a reference to Self.

Required Methods§

Source

fn as_hex<'a>(&'a self) -> Self::Display<'a>

Display Self as a continuous sequence of ASCII hex chars.

Source

fn hex_reserve_suggestion(&self) -> usize

Hints how many bytes to reserve when creating a String.

If you don’t know you can just return 0 and take the perf hit.

Provided Methods§

Source

fn to_lower_hex_string(&self) -> String

Available on crate feature alloc only.

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/wrap_array.rs (line 39)
12fn main() {
13    let hex = "deadbeefcafebabedeadbeefcafebabedeadbeefcafebabedeadbeefcafebabe";
14    println!("\nParse from hex: {}\n", hex);
15
16    let array = hex::decode_to_array::<32>(hex).expect("failed to parse array");
17    let wrap = Wrap::from_str(hex).expect("failed to parse wrapped array from hex string");
18
19    println!("Print an array using traits from the standard libraries `fmt` module along with the provided implementation of `DisplayHex`:\n");
20    println!("LowerHex: {:x}", array.as_hex());
21    println!("UpperHex: {:X}", array.as_hex());
22    println!("Display: {}", array.as_hex());
23    println!("Debug: {:?}", array.as_hex());
24    println!("Debug pretty: {:#?}", array.as_hex());
25
26    println!("\n");
27
28    println!(
29        "Print the wrapped array directly using traits from the standard libraries `fmt` module:\n"
30    );
31    println!("LowerHex: {:x}", wrap);
32    println!("UpperHex: {:X}", wrap);
33    println!("Display: {}", wrap);
34    println!("Debug: {:?}", wrap);
35    println!("Debug pretty: {:#?}", wrap);
36
37    #[cfg(feature = "alloc")]
38    {
39        let array_hex = array.to_lower_hex_string();
40        let other = array.as_hex().to_string();
41        assert_eq!(array_hex, other);
42
43        let wrap_hex = wrap.to_string();
44        assert_eq!(array_hex, wrap_hex);
45    }
46}
Source

fn to_upper_hex_string(&self) -> String

Available on crate feature alloc only.

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

Available on crate feature alloc only.

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<'a>(&'a self, case: Case, string: &mut String)

Available on crate feature alloc only.

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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl DisplayHex for [u8]

Source§

type Display<'a> = DisplayByteSlice<'a>

Source§

fn as_hex<'a>(&'a self) -> Self::Display<'a>

Source§

fn hex_reserve_suggestion(&self) -> usize

Implementors§