|
| 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 hexadecimal string literals to a byte array at compile time. |
| 10 | + |
| 11 | +It accepts the following characters in the input string: |
| 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 | +// Multi-line literals |
| 30 | +let bytes1 = hex!(" |
| 31 | + 00010203 04050607 |
| 32 | + 08090a0b 0c0d0e0f |
| 33 | +"); |
| 34 | +assert_eq!(bytes1, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]); |
| 35 | + |
| 36 | +// It's possible to use several literals (results will be concatenated) |
| 37 | +let bytes2 = hex!( |
| 38 | + "00010203 04050607" // first half |
| 39 | + "08090a0b" /* block comment */ "0c0d0e0f" // second half |
| 40 | +); |
| 41 | +assert_eq!(bytes1, bytes2); |
| 42 | +``` |
| 43 | + |
| 44 | +Using an unsupported character inside literals will result in a compilation error: |
| 45 | +```rust,compile_fail |
| 46 | +# use hex_literal::hex; |
| 47 | +hex!("АА"); // Cyrillic "А" |
| 48 | +hex!("11 22"); // Japanese space |
| 49 | +hex!("0123 // Сomments inside literals are not supported"); |
| 50 | +``` |
| 51 | + |
| 52 | +## Minimum Supported Rust Version |
| 53 | + |
| 54 | +Rust **1.57** or newer. |
| 55 | + |
| 56 | +In the future, we reserve the right to change MSRV (i.e. MSRV is out-of-scope for this crate's SemVer guarantees), however when we do it will be accompanied by a minor version bump. |
| 57 | + |
| 58 | +## License |
| 59 | + |
| 60 | +Licensed under either of: |
| 61 | + |
| 62 | +* [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) |
| 63 | +* [MIT license](http://opensource.org/licenses/MIT) |
| 64 | + |
| 65 | +at your option. |
| 66 | + |
| 67 | +### Contribution |
| 68 | + |
| 69 | +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. |
| 70 | + |
| 71 | +[//]: # (badges) |
| 72 | + |
| 73 | +[crate-image]: https://img.shields.io/crates/v/hex-literal.svg |
| 74 | +[crate-link]: https://crates.io/crates/hex-literal |
| 75 | +[docs-image]: https://docs.rs/hex-literal/badge.svg |
| 76 | +[docs-link]: https://docs.rs/hex-literal/ |
| 77 | +[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg |
| 78 | +[rustc-image]: https://img.shields.io/badge/rustc-1.57+-blue.svg |
| 79 | +[build-image]: https://github.com/RustCrypto/utils/actions/workflows/hex-literal.yml/badge.svg |
| 80 | +[build-link]: https://github.com/RustCrypto/utils/actions/workflows/hex-literal.yml |
| 81 | + |
| 82 | +[//]: # (general links) |
| 83 | + |
| 84 | +[RustCrypto]: https://github.com/RustCrypto |
0 commit comments