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

Investigate how to represent string constants #692

Open
retep998 opened this issue Sep 30, 2018 · 4 comments
Open

Investigate how to represent string constants #692

retep998 opened this issue Sep 30, 2018 · 4 comments
Labels
help wanted Help is requested to deal with this issue

Comments

@retep998
Copy link
Owner

String constants currently aren't very useful as they do not have null terminators, and they are normal Rust UTF-8 strings. This means that users have to do runtime conversions in order to pass these strings to winapi functions, instead of just grabbing a raw pointer and tossing it in.

A solution needs to be found using a (procedural?) macro that will transform a string literal into a constant with methods to access both the UTF-8 and UTF-16 variants, both with and without a null terminator.

@retep998 retep998 added the help wanted Help is requested to deal with this issue label Sep 30, 2018
@MSxDOS
Copy link
Contributor

MSxDOS commented Oct 11, 2018

I've implemented two ways, one using a decl macro + old proc_macro_derive for UTF16, and another using proc_macro_attribute for everything. I prefer the first one. L-like expression level macro for UTF16 literals may also be implemented if needed, I use it in my local projects. No external dependencies except for the separate crate for procedural macros. Target Rust is probably 1.31 (rust-lang/rust#53555 (comment))
Everything is here https://github.com/MSxDOS/string_const

@Eh2406
Copy link
Contributor

Eh2406 commented Oct 12, 2018

There is also wstr that uses proc-macro-hack to do something similar on stable. I don't know if it has all the features we need.

@MSxDOS
Copy link
Contributor

MSxDOS commented Oct 12, 2018

Besides having a lot of implicit dependencies, wstr uses encode_utf16 for everything and forces dynamic linking to std for some weird reason. Stable is also irrelevant here as either would require a version bump from 1.6 for procedural macros to work.

I don't know if it has all the features we need.

It obviously doesn't as it's just a simple expression level macro.

@suhr
Copy link

suhr commented May 19, 2020

There are some relevant crates:

It is probably a good idea to combine them together, so you could have &'static U16CStr. There's an issue at widestring-rs: starkat99/widestring-rs#11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Help is requested to deal with this issue
Projects
None yet
Development

No branches or pull requests

4 participants