From 3546493f1f402ff4076f959b35e3042eda50a4a9 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Tue, 1 Nov 2022 16:55:02 -0400 Subject: [PATCH] Make the `cstr16!` macro usable in const contexts --- CHANGELOG.md | 2 ++ src/data_types/strs.rs | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6d8aaad1..61c183c01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/data_types/strs.rs b/src/data_types/strs.rs index e93a99539..24759ba95 100644 --- a/src/data_types/strs.rs +++ b/src/data_types/strs.rs @@ -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) } @@ -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]); + } }