Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f6caee4

Browse files
committed
macros: allow to create empty cstr16 slice
1 parent 0607f74 commit f6caee4

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

‎uefi-macros/src/lib.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,28 @@ pub fn cstr8(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
283283
/// This will throw a compile error if an invalid character is in the passed string.
284284
///
285285
/// # Example
286-
/// ```
286+
/// ```rust
287287
/// # use uefi_macros::cstr16;
288+
/// // Empty string
289+
/// assert_eq!(cstr16!().to_u16_slice_with_nul(), [0]);
290+
/// assert_eq!(cstr16!("").to_u16_slice_with_nul(), [0]);
291+
/// // Non-empty string
288292
/// assert_eq!(cstr16!("test €").to_u16_slice_with_nul(), [116, 101, 115, 116, 32, 8364, 0]);
289293
/// ```
290294
#[proc_macro]
291295
pub fn cstr16(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
296+
// Accept empty input.
297+
if input.is_empty() {
298+
return quote!(unsafe { ::uefi::CStr16::from_u16_with_nul_unchecked(&[0]) }).into();
299+
}
292300
let input: LitStr = parse_macro_input!(input);
293301
let input = input.value();
302+
// Accept "" input.
303+
if input.is_empty() {
304+
return quote!(unsafe { ::uefi::CStr16::from_u16_with_nul_unchecked(&[0]) }).into();
305+
}
306+
307+
// Accept any non-empty string input.
294308
match input
295309
.chars()
296310
.map(|c| u16::try_from(c as u32))
@@ -299,8 +313,11 @@ pub fn cstr16(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
299313
Ok(c) => {
300314
quote!(unsafe { ::uefi::CStr16::from_u16_with_nul_unchecked(&[ #(#c),* , 0 ]) }).into()
301315
}
302-
Err(_) => syn::Error::new_spanned(input, "invalid character in string")
303-
.into_compile_error()
304-
.into(),
316+
Err(_) => syn::Error::new_spanned(
317+
input,
318+
"There are UTF-8 characters that can't be transformed to UCS-2 character",
319+
)
320+
.into_compile_error()
321+
.into(),
305322
}
306323
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /