nitric1 / wstr-rs

Rust macros for compile-time UTF-16 (wide) string literals.
MIT License
5 stars 0 forks source link

Provide a way to have null-terminated UTF16 literals #1

Closed Boddlnagg closed 6 years ago

Boddlnagg commented 6 years ago

Thanks for this very useful crate! I had been looking for such a thing for a long time 👍

WinRT's HStrings are null-terminated UTF16 strings. To be able to use literals without runtime allocation, it would be great if this crate would offer an additional macro variant with an added null terminator (e.g. wstrz!), which I could then wrap in a simple

macro_rules! hstr {
    ($str: tt) => { HStringReference::from_utf16(wstrz!($str)) }
}

(see https://github.com/contextfree/winrt-rust/blob/5deb9e19a2397d5b5bc48097a417b104b0dec6fe/src/hstring.rs#L74 for the definition of HStringReference)

I tried wstr!(concat!($str, "\0")), but it complains that input must be string literal. It would also be great if wstrz! could ensure that there are no interior nulls.

nitric1 commented 6 years ago

I appreciate your suggestion. wstrz! must be great, which I had not thought about it completely.

The feature will be implemented soon, with a new version number, 0.2.0.

nitric1 commented 6 years ago

Oh sorry, I missed the sentence about interior nulls.

Adding the limitation is easy work, but it should be careful, because there are some Win32 API's requiring nulls in the middle of string, like OPENFILENAME::lpstrFilter. Furthermore, wstr! allows nulls in strings, so I think wstrz! also should have same behavior.

Boddlnagg commented 6 years ago

@nitric1 Thanks for the change, and don't worry about interior nuls ... I just found out that WinRT can deal with interior nuls just fine!