#utf-16 #string-literal #literals

macro wide-literals

UTF-16 string literals for use with wide string APIs accepting pointers to null-terminated strings

2 unstable releases

0.2.0 Feb 7, 2021
0.1.0 Feb 7, 2021

#30 in #utf-16

CC0-1.0 OR Unlicense

4KB

Rust wide string literals

This library provides a macro to convert string literals to UTF-16 at compile-time, allowing you to pass them into C-based APIs that expect pointers to null-terminated wide strings, such as the Windows API. This saves conversion overhead at runtime by allowing you to store UTF-16 strings directly in your executable. The syntax is as brief as I can make it, to help avoid line noise.

let my_string: &'static u16 = w!("Hello, world!");
// These strings are references to 16-bit integers, which Rust considers “close enough” to the raw pointers that APIs expect to work without casting.
MessageBoxW(null_mut(), my_string, w!("Error"), MB_ICONEXCLAMATION | MB_OK);

The purpose of this macro is to provide the string in exactly the form that C-based APIs need. If you want to be able to manipulate the string first, or want to use UTF-16 for non-static strings, this is not what you want, although it might still be useful for you.

Dependencies

~1.5MB
~37K SLoC