Skip to content
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

Widestring literals #11

Closed
suhr opened this issue May 19, 2020 · 3 comments
Closed

Widestring literals #11

suhr opened this issue May 19, 2020 · 3 comments
Labels
awaiting stabilization Resolving this issue requires stabilization of unstable nightly Rust features enhancement

Comments

@suhr
Copy link

suhr commented May 19, 2020

There exist quite a handy crate: https://docs.rs/wchar. It provides wch_c! macro, which converts a sting literal to &'static [u16] at compile time.

It would be great to have a similar macro in widestring-rs, so you could define &'static U16CStr constants.

@luser
Copy link

luser commented Dec 16, 2020

There's also utf16_lit which does roughly the same thing, but doesn't have a dependency on syn, which is nice.

@starkat99
Copy link
Owner

I tried to make some macros, but const features are just not quite stabilized enough to do it with macro_rules, and I'd rather not add a dependency on syn just for this. utf16_lit is a good interim implementations for now, but the technique can only make arrays, then can't currently be converted to slices or this crate's string types in const contexts. Yet. Any one of several unstable features getting stabilized would enable this, so holding off for now.

@starkat99 starkat99 added awaiting stabilization Resolving this issue requires stabilization of unstable nightly Rust features enhancement labels Oct 8, 2021
@starkat99
Copy link
Owner

Rust 1.56 stabilized mem::transmute as const, so I was able to implement the macros. There is a macro for each wide string slice type, including alias macros for WideStr/WideCStr.

Seems to work out well in unit tests. Here's example usage:

const MY_STR: &U16CStr = u16cstr!("A constant, nul-terminated UTF-16 string!");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting stabilization Resolving this issue requires stabilization of unstable nightly Rust features enhancement
Projects
None yet
Development

No branches or pull requests

3 participants