From 7c67d96c3ef640f9913cb8adea2ffe7c9d09c62a Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Mon, 3 Apr 2023 16:51:22 -0500 Subject: [PATCH 1/2] core types --- crates/libs/bindgen/src/lib.rs | 199 +--------------- crates/libs/bindgen/src/standalone.rs | 215 ++++++++++++++++++ crates/libs/metadata/src/reader/mod.rs | 3 +- crates/tests/standalone/build.rs | 65 +++++- crates/tests/standalone/src/b_bstr.rs | 12 + crates/tests/standalone/src/b_guid.rs | 34 +++ crates/tests/standalone/src/b_hresult.rs | 11 + crates/tests/standalone/src/b_hstring.rs | 11 + crates/tests/standalone/src/b_inspectable.rs | 13 ++ crates/tests/standalone/src/b_none.rs | 10 + crates/tests/standalone/src/b_pcstr.rs | 11 + crates/tests/standalone/src/b_pcwstr.rs | 11 + crates/tests/standalone/src/b_pstr.rs | 12 + crates/tests/standalone/src/b_pwstr.rs | 21 ++ .../standalone/src/{bindings.rs => b_test.rs} | 12 +- crates/tests/standalone/src/b_unknown.rs | 12 + crates/tests/standalone/src/lib.rs | 94 +++++++- 17 files changed, 527 insertions(+), 219 deletions(-) create mode 100644 crates/libs/bindgen/src/standalone.rs create mode 100644 crates/tests/standalone/src/b_bstr.rs create mode 100644 crates/tests/standalone/src/b_guid.rs create mode 100644 crates/tests/standalone/src/b_hresult.rs create mode 100644 crates/tests/standalone/src/b_hstring.rs create mode 100644 crates/tests/standalone/src/b_inspectable.rs create mode 100644 crates/tests/standalone/src/b_none.rs create mode 100644 crates/tests/standalone/src/b_pcstr.rs create mode 100644 crates/tests/standalone/src/b_pcwstr.rs create mode 100644 crates/tests/standalone/src/b_pstr.rs create mode 100644 crates/tests/standalone/src/b_pwstr.rs rename crates/tests/standalone/src/{bindings.rs => b_test.rs} (99%) create mode 100644 crates/tests/standalone/src/b_unknown.rs diff --git a/crates/libs/bindgen/src/lib.rs b/crates/libs/bindgen/src/lib.rs index a175e96d2a..67e4713566 100644 --- a/crates/libs/bindgen/src/lib.rs +++ b/crates/libs/bindgen/src/lib.rs @@ -22,6 +22,8 @@ use method_names::*; use std::collections::*; use std::fmt::Write; use tokens::*; +mod standalone; +pub use standalone::*; #[doc(hidden)] pub use gen::*; @@ -194,203 +196,6 @@ pub fn component(namespace: &str, files: &[File]) -> String { bindings } -/// Generates standalone bindings for Windows APIs. -pub fn standalone(names: &[&str]) -> String { - let files = &File::with_default(&[]).unwrap(); - let reader = &Reader::new(files); - let mut gen = &mut Gen::new(reader); - gen.standalone = true; - gen.sys = true; - let mut tokens: TokenStream = format!( - r#"// Bindings generated by `windows-bindgen` {} - -"#, - std::env!("CARGO_PKG_VERSION") - ) - .into(); - - tokens.combine(&allow()); - - tokens.combine("e! { - pub type HRESULT = i32; - pub type HSTRING = *mut ::core::ffi::c_void; - pub type IUnknown = *mut ::core::ffi::c_void; - pub type IInspectable = *mut ::core::ffi::c_void; - pub type PSTR = *mut u8; - pub type PWSTR = *mut u16; - pub type PCSTR = *const u8; - pub type PCWSTR = *const u16; - pub type BSTR = *const u16; - #[repr(C)] - pub struct GUID { - pub data1: u32, - pub data2: u16, - pub data3: u16, - pub data4: [u8; 8], - } - impl GUID { - pub const fn from_u128(uuid: u128) -> Self { - Self { data1: (uuid >> 96) as u32, data2: (uuid >> 80 & 0xffff) as u16, data3: (uuid >> 64 & 0xffff) as u16, data4: (uuid as u64).to_be_bytes() } - } - } - impl ::core::marker::Copy for GUID {} - impl ::core::clone::Clone for GUID { - fn clone(&self) -> Self { - *self - } - } - }); - - let mut type_names = BTreeSet::new(); - - for name in names { - let type_name = TypeName::parse(name); - let mut found = false; - - if let Some(def) = reader.get(type_name).next() { - found = true; - type_names.insert(type_name); - for def in reader.type_def_cfg(def, &[]).types.values().flatten() { - type_names.insert(reader.type_def_type_name(*def)); - } - } - - if !found { - if let Some(def) = reader - .get(TypeName::new(type_name.namespace, "Apis")) - .next() - { - for method in gen.reader.type_def_methods(def) { - if found { - break; - } - let name = gen.reader.method_def_name(method); - if name == type_name.name { - found = true; - type_names.insert(type_name); - for def in reader - .signature_cfg(&reader.method_def_signature(method, &[])) - .types - .values() - .flatten() - { - type_names.insert(reader.type_def_type_name(*def)); - } - } - } - for field in gen.reader.type_def_fields(def) { - if found { - break; - } - let name = gen.reader.field_name(field); - if name == type_name.name { - found = true; - type_names.insert(type_name); - for def in reader.field_cfg(field).types.values().flatten() { - type_names.insert(reader.type_def_type_name(*def)); - } - } - } - } - } - } - - for type_name in type_names { - let mut found = false; - - for def in reader.get(type_name) { - found = true; - let kind = gen.reader.type_def_kind(def); - - match kind { - TypeKind::Class | TypeKind::Interface => unimplemented!(), - TypeKind::Enum => tokens.combine(&enums::gen(gen, def)), - TypeKind::Struct => { - if gen.reader.type_def_fields(def).next().is_none() { - if let Some(guid) = gen.reader.type_def_guid(def) { - let ident = to_ident(type_name.name); - let value = gen.guid(&guid); - let guid = gen.type_name(&Type::GUID); - let cfg = gen.reader.type_def_cfg(def, &[]); - let doc = gen.cfg_doc(&cfg); - let constant = quote! { - #doc - pub const #ident: #guid = #value; - }; - tokens.combine(&constant); - continue; - } - } - tokens.combine(&structs::gen(gen, def)); - } - TypeKind::Delegate => tokens.combine(&delegates::gen(gen, def)), - } - } - - if !found { - if let Some(def) = reader - .get(TypeName::new(type_name.namespace, "Apis")) - .next() - { - for method in gen.reader.type_def_methods(def) { - if found { - break; - } - let name = gen.reader.method_def_name(method); - if name == type_name.name { - found = true; - tokens.combine(&functions::gen(gen, method)); - } - } - for field in gen.reader.type_def_fields(def) { - if found { - break; - } - let name = gen.reader.field_name(field); - if name == type_name.name { - found = true; - tokens.combine(&constants::gen(gen, field)); - } - } - } - } - } - - try_format(tokens.into_string()) -} - -fn try_format(tokens: String) -> String { - use std::io::Write; - - let Ok(mut child) = std::process::Command::new("rustfmt").stdin(std::process::Stdio::piped()).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::null()).spawn() else { - return tokens; - }; - - let Some(mut stdin) = child.stdin.take() else { - return tokens; - }; - - if stdin.write_all(tokens.as_bytes()).is_err() { - return tokens; - } - - drop(stdin); - - let Ok(output) = child.wait_with_output() else { - return tokens; - }; - - if !output.status.success() { - return tokens; - } - - if let Ok(result) = String::from_utf8(output.stdout) { - result - } else { - tokens - } -} - fn allow() -> TokenStream { quote! { #![allow(non_snake_case, non_upper_case_globals, non_camel_case_types, dead_code, clippy::all)] diff --git a/crates/libs/bindgen/src/standalone.rs b/crates/libs/bindgen/src/standalone.rs new file mode 100644 index 0000000000..07ca07745e --- /dev/null +++ b/crates/libs/bindgen/src/standalone.rs @@ -0,0 +1,215 @@ +use super::*; + +/// Generates standalone bindings for Windows APIs. +pub fn standalone(names: &[&str]) -> String { + let files = &File::with_default(&[]).unwrap(); + let reader = &Reader::new(files); + let mut gen = &mut Gen::new(reader); + gen.standalone = true; + gen.sys = true; + let mut tokens: TokenStream = format!( + r#"// Bindings generated by `windows-bindgen` {} + +"#, + std::env!("CARGO_PKG_VERSION") + ) + .into(); + + tokens.combine(&allow()); + + let mut type_names = BTreeSet::new(); + let mut core_types = BTreeSet::new(); + + for name in names { + let type_name = TypeName::parse(name); + let mut found = false; + + if let Some(def) = reader.get(type_name).next() { + found = true; + type_names.insert(type_name); + let mut cfg = reader.type_def_cfg(def, &[]); + core_types.append(&mut cfg.core_types); + for def in cfg.types.values().flatten() { + type_names.insert(reader.type_def_type_name(*def)); + } + if gen.reader.type_def_kind(def) == TypeKind::Struct + && gen.reader.type_def_fields(def).next().is_none() + && gen.reader.type_def_guid(def).is_some() + { + core_types.insert(Type::GUID); + } + } + + if !found { + if let Some(def) = reader + .get(TypeName::new(type_name.namespace, "Apis")) + .next() + { + for method in gen.reader.type_def_methods(def) { + if found { + break; + } + let name = gen.reader.method_def_name(method); + if name == type_name.name { + found = true; + type_names.insert(type_name); + let mut cfg = + reader.signature_cfg(&reader.method_def_signature(method, &[])); + core_types.append(&mut cfg.core_types); + for def in cfg.types.values().flatten() { + type_names.insert(reader.type_def_type_name(*def)); + } + } + } + for field in gen.reader.type_def_fields(def) { + if found { + break; + } + let name = gen.reader.field_name(field); + if name == type_name.name { + found = true; + type_names.insert(type_name); + let mut cfg = reader.field_cfg(field); + core_types.append(&mut cfg.core_types); + for def in cfg.types.values().flatten() { + type_names.insert(reader.type_def_type_name(*def)); + } + } + } + } + } + } + + for ty in core_types { + match ty { + Type::HRESULT => tokens.combine("e! { pub type HRESULT = i32; }), + Type::String => { + tokens.combine("e! { pub type HSTRING = *mut ::core::ffi::c_void; }) + } + Type::IUnknown => { + tokens.combine("e! { pub type IUnknown = *mut ::core::ffi::c_void; }) + } + Type::IInspectable => { + tokens.combine("e! { pub type IInspectable = *mut ::core::ffi::c_void; }) + } + Type::PSTR => tokens.combine("e! { pub type PSTR = *mut u8; }), + Type::PWSTR => tokens.combine("e! { pub type PWSTR = *mut u16; }), + Type::PCSTR => tokens.combine("e! { pub type PCSTR = *const u8; }), + Type::PCWSTR => tokens.combine("e! { pub type PCWSTR = *const u16; }), + Type::BSTR => tokens.combine("e! { pub type BSTR = *const u16; }), + Type::GUID => tokens.combine("e! { + #[repr(C)] + pub struct GUID { + pub data1: u32, + pub data2: u16, + pub data3: u16, + pub data4: [u8; 8], + } + impl GUID { + pub const fn from_u128(uuid: u128) -> Self { + Self { data1: (uuid >> 96) as u32, data2: (uuid >> 80 & 0xffff) as u16, data3: (uuid >> 64 & 0xffff) as u16, data4: (uuid as u64).to_be_bytes() } + } + } + impl ::core::marker::Copy for GUID {} + impl ::core::clone::Clone for GUID { + fn clone(&self) -> Self { + *self + } + } + }), + _ => {} + } + } + + for type_name in type_names { + let mut found = false; + + for def in reader.get(type_name) { + found = true; + let kind = gen.reader.type_def_kind(def); + + match kind { + TypeKind::Class | TypeKind::Interface => unimplemented!(), + TypeKind::Enum => tokens.combine(&enums::gen(gen, def)), + TypeKind::Struct => { + if gen.reader.type_def_fields(def).next().is_none() { + if let Some(guid) = gen.reader.type_def_guid(def) { + let ident = to_ident(type_name.name); + let value = gen.guid(&guid); + let guid = gen.type_name(&Type::GUID); + let constant = quote! { + pub const #ident: #guid = #value; + }; + tokens.combine(&constant); + continue; + } + } + tokens.combine(&structs::gen(gen, def)); + } + TypeKind::Delegate => tokens.combine(&delegates::gen(gen, def)), + } + } + + if !found { + if let Some(def) = reader + .get(TypeName::new(type_name.namespace, "Apis")) + .next() + { + for method in gen.reader.type_def_methods(def) { + if found { + break; + } + let name = gen.reader.method_def_name(method); + if name == type_name.name { + found = true; + tokens.combine(&functions::gen(gen, method)); + } + } + for field in gen.reader.type_def_fields(def) { + if found { + break; + } + let name = gen.reader.field_name(field); + if name == type_name.name { + found = true; + tokens.combine(&constants::gen(gen, field)); + } + } + } + } + } + + try_format(tokens.into_string()) +} + +fn try_format(tokens: String) -> String { + use std::io::Write; + + let Ok(mut child) = std::process::Command::new("rustfmt").stdin(std::process::Stdio::piped()).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::null()).spawn() else { + return tokens; + }; + + let Some(mut stdin) = child.stdin.take() else { + return tokens; + }; + + if stdin.write_all(tokens.as_bytes()).is_err() { + return tokens; + } + + drop(stdin); + + let Ok(output) = child.wait_with_output() else { + return tokens; + }; + + if !output.status.success() { + return tokens; + } + + if let Ok(result) = String::from_utf8(output.stdout) { + result + } else { + tokens + } +} diff --git a/crates/libs/metadata/src/reader/mod.rs b/crates/libs/metadata/src/reader/mod.rs index a82469cada..d1906fb367 100644 --- a/crates/libs/metadata/src/reader/mod.rs +++ b/crates/libs/metadata/src/reader/mod.rs @@ -157,6 +157,7 @@ pub struct SignatureParam { #[derive(Default, Clone)] pub struct Cfg<'a> { pub types: BTreeMap<&'a str, BTreeSet>, + pub core_types: BTreeSet, pub arches: BTreeSet<&'static str>, pub implement: bool, } @@ -1469,7 +1470,7 @@ impl<'a> Reader<'a> { Type::MutPtr((ty, _)) => self.type_cfg_combine(ty, cfg), Type::WinrtArray(ty) => self.type_cfg_combine(ty, cfg), Type::WinrtArrayRef(ty) => self.type_cfg_combine(ty, cfg), - _ => {} + ty => _ = cfg.core_types.insert(ty.clone()), } } pub fn type_interfaces(&self, ty: &Type) -> Vec { diff --git a/crates/tests/standalone/build.rs b/crates/tests/standalone/build.rs index 3d94523c2d..6b8133e68e 100644 --- a/crates/tests/standalone/build.rs +++ b/crates/tests/standalone/build.rs @@ -1,14 +1,59 @@ fn main() { - let apis = [ - "Windows.Win32.Foundation.CloseHandle", - "Windows.Win32.System.Com.CoCreateInstance", - "Windows.Win32.System.Com.STGTY_REPEAT", - "Windows.Win32.System.Threading.CreateEventW", - "Windows.Win32.System.Threading.SetEvent", - "Windows.Win32.System.Threading.WaitForSingleObject", - "Windows.Win32.UI.Animation.UIAnimationManager", - ]; + write( + "src/b_none.rs", + &["Windows.Win32.System.SystemInformation.GetTickCount"], + ); + write( + "src/b_hresult.rs", + &["Windows.Win32.System.Com.CoInitialize"], + ); + + write( + "src/b_hstring.rs", + &["Windows.Win32.System.WinRT.WindowsGetStringLen"], + ); + + write( + "src/b_unknown.rs", + &["Windows.Win32.System.Com.CoIsHandlerConnected"], + ); + + write( + "src/b_inspectable.rs", + &["Windows.Win32.System.WinRT.RoActivateInstance"], + ); + + write("src/b_pstr.rs", &["Windows.Win32.System.Ole.VarI1FromDate"]); + + write("src/b_pwstr.rs", &["Windows.Win32.System.Ole.CALPOLESTR"]); + + write("src/b_pcstr.rs", &["Windows.Win32.Globalization.lstrlenA"]); + + write("src/b_pcwstr.rs", &["Windows.Win32.Globalization.lstrlenW"]); + + write( + "src/b_bstr.rs", + &["Windows.Win32.Foundation.SysAllocString"], + ); + + write("src/b_guid.rs", &["Windows.Win32.System.Com.CoCreateGuid"]); + + write( + "src/b_test.rs", + &[ + "Windows.Win32.Foundation.CloseHandle", + "Windows.Win32.System.Com.CoCreateInstance", + "Windows.Win32.System.Com.STGTY_REPEAT", + "Windows.Win32.System.Threading.CreateEventW", + "Windows.Win32.System.Threading.SetEvent", + "Windows.Win32.System.Threading.WaitForSingleObject", + "Windows.Win32.UI.Animation.UIAnimationManager", + ], + ); +} + +fn write(filename: &str, apis: &[&str]) { let bindings = windows_bindgen::standalone(&apis); - std::fs::write("src/bindings.rs", bindings).unwrap(); + std::fs::write(filename, bindings).unwrap(); } diff --git a/crates/tests/standalone/src/b_bstr.rs b/crates/tests/standalone/src/b_bstr.rs new file mode 100644 index 0000000000..b013bd2c0f --- /dev/null +++ b/crates/tests/standalone/src/b_bstr.rs @@ -0,0 +1,12 @@ +// Bindings generated by `windows-bindgen` 0.48.0 + +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] +pub type PCWSTR = *const u16; +pub type BSTR = *const u16; +::windows_targets::link ! ( "oleaut32.dll""system" fn SysAllocString ( psz : PCWSTR ) -> BSTR ); diff --git a/crates/tests/standalone/src/b_guid.rs b/crates/tests/standalone/src/b_guid.rs new file mode 100644 index 0000000000..a54a784fe2 --- /dev/null +++ b/crates/tests/standalone/src/b_guid.rs @@ -0,0 +1,34 @@ +// Bindings generated by `windows-bindgen` 0.48.0 + +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] +#[repr(C)] +pub struct GUID { + pub data1: u32, + pub data2: u16, + pub data3: u16, + pub data4: [u8; 8], +} +impl GUID { + pub const fn from_u128(uuid: u128) -> Self { + Self { + data1: (uuid >> 96) as u32, + data2: (uuid >> 80 & 0xffff) as u16, + data3: (uuid >> 64 & 0xffff) as u16, + data4: (uuid as u64).to_be_bytes(), + } + } +} +impl ::core::marker::Copy for GUID {} +impl ::core::clone::Clone for GUID { + fn clone(&self) -> Self { + *self + } +} +pub type HRESULT = i32; +::windows_targets::link ! ( "ole32.dll""system" fn CoCreateGuid ( pguid : *mut GUID ) -> HRESULT ); diff --git a/crates/tests/standalone/src/b_hresult.rs b/crates/tests/standalone/src/b_hresult.rs new file mode 100644 index 0000000000..8891115bb2 --- /dev/null +++ b/crates/tests/standalone/src/b_hresult.rs @@ -0,0 +1,11 @@ +// Bindings generated by `windows-bindgen` 0.48.0 + +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] +pub type HRESULT = i32; +::windows_targets::link ! ( "ole32.dll""system" fn CoInitialize ( pvreserved : *const ::core::ffi::c_void ) -> HRESULT ); diff --git a/crates/tests/standalone/src/b_hstring.rs b/crates/tests/standalone/src/b_hstring.rs new file mode 100644 index 0000000000..ac9f67bea6 --- /dev/null +++ b/crates/tests/standalone/src/b_hstring.rs @@ -0,0 +1,11 @@ +// Bindings generated by `windows-bindgen` 0.48.0 + +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] +pub type HSTRING = *mut ::core::ffi::c_void; +::windows_targets::link ! ( "api-ms-win-core-winrt-string-l1-1-0.dll""system" fn WindowsGetStringLen ( string : HSTRING ) -> u32 ); diff --git a/crates/tests/standalone/src/b_inspectable.rs b/crates/tests/standalone/src/b_inspectable.rs new file mode 100644 index 0000000000..332456c4e0 --- /dev/null +++ b/crates/tests/standalone/src/b_inspectable.rs @@ -0,0 +1,13 @@ +// Bindings generated by `windows-bindgen` 0.48.0 + +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] +pub type HSTRING = *mut ::core::ffi::c_void; +pub type IInspectable = *mut ::core::ffi::c_void; +pub type HRESULT = i32; +::windows_targets::link ! ( "api-ms-win-core-winrt-l1-1-0.dll""system" fn RoActivateInstance ( activatableclassid : HSTRING , instance : *mut IInspectable ) -> HRESULT ); diff --git a/crates/tests/standalone/src/b_none.rs b/crates/tests/standalone/src/b_none.rs new file mode 100644 index 0000000000..5f5d4017a4 --- /dev/null +++ b/crates/tests/standalone/src/b_none.rs @@ -0,0 +1,10 @@ +// Bindings generated by `windows-bindgen` 0.48.0 + +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] +::windows_targets::link ! ( "kernel32.dll""system" fn GetTickCount ( ) -> u32 ); diff --git a/crates/tests/standalone/src/b_pcstr.rs b/crates/tests/standalone/src/b_pcstr.rs new file mode 100644 index 0000000000..b29bef83d2 --- /dev/null +++ b/crates/tests/standalone/src/b_pcstr.rs @@ -0,0 +1,11 @@ +// Bindings generated by `windows-bindgen` 0.48.0 + +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] +pub type PCSTR = *const u8; +::windows_targets::link ! ( "kernel32.dll""system" fn lstrlenA ( lpstring : PCSTR ) -> i32 ); diff --git a/crates/tests/standalone/src/b_pcwstr.rs b/crates/tests/standalone/src/b_pcwstr.rs new file mode 100644 index 0000000000..a075f596f5 --- /dev/null +++ b/crates/tests/standalone/src/b_pcwstr.rs @@ -0,0 +1,11 @@ +// Bindings generated by `windows-bindgen` 0.48.0 + +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] +pub type PCWSTR = *const u16; +::windows_targets::link ! ( "kernel32.dll""system" fn lstrlenW ( lpstring : PCWSTR ) -> i32 ); diff --git a/crates/tests/standalone/src/b_pstr.rs b/crates/tests/standalone/src/b_pstr.rs new file mode 100644 index 0000000000..eda07371b5 --- /dev/null +++ b/crates/tests/standalone/src/b_pstr.rs @@ -0,0 +1,12 @@ +// Bindings generated by `windows-bindgen` 0.48.0 + +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] +pub type HRESULT = i32; +pub type PSTR = *mut u8; +::windows_targets::link ! ( "oleaut32.dll""system" fn VarI1FromDate ( datein : f64 , pcout : PSTR ) -> HRESULT ); diff --git a/crates/tests/standalone/src/b_pwstr.rs b/crates/tests/standalone/src/b_pwstr.rs new file mode 100644 index 0000000000..2ed8e10704 --- /dev/null +++ b/crates/tests/standalone/src/b_pwstr.rs @@ -0,0 +1,21 @@ +// Bindings generated by `windows-bindgen` 0.48.0 + +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] +pub type PWSTR = *mut u16; +#[repr(C)] +pub struct CALPOLESTR { + pub cElems: u32, + pub pElems: *mut PWSTR, +} +impl ::core::marker::Copy for CALPOLESTR {} +impl ::core::clone::Clone for CALPOLESTR { + fn clone(&self) -> Self { + *self + } +} diff --git a/crates/tests/standalone/src/bindings.rs b/crates/tests/standalone/src/b_test.rs similarity index 99% rename from crates/tests/standalone/src/bindings.rs rename to crates/tests/standalone/src/b_test.rs index a823fdbd4c..25a76677e8 100644 --- a/crates/tests/standalone/src/bindings.rs +++ b/crates/tests/standalone/src/b_test.rs @@ -7,15 +7,6 @@ dead_code, clippy::all )] -pub type HRESULT = i32; -pub type HSTRING = *mut ::core::ffi::c_void; -pub type IUnknown = *mut ::core::ffi::c_void; -pub type IInspectable = *mut ::core::ffi::c_void; -pub type PSTR = *mut u8; -pub type PWSTR = *mut u16; -pub type PCSTR = *const u8; -pub type PCWSTR = *const u16; -pub type BSTR = *const u16; #[repr(C)] pub struct GUID { pub data1: u32, @@ -39,6 +30,9 @@ impl ::core::clone::Clone for GUID { *self } } +pub type IUnknown = *mut ::core::ffi::c_void; +pub type HRESULT = i32; +pub type PCWSTR = *const u16; pub type BOOL = i32; ::windows_targets::link ! ( "kernel32.dll""system" fn CloseHandle ( hobject : HANDLE ) -> BOOL ); pub type HANDLE = isize; diff --git a/crates/tests/standalone/src/b_unknown.rs b/crates/tests/standalone/src/b_unknown.rs new file mode 100644 index 0000000000..46eb779d61 --- /dev/null +++ b/crates/tests/standalone/src/b_unknown.rs @@ -0,0 +1,12 @@ +// Bindings generated by `windows-bindgen` 0.48.0 + +#![allow( + non_snake_case, + non_upper_case_globals, + non_camel_case_types, + dead_code, + clippy::all +)] +pub type IUnknown = *mut ::core::ffi::c_void; +pub type BOOL = i32; +::windows_targets::link ! ( "ole32.dll""system" fn CoIsHandlerConnected ( punk : IUnknown ) -> BOOL ); diff --git a/crates/tests/standalone/src/lib.rs b/crates/tests/standalone/src/lib.rs index ebdf746520..e9fa41a095 100644 --- a/crates/tests/standalone/src/lib.rs +++ b/crates/tests/standalone/src/lib.rs @@ -1,10 +1,100 @@ #![cfg(test)] -mod bindings; -use bindings::*; +mod b_bstr; +mod b_guid; +mod b_hresult; +mod b_hstring; +mod b_inspectable; +mod b_none; +mod b_pcstr; +mod b_pcwstr; +mod b_pstr; +mod b_pwstr; +mod b_test; +mod b_unknown; + +#[test] +fn bstr() { + unsafe { + b_bstr::SysAllocString(std::ptr::null()); + } +} + +#[test] +fn guid() { + unsafe { + b_guid::CoCreateGuid(std::ptr::null_mut()); + } +} + +#[test] +fn hresult() { + unsafe { + b_hresult::CoInitialize(std::ptr::null()); + } +} + +#[test] +fn hstring() { + unsafe { + b_hstring::WindowsGetStringLen(std::ptr::null_mut()); + } +} + +#[test] +fn inspectable() { + unsafe { + b_inspectable::RoActivateInstance(std::ptr::null_mut(), &mut std::ptr::null_mut()); + } +} + +#[test] +fn none() { + unsafe { + b_none::GetTickCount(); + } +} + +#[test] +fn pcstr() { + unsafe { + b_pcstr::lstrlenA(std::ptr::null()); + } +} + +#[test] +fn pcwstr() { + unsafe { + b_pcwstr::lstrlenW(std::ptr::null()); + } +} + +#[test] +fn pstr() { + unsafe { + let mut buffer = [0; 10]; + b_pstr::VarI1FromDate(0.0, buffer.as_mut_ptr()); + } +} + +#[test] +fn pwstr() { + let _ = b_pwstr::CALPOLESTR { + cElems: 0, + pElems: std::ptr::null_mut(), + }; +} + +#[test] +fn unknown() { + unsafe { + b_unknown::CoIsHandlerConnected(std::ptr::null_mut()); + } +} #[test] fn test() { + use b_test::*; unsafe { let event = CreateEventW(std::ptr::null(), 1, 0, std::ptr::null()); SetEvent(event); From 50b5381b5bf20e00d1c39810334e9f521c7c072f Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Mon, 3 Apr 2023 20:02:00 -0500 Subject: [PATCH 2/2] clippy --- crates/tests/standalone/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tests/standalone/build.rs b/crates/tests/standalone/build.rs index 6b8133e68e..774126a285 100644 --- a/crates/tests/standalone/build.rs +++ b/crates/tests/standalone/build.rs @@ -54,6 +54,6 @@ fn main() { } fn write(filename: &str, apis: &[&str]) { - let bindings = windows_bindgen::standalone(&apis); + let bindings = windows_bindgen::standalone(apis); std::fs::write(filename, bindings).unwrap(); }