Skip to content

Commit

Permalink
Fix string constants for standalone sys
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake-Shadle committed May 10, 2023
1 parent 83a451c commit deb0645
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
6 changes: 5 additions & 1 deletion crates/libs/bindgen/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ pub fn gen(gen: &Gen, def: Field) -> TokenStream {

if ty == constant_type {
if ty == Type::String {
let crate_name = gen.crate_name();
let crate_name: TokenStream = if gen.sys && !gen.standalone {
"::windows_sys::core::".into()
} else {
"::windows_core::".into()
};
if gen.reader.field_is_ansi(def) {
let value = gen.value(&gen.reader.constant_value(constant));
quote! {
Expand Down
11 changes: 9 additions & 2 deletions crates/libs/bindgen/src/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,15 @@ fn standalone_imp(gen: &mut Gen, names: &[&str]) -> String {
.find(|field| gen.reader.field_name(*field) == type_name.name)
{
constants.insert(field);
gen.reader
.type_collect_standalone(&gen.reader.field_type(field, None), &mut types);

let ft = gen.reader.field_type(field, None);

// String normally gets resolved to HSTRING, but since macros
// from windows_core/windows_sys::core are used to create the
// string literals we can skip the type collection
if ft != Type::String {
gen.reader.type_collect_standalone(&ft, &mut types);
}
}

if let Some(field) = gen
Expand Down
13 changes: 12 additions & 1 deletion crates/tests/standalone/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,18 @@ fn main() {
write_sys(
"src/b_overloads.rs",
&["Windows.Win32.NetworkManagement.NetManagement.AE_RESACCESS"],
)
);

// Ensures that string constants are written correctly
write_sys(
"src/b_string_constants.rs",
&[
// utf-16
"Windows.Win32.Devices.Enumeration.Pnp.ADDRESS_FAMILY_VALUE_NAME",
// ansi
"Windows.Win32.Media.Multimedia.JOY_CONFIGCHANGED_MSGSTRING",
],
);
}

fn write_sys(filename: &str, apis: &[&str]) {
Expand Down
14 changes: 14 additions & 0 deletions crates/tests/standalone/src/b_string_constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Bindings generated by `windows-bindgen` 0.49.0

#![allow(
non_snake_case,
non_upper_case_globals,
non_camel_case_types,
dead_code,
clippy::all
)]
pub const ADDRESS_FAMILY_VALUE_NAME: ::windows_core::PCWSTR = ::windows_core::w!("AddressFamily");
pub const JOY_CONFIGCHANGED_MSGSTRING: ::windows_core::PCSTR =
::windows_core::s!("MSJSTICK_VJOYD_MSGSTR");
pub type PCSTR = *const u8;
pub type PCWSTR = *const u16;
1 change: 1 addition & 0 deletions crates/tests/standalone/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod b_pcwstr;
mod b_pstr;
mod b_pwstr;
mod b_std;
mod b_string_constants;
mod b_stringable;
mod b_test;
mod b_unknown;
Expand Down

0 comments on commit deb0645

Please sign in to comment.