Skip to content

Commit

Permalink
Make handle fields const (#1468)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Jan 27, 2022
1 parent 44f7804 commit d2e49d9
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 46 deletions.
8 changes: 5 additions & 3 deletions .github/readme.md
Expand Up @@ -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);
}
}
```
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/functions.rs
Expand Up @@ -38,7 +38,7 @@ pub fn gen_function(def: &MethodDef, gen: &Gen) -> TokenStream {
}
}

fn gen_function_if(entry: &Vec<ElementType>, gen: &Gen) -> TokenStream {
fn gen_function_if(entry: &[ElementType], gen: &Gen) -> TokenStream {
let mut tokens = TokenStream::new();

for def in entry {
Expand Down
6 changes: 2 additions & 4 deletions crates/libs/bindgen/src/gen.rs
Expand Up @@ -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);
}
}

Expand Down
6 changes: 5 additions & 1 deletion crates/libs/bindgen/src/handles.rs
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions crates/libs/bindgen/src/implements.rs
Expand Up @@ -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(&quote! { #name: #name::<#(#generics)* Identity, Impl, OFFSET>, });
}

Expand Down
7 changes: 3 additions & 4 deletions crates/libs/bindgen/src/replacements/bstr.rs
Expand Up @@ -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())
Expand All @@ -28,7 +28,7 @@ pub fn gen() -> TokenStream {

unsafe {
SysAllocStringLen(
PWSTR(value.as_ptr() as *mut _),
PWSTR(value.as_ptr()),
value.len() as u32,
)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -133,7 +133,6 @@ pub fn gen() -> TokenStream {
unsafe impl ::windows::core::Abi for BSTR {
type Abi = ::core::mem::ManuallyDrop<Self>;
}
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> {
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/replacements/pstr.rs
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/replacements/pwstr.rs
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/libs/sys/src/Windows/Win32/Foundation/mod.rs
Expand Up @@ -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'*"]
Expand Down Expand Up @@ -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'*"]
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/sys/src/Windows/Win32/System/Threading/mod.rs
Expand Up @@ -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<unsafe extern "system" fn(lpfiberparameter: *mut ::core::ffi::c_void)>;
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<unsafe extern "system" fn(lpthreadparameter: *mut ::core::ffi::c_void) -> u32>;
#[doc = "*Required features: 'Win32_System_Threading'*"]
Expand Down
15 changes: 7 additions & 8 deletions crates/libs/windows/src/Windows/Win32/Foundation/mod.rs
Expand Up @@ -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())
Expand All @@ -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 {
Expand Down Expand Up @@ -348,7 +348,6 @@ impl ::core::ops::Drop for BSTR {
unsafe impl ::windows::core::Abi for BSTR {
type Abi = ::core::mem::ManuallyDrop<Self>;
}
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> {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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()
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
Expand Up @@ -2735,7 +2735,7 @@ pub unsafe fn IsWow64Process2<'a, Param0: ::windows::core::IntoParam<'a, super::
pub type LPFIBER_START_ROUTINE = ::core::option::Option<unsafe extern "system" fn(lpfiberparameter: *mut ::core::ffi::c_void)>;
#[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() }
Expand Down
15 changes: 7 additions & 8 deletions crates/libs/windows/src/core/bindings.rs
Expand Up @@ -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())
Expand All @@ -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 {
Expand Down Expand Up @@ -1292,7 +1292,6 @@ impl ::core::ops::Drop for BSTR {
unsafe impl ::windows::core::Abi for BSTR {
type Abi = ::core::mem::ManuallyDrop<Self>;
}
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> {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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()
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/samples/create_window/src/main.rs
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions crates/samples/create_window_sys/src/main.rs
Expand Up @@ -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),
Expand All @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions crates/samples/direct2d/src/main.rs
Expand Up @@ -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),
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions crates/samples/direct3d12/src/main.rs
Expand Up @@ -53,7 +53,7 @@ where
lpfnWndProc: Some(wndproc::<S>),
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()
};

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit d2e49d9

Please sign in to comment.