Macro hex_magic::hex[][src]

hex!() { /* proc-macro */ }

Macro which converts string literals ("7D2B") to byte arrays ([0x7D, 0x2B]) at compile time.

It’s a rewrite of the hex! macro provided by the hex-literal crate with stricter rules requiring bytes to come in pairs (so "12 34" is allowed but "1 2 3 4" is not) and with the addition of being able to parse __ and .. to create match patterns.

It accepts the following characters in the input string:

  • '0'...'9', 'a'...'f', 'A'...'F' – hex characters which will be used in construction of the output byte array
  • ' ', '\r', '\n', '\t' – formatting characters which will be ignored
  • '_', '.' – formatting characters which will be used to create match patterns

Example

use hex_magic::hex;

const BYTES: [u8; 3] = hex!("DEAD AF");

fn main() {
    assert_eq!(BYTES, [0xDE, 0xAD, 0xAF]);
    assert_eq!(hex!("aA aa aA Aa aa"), [0xAA; 5]);

    match [1, 2, 3, 4] {
        hex!("AABBCCDD") => panic!("bytes don't match at all"),
        hex!("01__FF__") => panic!("[1, _, 0xFF, _] does not match"),
        hex!("01..04") => println!("[1, .., 4] would match"),
        hex!("..") => unreachable!("[..] would match"),
    }
}