Debian Codegraph @

debian/rust-vendor/hex-literal/README.md

raw
1 # [RustCrypto]: hex-literal
2
3 [![Crate][crate-image]][crate-link]
4 [![Docs][docs-image]][docs-link]
5 ![Apache 2.0/MIT Licensed][license-image]
6 ![MSRV][rustc-image]
7 [![Build Status][build-image]][build-link]
8
9 This crate provides the `hex!` macro for converting a sequence of hexadecimal string literals to a byte array at compile time.
10
11 The macro accepts the following characters in input string literals:
12
13 - `'0'...'9'`, `'a'...'f'`, `'A'...'F'` — hex characters which will be used in construction of the output byte array
14 - `' '`, `:`, `'\r'`, `'\n'`, `'\t'` — formatting characters which will be ignored
15
16 # Examples
17 ```rust
18 use hex_literal::hex;
19
20 // The macro can be used in const contexts
21 const DATA: [u8; 4] = hex!("01020304");
22 assert_eq!(DATA, [1, 2, 3, 4]);
23
24 // Both upper and lower hex values are supported
25 assert_eq!(hex!("a1 b2 c3 d4"), [0xA1, 0xB2, 0xC3, 0xD4]);
26 assert_eq!(hex!("E5 E6 90 92"), [0xE5, 0xE6, 0x90, 0x92]);
27 assert_eq!(hex!("0a0B 0C0d"), [10, 11, 12, 13]);
28
29 // Colon-delimited literals
30 assert_eq!(hex!("0A:0B:0C:0D"), [10, 11, 12, 13]);
31
32 // Multi-line literals
33 let bytes1 = hex!("
34 00010203 04050607
35 08090a0b 0c0d0e0f
36 ");
37 assert_eq!(
38 bytes1,
39 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
40 );
41
42 // It's possible to use several literals
43 // (results will be concatenated)
44 let bytes2 = hex!(
45 "00010203 04050607" // first half
46 "08090a0b 0c0d0e0f" // second half
47 );
48 assert_eq!(bytes1, bytes2);
49 ```
50
51 Using an unsupported character inside literals will result in a compilation error:
52 ```rust,compile_fail
53 hex_literal::hex!("АА"); // Cyrillic "А"
54 hex_literal::hex!("11 22"); // Japanese space
55 ```
56
57 Сomments inside literals are not supported:
58 ```rust,compile_fail
59 hex_literal::hex!("0123 // foo");
60 ```
61
62 Each literal must contain an even number of hex characters:
63 ```rust,compile_fail
64 hex_literal::hex!(
65 "01234"
66 "567"
67 );
68 ```
69
70 ## License
71
72 Licensed under either of:
73
74 * [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
75 * [MIT license](http://opensource.org/licenses/MIT)
76
77 at your option.
78
79 ### Contribution
80
81 Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
82
83 [//]: # (badges)
84
85 [crate-image]: https://img.shields.io/crates/v/hex-literal.svg
86 [crate-link]: https://crates.io/crates/hex-literal
87 [docs-image]: https://docs.rs/hex-literal/badge.svg
88 [docs-link]: https://docs.rs/hex-literal/
89 [license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
90 [rustc-image]: https://img.shields.io/badge/rustc-1.85+-blue.svg
91 [build-image]: https://github.com/RustCrypto/utils/actions/workflows/hex-literal.yml/badge.svg?branch=master
92 [build-link]: https://github.com/RustCrypto/utils/actions/workflows/hex-literal.yml?query=branch:master
93
94 [//]: # (general links)
95
96 [RustCrypto]: https://github.com/RustCrypto