pub trait FromHex: Sized {
    type Err: From<HexToBytesError> + Sized + Debug + Display;

    // Required method
    fn from_byte_iter<I>(iter: I) -> Result<Self, Self::Err>
       where I: Iterator<Item = Result<u8, HexToBytesError>> + ExactSizeIterator + DoubleEndedIterator;

    // Provided method
    fn from_hex(s: &str) -> Result<Self, Self::Err> { ... }
}
Expand description

Trait for objects that can be deserialized from hex strings.

Required Associated Types§

source

type Err: From<HexToBytesError> + Sized + Debug + Display

Error type returned while parsing hex string.

Required Methods§

source

fn from_byte_iter<I>(iter: I) -> Result<Self, Self::Err>where I: Iterator<Item = Result<u8, HexToBytesError>> + ExactSizeIterator + DoubleEndedIterator,

Produces an object from a byte iterator.

Provided Methods§

source

fn from_hex(s: &str) -> Result<Self, Self::Err>

Produces an object from a hex string.

Examples found in repository?
examples/hexy.rs (line 17)
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
44
45
46
47
48
49
50
51
fn main() {
    let s = "deadbeefcafebabedeadbeefcafebabedeadbeefcafebabedeadbeefcafebabe";
    println!("Parse hex from string:  {}", s);

    let hexy = Hexy::from_hex(s).expect("the correct number of valid hex digits");
    let display = format!("{}", hexy);
    println!("Display Hexy as string: {}", display);

    assert_eq!(display, s);
}

/// A struct that always uses hex when in string form.
pub struct Hexy {
    // Some opaque data.
    data: [u8; 32],
}

impl Hexy {
    /// Demonstrates getting internal opaque data as a byte slice.
    pub fn as_bytes(&self) -> &[u8] { &self.data }
}

impl fmt::Debug for Hexy {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        fmt::Formatter::debug_struct(f, "Hexy").field("data", &self.data.as_hex()).finish()
    }
}

// We implement `Display`/`FromStr` using `LowerHex`/`FromHex` respectively, if hex was not the
// natural representation for this type this would not be the case.

impl fmt::Display for Hexy {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(self, f) }
}

impl FromStr for Hexy {
    type Err = HexToArrayError;

    fn from_str(s: &str) -> Result<Self, Self::Err> { Hexy::from_hex(s) }
More examples
Hide additional examples
examples/custom.rs (line 15)
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);
    }
}
examples/wrap_array_fmt_traits.rs (line 16)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
fn main() {
    let hex = "deadbeefcafebabedeadbeefcafebabedeadbeefcafebabedeadbeefcafebabe";
    println!("\nParse from hex: {}\n", hex);

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

    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);
    println!("UpperHex: {:X}", wrap);
    println!("Display: {}", wrap);
    println!("Debug: {:?}", wrap);
    println!("Debug pretty: {:#?}", wrap);

    // We cannot call `to_lower_hex_string` on the wrapped type to allocate a string, if you wish to
    // use that trait method see `./wrap_array_display_hex_trait.rs`.
    let array_hex = array.as_hex().to_string();
    let wrap_hex = wrap.to_string();
    assert_eq!(array_hex, wrap_hex);
}

pub struct Wrap([u8; 32]);

impl fmt::Debug for Wrap {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        fmt::Formatter::debug_tuple(f, "Wrap").field(&self.0.as_hex()).finish()
    }
}

impl fmt::Display for Wrap {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        fmt::Display::fmt(&self.0.as_hex(), f)
    }
}

impl fmt::LowerHex for Wrap {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        fmt::LowerHex::fmt(&self.0.as_hex(), f)
    }
}

impl fmt::UpperHex for Wrap {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        fmt::LowerHex::fmt(&self.0.as_hex(), f)
    }
}

impl FromStr for Wrap {
    type Err = HexToArrayError;
    fn from_str(s: &str) -> Result<Self, Self::Err> { Ok(Self(FromHex::from_hex(s)?)) }
examples/wrap_array_display_hex_trait.rs (line 14)
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);
    }
}

Implementations on Foreign Types§

source§

impl FromHex for [u8; 16]

source§

impl FromHex for [u8; 65]

source§

impl FromHex for [u8; 256]

source§

impl FromHex for [u8; 24]

source§

impl FromHex for [u8; 2]

source§

impl FromHex for [u8; 28]

source§

impl FromHex for [u8; 14]

source§

impl FromHex for [u8; 128]

source§

impl FromHex for [u8; 33]

source§

impl FromHex for [u8; 4]

source§

impl FromHex for [u8; 512]

source§

impl FromHex for [u8; 64]

source§

impl FromHex for Vec<u8>

source§

impl FromHex for [u8; 10]

source§

impl FromHex for [u8; 6]

source§

impl FromHex for [u8; 8]

source§

impl FromHex for [u8; 20]

source§

impl FromHex for [u8; 12]

source§

impl FromHex for [u8; 32]

source§

impl FromHex for [u8; 384]

Implementors§