Skip to content

Make the cstr16! macro usable in const contexts #544

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

Merged
merged 1 commit into from
Nov 2, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
- The `Revision` type now implements `Display` with correct formatting
for all UEFI versions. The custom `Debug` impl has been removed and
replaced with a derived `Debug` impl.
- `CStr16::from_u16_with_nul_unchecked` and `cstr16!` are now allowed in
`const` contexts.

### Removed

Expand Down
9 changes: 8 additions & 1 deletion src/data_types/strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl CStr16 {
///
/// It's the callers responsibility to ensure chars is a valid UCS-2
/// null-terminated string, with no interior null bytes.
pub unsafe fn from_u16_with_nul_unchecked(codes: &[u16]) -> &Self {
pub const unsafe fn from_u16_with_nul_unchecked(codes: &[u16]) -> &Self {
&*(codes as *const [u16] as *const Self)
}

Expand Down Expand Up @@ -568,4 +568,11 @@ mod tests {
let input: &CStr16 = cstr16!("test");
test_compare_cstrX!(input);
}

/// Test that the `cstr16!` macro can be used in a `const` context.
#[test]
fn test_cstr16_macro_const() {
const S: &CStr16 = cstr16!("ABC");
assert_eq!(S.to_u16_slice_with_nul(), [65, 66, 67, 0]);
}
}