Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hex-literal: enforce const evaluation #889

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions hex-literal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.4.1 (2023-04-05)
### Changed
- Enforce const evaluation ([#889])

[#889]: https://github.com/RustCrypto/utils/pull/889

## 0.4.0 (2023-04-02)
### Changed
- Disallow comments inside hex strings ([#816])
Expand Down
2 changes: 1 addition & 1 deletion hex-literal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hex-literal"
version = "0.4.0"
version = "0.4.1"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
description = "Macro for converting hexadecimal string to a byte array at compile time"
Expand Down
29 changes: 22 additions & 7 deletions hex-literal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,37 @@ let bytes1 = hex!("
00010203 04050607
08090a0b 0c0d0e0f
");
assert_eq!(bytes1, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
assert_eq!(
bytes1,
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
);

// It's possible to use several literals (results will be concatenated)
// It's possible to use several literals
// (results will be concatenated)
let bytes2 = hex!(
"00010203 04050607" // first half
"08090a0b" /* block comment */ "0c0d0e0f" // second half
"08090a0b 0c0d0e0f" // second half
);
assert_eq!(bytes1, bytes2);
```

Using an unsupported character inside literals will result in a compilation error:
```rust,compile_fail
# use hex_literal::hex;
hex!("АА"); // Cyrillic "А"
hex!("11 22"); // Japanese space
hex!("0123 // Сomments inside literals are not supported");
hex_literal::hex!("АА"); // Cyrillic "А"
hex_literal::hex!("11 22"); // Japanese space
```

Сomments inside literals are not supported:
```rust,compile_fail
hex_literal::hex!("0123 // foo");
```

Each literal must contain an even number of hex characters:
```rust,compile_fail
hex_literal::hex!(
"01234"
"567"
);
```

## Minimum Supported Rust Version
Expand Down
4 changes: 3 additions & 1 deletion hex-literal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub const fn decode<const LEN: usize>(strings: &[&[u8]]) -> [u8; LEN] {
macro_rules! hex {
($($s:literal)*) => {{
const STRINGS: &[&'static [u8]] = &[$($s.as_bytes(),)*];
$crate::decode::<{ $crate::len(STRINGS) }>(STRINGS)
const LEN: usize = $crate::len(STRINGS);
const RES: [u8; LEN] = $crate::decode(STRINGS);
RES
}};
}