diff --git a/.github/readme.md b/.github/readme.md index 92f439fe57..0cf44863cd 100644 --- a/.github/readme.md +++ b/.github/readme.md @@ -75,16 +75,18 @@ features = [ Make use of any Windows APIs as needed. ```rust -use windows_sys::{Win32::Foundation::*, Win32::System::Threading::*, Win32::UI::WindowsAndMessaging::*}; +use windows_sys::{ + Win32::Foundation::*, Win32::System::Threading::*, Win32::UI::WindowsAndMessaging::*, +}; fn main() { unsafe { - let event = CreateEventW(std::ptr::null_mut(), 1, 0, std::ptr::null_mut()); + let event = CreateEventW(std::ptr::null(), 1, 0, std::ptr::null()); SetEvent(event); WaitForSingleObject(event, 0); CloseHandle(event); - MessageBoxA(0, b"Text\0".as_ptr() as _, b"Caption\0".as_ptr() as _, MB_OK); + MessageBoxA(0, b"Text\0".as_ptr(), b"Caption\0".as_ptr(), MB_OK); } } ``` diff --git a/crates/libs/bindgen/src/functions.rs b/crates/libs/bindgen/src/functions.rs index affa12f02e..8af9f609f3 100644 --- a/crates/libs/bindgen/src/functions.rs +++ b/crates/libs/bindgen/src/functions.rs @@ -38,7 +38,7 @@ pub fn gen_function(def: &MethodDef, gen: &Gen) -> TokenStream { } } -fn gen_function_if(entry: &Vec, gen: &Gen) -> TokenStream { +fn gen_function_if(entry: &[ElementType], gen: &Gen) -> TokenStream { let mut tokens = TokenStream::new(); for def in entry { diff --git a/crates/libs/bindgen/src/gen.rs b/crates/libs/bindgen/src/gen.rs index acf8bf8f11..346359712e 100644 --- a/crates/libs/bindgen/src/gen.rs +++ b/crates/libs/bindgen/src/gen.rs @@ -116,10 +116,8 @@ impl Gen<'_> { } fn add_namespace(&self, namespace: &'static str, namespaces: &mut BTreeSet<&'static str>) { - if !namespace.is_empty() && namespace != self.namespace { - if !self.namespace.starts_with(format!("{}.", namespace).as_str()) { - namespaces.insert(namespace); - } + if !namespace.is_empty() && namespace != self.namespace && !self.namespace.starts_with(format!("{}.", namespace).as_str()) { + namespaces.insert(namespace); } } diff --git a/crates/libs/bindgen/src/handles.rs b/crates/libs/bindgen/src/handles.rs index 9e8d3ee4c1..9b32a76f1a 100644 --- a/crates/libs/bindgen/src/handles.rs +++ b/crates/libs/bindgen/src/handles.rs @@ -79,6 +79,10 @@ pub fn gen_win_handle(def: &TypeDef, gen: &Gen) -> TokenStream { } fn gen_signature(def: &TypeDef, gen: &Gen) -> TokenStream { - let signature = def.fields().next().map(|field| field.signature(Some(def))).unwrap(); + let mut signature = def.fields().next().map(|field| field.signature(Some(def))).unwrap(); + // Handle fields don't need to be mutable and it's easier to work with handles when + // they're `*const` rather than `*mut`. This mainly affects string handle types + // like PWSTR/PSTR/BSTR. + signature.is_const = true; gen_sig(&signature, gen) } diff --git a/crates/libs/bindgen/src/implements.rs b/crates/libs/bindgen/src/implements.rs index 32b68f924a..084764ba9f 100644 --- a/crates/libs/bindgen/src/implements.rs +++ b/crates/libs/bindgen/src/implements.rs @@ -102,6 +102,7 @@ pub fn gen(def: &TypeDef, gen: &Gen) -> TokenStream { for method in def.methods() { let name = method_names.add(&method); + // TODO: can we use core::ptr::addr_of to avoid the need for const_fn_fn_ptr_basics? methods.combine("e! { #name: #name::<#(#generics)* Identity, Impl, OFFSET>, }); } diff --git a/crates/libs/bindgen/src/replacements/bstr.rs b/crates/libs/bindgen/src/replacements/bstr.rs index 4937b07611..b64bab89d7 100644 --- a/crates/libs/bindgen/src/replacements/bstr.rs +++ b/crates/libs/bindgen/src/replacements/bstr.rs @@ -3,7 +3,7 @@ use super::*; pub fn gen() -> TokenStream { quote! { #[repr(transparent)] - pub struct BSTR(*mut u16); + pub struct BSTR(*const u16); impl BSTR { pub fn new() -> Self { Self(core::ptr::null_mut()) @@ -28,7 +28,7 @@ pub fn gen() -> TokenStream { unsafe { SysAllocStringLen( - PWSTR(value.as_ptr() as *mut _), + PWSTR(value.as_ptr()), value.len() as u32, ) } @@ -39,7 +39,7 @@ pub fn gen() -> TokenStream { return &[]; } - unsafe { ::core::slice::from_raw_parts(self.0 as *const u16, self.len()) } + unsafe { ::core::slice::from_raw_parts(self.0, self.len()) } } } impl ::core::clone::Clone for BSTR { @@ -133,7 +133,6 @@ pub fn gen() -> TokenStream { unsafe impl ::windows::core::Abi for BSTR { type Abi = ::core::mem::ManuallyDrop; } - pub type BSTR_abi = *mut u16; #[cfg(feature = "alloc")] impl<'a> ::windows::core::IntoParam<'a, BSTR> for &str { fn into_param(self) -> ::windows::core::Param<'a, BSTR> { diff --git a/crates/libs/bindgen/src/replacements/pstr.rs b/crates/libs/bindgen/src/replacements/pstr.rs index 8c29cac660..fd17a329ee 100644 --- a/crates/libs/bindgen/src/replacements/pstr.rs +++ b/crates/libs/bindgen/src/replacements/pstr.rs @@ -4,7 +4,7 @@ use super::*; pub fn gen() -> TokenStream { quote! { #[repr(transparent)] - pub struct PSTR(pub *mut u8); + pub struct PSTR(pub *const u8); impl PSTR { pub fn is_null(&self) -> bool { self.0.is_null() @@ -39,7 +39,7 @@ pub fn gen() -> TokenStream { unsafe fn drop_param(param: &mut ::windows::core::Param<'_, Self>) { if let ::windows::core::Param::Boxed(value) = param { if !value.is_null() { - ::windows::core::alloc::boxed::Box::from_raw(value.0); + ::windows::core::alloc::boxed::Box::from_raw(value.0 as *mut u8); } } } diff --git a/crates/libs/bindgen/src/replacements/pwstr.rs b/crates/libs/bindgen/src/replacements/pwstr.rs index e526ad97f6..8d6eba585a 100644 --- a/crates/libs/bindgen/src/replacements/pwstr.rs +++ b/crates/libs/bindgen/src/replacements/pwstr.rs @@ -4,7 +4,7 @@ use super::*; pub fn gen() -> TokenStream { quote! { #[repr(transparent)] - pub struct PWSTR(pub *mut u16); + pub struct PWSTR(pub *const u16); impl PWSTR { pub fn is_null(&self) -> bool { self.0.is_null() @@ -39,7 +39,7 @@ pub fn gen() -> TokenStream { unsafe fn drop_param(param: &mut ::windows::core::Param<'_, Self>) { if let ::windows::core::Param::Boxed(value) = param { if !value.is_null() { - ::windows::core::alloc::boxed::Box::from_raw(value.0); + ::windows::core::alloc::boxed::Box::from_raw(value.0 as *mut u16); } } } diff --git a/crates/libs/sys/src/Windows/Win32/Foundation/mod.rs b/crates/libs/sys/src/Windows/Win32/Foundation/mod.rs index aee5b4b88f..7eb6a05321 100644 --- a/crates/libs/sys/src/Windows/Win32/Foundation/mod.rs +++ b/crates/libs/sys/src/Windows/Win32/Foundation/mod.rs @@ -121,7 +121,7 @@ impl ::core::clone::Clone for APP_LOCAL_DEVICE_ID { pub const APP_LOCAL_DEVICE_ID_SIZE: u32 = 32u32; pub type BOOL = i32; pub type BOOLEAN = u8; -pub type BSTR = *mut u16; +pub type BSTR = *const u16; #[doc = "*Required features: 'Win32_Foundation'*"] pub const BT_E_SPURIOUS_ACTIVATION: ::windows_sys::core::HRESULT = -2146958592i32; #[doc = "*Required features: 'Win32_Foundation'*"] @@ -4836,8 +4836,8 @@ pub const PSINK_E_LARGE_ATTACHMENT: ::windows_sys::core::HRESULT = -2147215470i3 pub const PSINK_E_QUERY_ONLY: ::windows_sys::core::HRESULT = -2147215472i32; #[doc = "*Required features: 'Win32_Foundation'*"] pub const PSINK_S_LARGE_WORD: ::windows_sys::core::HRESULT = 268179i32; -pub type PSTR = *mut u8; -pub type PWSTR = *mut u16; +pub type PSTR = *const u8; +pub type PWSTR = *const u16; #[doc = "*Required features: 'Win32_Foundation'*"] pub const QPARSE_E_EXPECTING_BRACE: ::windows_sys::core::HRESULT = -2147215770i32; #[doc = "*Required features: 'Win32_Foundation'*"] diff --git a/crates/libs/sys/src/Windows/Win32/System/Threading/mod.rs b/crates/libs/sys/src/Windows/Win32/System/Threading/mod.rs index 24f4016f73..e9209da918 100644 --- a/crates/libs/sys/src/Windows/Win32/System/Threading/mod.rs +++ b/crates/libs/sys/src/Windows/Win32/System/Threading/mod.rs @@ -869,7 +869,7 @@ impl ::core::clone::Clone for IO_COUNTERS { } #[doc = "*Required features: 'Win32_System_Threading'*"] pub type LPFIBER_START_ROUTINE = ::core::option::Option; -pub type LPPROC_THREAD_ATTRIBUTE_LIST = *mut ::core::ffi::c_void; +pub type LPPROC_THREAD_ATTRIBUTE_LIST = *const ::core::ffi::c_void; #[doc = "*Required features: 'Win32_System_Threading'*"] pub type LPTHREAD_START_ROUTINE = ::core::option::Option u32>; #[doc = "*Required features: 'Win32_System_Threading'*"] diff --git a/crates/libs/windows/src/Windows/Win32/Foundation/mod.rs b/crates/libs/windows/src/Windows/Win32/Foundation/mod.rs index 6b6f076d67..afb83c9eed 100644 --- a/crates/libs/windows/src/Windows/Win32/Foundation/mod.rs +++ b/crates/libs/windows/src/Windows/Win32/Foundation/mod.rs @@ -232,7 +232,7 @@ unsafe impl ::windows::core::Abi for BOOLEAN { type Abi = Self; } #[repr(transparent)] -pub struct BSTR(*mut u16); +pub struct BSTR(*const u16); impl BSTR { pub fn new() -> Self { Self(core::ptr::null_mut()) @@ -251,13 +251,13 @@ impl BSTR { if value.is_empty() { return Self(::core::ptr::null_mut()); } - unsafe { SysAllocStringLen(PWSTR(value.as_ptr() as *mut _), value.len() as u32) } + unsafe { SysAllocStringLen(PWSTR(value.as_ptr()), value.len() as u32) } } pub fn as_wide(&self) -> &[u16] { if self.0.is_null() { return &[]; } - unsafe { ::core::slice::from_raw_parts(self.0 as *const u16, self.len()) } + unsafe { ::core::slice::from_raw_parts(self.0, self.len()) } } } impl ::core::clone::Clone for BSTR { @@ -348,7 +348,6 @@ impl ::core::ops::Drop for BSTR { unsafe impl ::windows::core::Abi for BSTR { type Abi = ::core::mem::ManuallyDrop; } -pub type BSTR_abi = *mut u16; #[cfg(feature = "alloc")] impl<'a> ::windows::core::IntoParam<'a, BSTR> for &str { fn into_param(self) -> ::windows::core::Param<'a, BSTR> { @@ -5885,7 +5884,7 @@ pub const PSINK_E_QUERY_ONLY: ::windows::core::HRESULT = ::windows::core::HRESUL #[doc = "*Required features: 'Win32_Foundation'*"] pub const PSINK_S_LARGE_WORD: ::windows::core::HRESULT = ::windows::core::HRESULT(268179i32); #[repr(transparent)] -pub struct PSTR(pub *mut u8); +pub struct PSTR(pub *const u8); impl PSTR { pub fn is_null(&self) -> bool { self.0.is_null() @@ -5919,7 +5918,7 @@ unsafe impl ::windows::core::Abi for PSTR { unsafe fn drop_param(param: &mut ::windows::core::Param<'_, Self>) { if let ::windows::core::Param::Boxed(value) = param { if !value.is_null() { - ::windows::core::alloc::boxed::Box::from_raw(value.0); + ::windows::core::alloc::boxed::Box::from_raw(value.0 as *mut u8); } } } @@ -5937,7 +5936,7 @@ impl<'a> ::windows::core::IntoParam<'a, PSTR> for ::windows::core::alloc::string } } #[repr(transparent)] -pub struct PWSTR(pub *mut u16); +pub struct PWSTR(pub *const u16); impl PWSTR { pub fn is_null(&self) -> bool { self.0.is_null() @@ -5971,7 +5970,7 @@ unsafe impl ::windows::core::Abi for PWSTR { unsafe fn drop_param(param: &mut ::windows::core::Param<'_, Self>) { if let ::windows::core::Param::Boxed(value) = param { if !value.is_null() { - ::windows::core::alloc::boxed::Box::from_raw(value.0); + ::windows::core::alloc::boxed::Box::from_raw(value.0 as *mut u16); } } } diff --git a/crates/libs/windows/src/Windows/Win32/System/Threading/mod.rs b/crates/libs/windows/src/Windows/Win32/System/Threading/mod.rs index 0d5b346fee..760d2e470e 100644 --- a/crates/libs/windows/src/Windows/Win32/System/Threading/mod.rs +++ b/crates/libs/windows/src/Windows/Win32/System/Threading/mod.rs @@ -2735,7 +2735,7 @@ pub unsafe fn IsWow64Process2<'a, Param0: ::windows::core::IntoParam<'a, super:: pub type LPFIBER_START_ROUTINE = ::core::option::Option; #[repr(transparent)] #[derive(:: core :: cmp :: PartialEq, :: core :: cmp :: Eq)] -pub struct LPPROC_THREAD_ATTRIBUTE_LIST(pub *mut ::core::ffi::c_void); +pub struct LPPROC_THREAD_ATTRIBUTE_LIST(pub *const ::core::ffi::c_void); impl LPPROC_THREAD_ATTRIBUTE_LIST { pub fn is_invalid(&self) -> bool { *self == unsafe { ::core::mem::zeroed() } diff --git a/crates/libs/windows/src/core/bindings.rs b/crates/libs/windows/src/core/bindings.rs index bba550d309..d206d01002 100644 --- a/crates/libs/windows/src/core/bindings.rs +++ b/crates/libs/windows/src/core/bindings.rs @@ -1176,7 +1176,7 @@ impl<'a> ::windows::core::IntoParam<'a, BOOL> for bool { } } #[repr(transparent)] -pub struct BSTR(*mut u16); +pub struct BSTR(*const u16); impl BSTR { pub fn new() -> Self { Self(core::ptr::null_mut()) @@ -1195,13 +1195,13 @@ impl BSTR { if value.is_empty() { return Self(::core::ptr::null_mut()); } - unsafe { SysAllocStringLen(PWSTR(value.as_ptr() as *mut _), value.len() as u32) } + unsafe { SysAllocStringLen(PWSTR(value.as_ptr()), value.len() as u32) } } pub fn as_wide(&self) -> &[u16] { if self.0.is_null() { return &[]; } - unsafe { ::core::slice::from_raw_parts(self.0 as *const u16, self.len()) } + unsafe { ::core::slice::from_raw_parts(self.0, self.len()) } } } impl ::core::clone::Clone for BSTR { @@ -1292,7 +1292,6 @@ impl ::core::ops::Drop for BSTR { unsafe impl ::windows::core::Abi for BSTR { type Abi = ::core::mem::ManuallyDrop; } -pub type BSTR_abi = *mut u16; #[cfg(feature = "alloc")] impl<'a> ::windows::core::IntoParam<'a, BSTR> for &str { fn into_param(self) -> ::windows::core::Param<'a, BSTR> { @@ -1410,7 +1409,7 @@ unsafe impl ::windows::core::Abi for HINSTANCE { type Abi = Self; } #[repr(transparent)] -pub struct PSTR(pub *mut u8); +pub struct PSTR(pub *const u8); impl PSTR { pub fn is_null(&self) -> bool { self.0.is_null() @@ -1444,7 +1443,7 @@ unsafe impl ::windows::core::Abi for PSTR { unsafe fn drop_param(param: &mut ::windows::core::Param<'_, Self>) { if let ::windows::core::Param::Boxed(value) = param { if !value.is_null() { - ::windows::core::alloc::boxed::Box::from_raw(value.0); + ::windows::core::alloc::boxed::Box::from_raw(value.0 as *mut u8); } } } @@ -1462,7 +1461,7 @@ impl<'a> ::windows::core::IntoParam<'a, PSTR> for ::windows::core::alloc::string } } #[repr(transparent)] -pub struct PWSTR(pub *mut u16); +pub struct PWSTR(pub *const u16); impl PWSTR { pub fn is_null(&self) -> bool { self.0.is_null() @@ -1496,7 +1495,7 @@ unsafe impl ::windows::core::Abi for PWSTR { unsafe fn drop_param(param: &mut ::windows::core::Param<'_, Self>) { if let ::windows::core::Param::Boxed(value) = param { if !value.is_null() { - ::windows::core::alloc::boxed::Box::from_raw(value.0); + ::windows::core::alloc::boxed::Box::from_raw(value.0 as *mut u16); } } } diff --git a/crates/samples/create_window/src/main.rs b/crates/samples/create_window/src/main.rs index 016cecdada..76b9262d31 100644 --- a/crates/samples/create_window/src/main.rs +++ b/crates/samples/create_window/src/main.rs @@ -10,7 +10,7 @@ fn main() -> Result<()> { let wc = WNDCLASSA { hCursor: LoadCursorW(None, IDC_ARROW), hInstance: instance, - lpszClassName: PSTR(b"window\0".as_ptr() as _), + lpszClassName: PSTR(b"window\0".as_ptr()), style: CS_HREDRAW | CS_VREDRAW, lpfnWndProc: Some(wndproc), diff --git a/crates/samples/create_window_sys/src/main.rs b/crates/samples/create_window_sys/src/main.rs index 9a8774d193..76ba7e206f 100644 --- a/crates/samples/create_window_sys/src/main.rs +++ b/crates/samples/create_window_sys/src/main.rs @@ -5,7 +5,7 @@ fn main() { let instance = GetModuleHandleA(std::ptr::null_mut()); debug_assert!(instance != 0); - let window_class = b"window\0".as_ptr() as _; + let window_class = b"window\0".as_ptr(); let wc = WNDCLASSA { hCursor: LoadCursorW(0, IDC_ARROW), @@ -23,7 +23,7 @@ fn main() { let atom = RegisterClassA(&wc); debug_assert!(atom != 0); - CreateWindowExA(0, window_class, b"This is a sample window".as_ptr() as _, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, instance, std::ptr::null_mut()); + CreateWindowExA(0, window_class, b"This is a sample window\0".as_ptr(), WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, instance, std::ptr::null_mut()); let mut message = std::mem::zeroed(); diff --git a/crates/samples/direct2d/src/main.rs b/crates/samples/direct2d/src/main.rs index 01649b1b52..8e2cd3d7bb 100644 --- a/crates/samples/direct2d/src/main.rs +++ b/crates/samples/direct2d/src/main.rs @@ -315,7 +315,7 @@ impl Window { let wc = WNDCLASSA { hCursor: LoadCursorW(None, IDC_HAND), hInstance: instance, - lpszClassName: PSTR(b"window\0".as_ptr() as _), + lpszClassName: PSTR(b"window\0".as_ptr()), style: CS_HREDRAW | CS_VREDRAW, lpfnWndProc: Some(Self::wndproc), @@ -325,7 +325,7 @@ impl Window { let atom = RegisterClassA(&wc); debug_assert!(atom != 0); - let handle = CreateWindowExA(Default::default(), PSTR(b"window\0".as_ptr() as _), PSTR(b"Sample Window\0".as_ptr() as _), WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, None, None, instance, self as *mut _ as _); + let handle = CreateWindowExA(Default::default(), PSTR(b"window\0".as_ptr()), PSTR(b"Sample Window\0".as_ptr()), WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, None, None, instance, self as *mut _ as _); debug_assert!(handle.0 != 0); debug_assert!(handle == self.handle); diff --git a/crates/samples/direct3d12/src/main.rs b/crates/samples/direct3d12/src/main.rs index e318b3ea07..fd8be86614 100644 --- a/crates/samples/direct3d12/src/main.rs +++ b/crates/samples/direct3d12/src/main.rs @@ -53,7 +53,7 @@ where lpfnWndProc: Some(wndproc::), hInstance: instance, hCursor: unsafe { LoadCursorW(None, IDC_ARROW) }, - lpszClassName: PSTR(b"RustWindowClass\0".as_ptr() as _), + lpszClassName: PSTR(b"RustWindowClass\0".as_ptr()), ..Default::default() }; @@ -459,7 +459,7 @@ mod d3d12_hello_triangle { let mut input_element_descs: [D3D12_INPUT_ELEMENT_DESC; 2] = [ D3D12_INPUT_ELEMENT_DESC { - SemanticName: PSTR(b"POSITION\0".as_ptr() as _), + SemanticName: PSTR(b"POSITION\0".as_ptr()), SemanticIndex: 0, Format: DXGI_FORMAT_R32G32B32_FLOAT, InputSlot: 0, @@ -468,7 +468,7 @@ mod d3d12_hello_triangle { InstanceDataStepRate: 0, }, D3D12_INPUT_ELEMENT_DESC { - SemanticName: PSTR(b"COLOR\0".as_ptr() as _), + SemanticName: PSTR(b"COLOR\0".as_ptr()), SemanticIndex: 0, Format: DXGI_FORMAT_R32G32B32A32_FLOAT, InputSlot: 0,