wstr-literal
Procedural macros for building UTF-16 null-terminated string arrays for Windows FFI and similar APIs at compile time.
This crate provides two macros:
- [
wstr!] — a function-like macro that converts a string literal to a UTF-16 array with a trailing null (0u16). Optionally accepts an explicit array length and pads with zeros. - [
wstr_literal] — an attribute macro for const or static declaration that transforms a string-literal into a UTF-16 array with a trailing null. Supports either a fixed array length or an inferred length via placeholder(_).
Both macros expand at compile time into numeric [u16; N] array literals; there is no runtime allocation or conversion.
Installation
Add this crate to your Cargo.toml:
[]
= "0.1"
Examples
use ;
// Exact-length array (5 code units + 1 null terminator)
let hello: = wstr!;
assert_eq!;
// Pad to a larger, fixed length
let padded: = wstr!;
assert_eq!;
assert_eq!;
// Slice
let slice: & = &wstr!;
assert_eq!;
const HELLO: = "hello"; // length inferred (5 + 1)
assert_eq!;
static HELLO_PADDED: = "hello"; // padded with zeros to length 0x10
assert_eq!;
assert_eq!; // padding
// supports const/static, visibility, mutability, and other attributes
pub static mut GlobalMut: = "x";
Using with windows-rs
These arrays are suitable for FFI calls expecting UTF-16 with a trailing null, for example with windows crate:
use wstr_literal;
use PCWSTR;
static APP_NAME: = "MyApp";
let pcwstr = PCWSTR;
// pass `pcwstr` to Windows APIs that expect a wide string
Acknowledgments
This macro was inspired by auto-const-array crate.