Skip to content

Commit 3546493

Browse files
Make the cstr16! macro usable in const contexts
1 parent cbe49e2 commit 3546493

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
- The `Revision` type now implements `Display` with correct formatting
2727
for all UEFI versions. The custom `Debug` impl has been removed and
2828
replaced with a derived `Debug` impl.
29+
- `CStr16::from_u16_with_nul_unchecked` and `cstr16!` are now allowed in
30+
`const` contexts.
2931

3032
### Removed
3133

src/data_types/strs.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl CStr16 {
212212
///
213213
/// It's the callers responsibility to ensure chars is a valid UCS-2
214214
/// null-terminated string, with no interior null bytes.
215-
pub unsafe fn from_u16_with_nul_unchecked(codes: &[u16]) -> &Self {
215+
pub const unsafe fn from_u16_with_nul_unchecked(codes: &[u16]) -> &Self {
216216
&*(codes as *const [u16] as *const Self)
217217
}
218218

@@ -568,4 +568,11 @@ mod tests {
568568
let input: &CStr16 = cstr16!("test");
569569
test_compare_cstrX!(input);
570570
}
571+
572+
/// Test that the `cstr16!` macro can be used in a `const` context.
573+
#[test]
574+
fn test_cstr16_macro_const() {
575+
const S: &CStr16 = cstr16!("ABC");
576+
assert_eq!(S.to_u16_slice_with_nul(), [65, 66, 67, 0]);
577+
}
571578
}

0 commit comments

Comments
 (0)