Skip to content

Commit d64eeaa

Browse files
committed
hex-literal: enforce const evaluation
1 parent 13385f6 commit d64eeaa

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

Cargo.lock

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hex-literal/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## 0.4.1 (2023-04-05)
8+
### Changed
9+
- Enforce const evaluation ([#886])
10+
11+
[#886]: https://github.com/RustCrypto/utils/pull/886
12+
713
## 0.4.0 (2023-04-02)
814
### Changed
915
- Disallow comments inside hex strings ([#816])

hex-literal/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hex-literal"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
authors = ["RustCrypto Developers"]
55
license = "MIT OR Apache-2.0"
66
description = "Macro for converting hexadecimal string to a byte array at compile time"

hex-literal/README.md

+22-7
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,37 @@ let bytes1 = hex!("
3131
00010203 04050607
3232
08090a0b 0c0d0e0f
3333
");
34-
assert_eq!(bytes1, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
34+
assert_eq!(
35+
bytes1,
36+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
37+
);
3538

36-
// It's possible to use several literals (results will be concatenated)
39+
// It's possible to use several literals
40+
// (results will be concatenated)
3741
let bytes2 = hex!(
3842
"00010203 04050607" // first half
39-
"08090a0b" /* block comment */ "0c0d0e0f" // second half
43+
"08090a0b 0c0d0e0f" // second half
4044
);
4145
assert_eq!(bytes1, bytes2);
4246
```
4347

4448
Using an unsupported character inside literals will result in a compilation error:
4549
```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+
hex_literal::hex!("АА"); // Cyrillic "А"
51+
hex_literal::hex!("11 22"); // Japanese space
52+
```
53+
54+
Сomments inside literals are not supported:
55+
```rust,compile_fail
56+
hex_literal::hex!("0123 // foo");
57+
```
58+
59+
Each literal must contain an even number of hex characters:
60+
```rust,compile_fail
61+
hex_literal::hex!(
62+
"01234"
63+
"567"
64+
);
5065
```
5166

5267
## Minimum Supported Rust Version

hex-literal/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ pub const fn decode<const LEN: usize>(strings: &[&[u8]]) -> [u8; LEN] {
8181
macro_rules! hex {
8282
($($s:literal)*) => {{
8383
const STRINGS: &[&'static [u8]] = &[$($s.as_bytes(),)*];
84-
$crate::decode::<{ $crate::len(STRINGS) }>(STRINGS)
84+
const LEN: usize = $crate::len(STRINGS);
85+
const RES: [u8; LEN] = $crate::decode(STRINGS);
86+
RES
8587
}};
8688
}

0 commit comments

Comments
 (0)