From 87f268ad69d9a97f0cec7e8f651ea8ab95f80774 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Mon, 13 Jan 2025 13:59:42 -0600 Subject: [PATCH] Use `Ref` for generic type parameters (#3435) --- crates/libs/bindgen/src/types/method.rs | 5 ++- crates/libs/core/src/type.rs | 8 ++++ .../src/Windows/Foundation/Collections/mod.rs | 44 +++++++++---------- .../windows/src/Windows/Foundation/mod.rs | 32 +++++++------- .../Foundation/Collections/MapView.rs | 10 ++--- .../Foundation/Collections/VectorView.rs | 6 +-- crates/tests/bindgen/src/class_dep.rs | 4 +- crates/tests/bindgen/src/delegate_generic.rs | 8 ++-- .../tests/bindgen/src/interface_iterable.rs | 16 +++---- .../misc/implement/tests/generic_default.rs | 8 +++- .../misc/implement/tests/generic_generic.rs | 2 +- .../misc/implement/tests/generic_primitive.rs | 4 +- .../implement/tests/generic_stringable.rs | 2 +- crates/tests/misc/implement/tests/map.rs | 12 ++--- crates/tests/misc/implement/tests/vector.rs | 23 +++++----- crates/tests/winrt/event_core/tests/tests.rs | 8 ++-- crates/tests/winrt/events_client/src/lib.rs | 18 ++++---- crates/tests/winrt/old/tests/delegates.rs | 2 +- 18 files changed, 115 insertions(+), 97 deletions(-) diff --git a/crates/libs/bindgen/src/types/method.rs b/crates/libs/bindgen/src/types/method.rs index a6555bdf4a..17615d0a5a 100644 --- a/crates/libs/bindgen/src/types/method.rs +++ b/crates/libs/bindgen/src/types/method.rs @@ -47,7 +47,7 @@ impl Method { quote! { core::slice::from_raw_parts(core::mem::transmute_copy(&#name), #abi_size_name as usize) } } else if param.0.is_primitive() { quote! { #name } - } else if param.0.is_const_ref() || param.0.is_interface() { + } else if param.0.is_const_ref() || param.0.is_interface() || matches!(¶m.0, Type::Param(_)) { quote! { core::mem::transmute_copy(&#name) } } else { quote! { core::mem::transmute(&#name) } @@ -155,6 +155,9 @@ impl Method { } else if p.0.is_interface() { let type_name = p.0.write_name(writer); quote! { windows_core::Ref<#type_name> } + } else if matches!(&p.0, Type::Param(_)) { + let type_name = p.0.write_name(writer); + quote! { <#type_name as windows_core::Type<#type_name>>::Ref } } else { quote! { &#default_type } } diff --git a/crates/libs/core/src/type.rs b/crates/libs/core/src/type.rs index a64f191d0a..3cc42de94a 100644 --- a/crates/libs/core/src/type.rs +++ b/crates/libs/core/src/type.rs @@ -17,12 +17,17 @@ pub struct CopyType; #[doc(hidden)] pub trait Type::TypeKind>: TypeKind + Sized + Clone { type Abi; + type Ref; type Default; fn is_null(abi: &Self::Abi) -> bool; unsafe fn assume_init_ref(abi: &Self::Abi) -> &Self; unsafe fn from_abi(abi: Self::Abi) -> Result; fn from_default(default: &Self::Default) -> Result; + + fn deref(param: &Self::Ref) -> &Self::Default { + unsafe { core::mem::transmute(param) } + } } impl Type for T @@ -30,6 +35,7 @@ where T: TypeKind + Clone, { type Abi = *mut core::ffi::c_void; + type Ref = Ref; type Default = Option; fn is_null(abi: &Self::Abi) -> bool { @@ -60,6 +66,7 @@ where T: TypeKind + Clone, { type Abi = core::mem::MaybeUninit; + type Ref = Ref; type Default = Self; fn is_null(_: &Self::Abi) -> bool { @@ -84,6 +91,7 @@ where T: TypeKind + Clone, { type Abi = Self; + type Ref = Self; type Default = Self; fn is_null(_: &Self::Abi) -> bool { diff --git a/crates/libs/windows/src/Windows/Foundation/Collections/mod.rs b/crates/libs/windows/src/Windows/Foundation/Collections/mod.rs index 607f5fc3c9..75978e349e 100644 --- a/crates/libs/windows/src/Windows/Foundation/Collections/mod.rs +++ b/crates/libs/windows/src/Windows/Foundation/Collections/mod.rs @@ -434,12 +434,12 @@ where K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'static, { - fn Lookup(&self, key: &>::Default) -> windows_core::Result; + fn Lookup(&self, key: >::Ref) -> windows_core::Result; fn Size(&self) -> windows_core::Result; - fn HasKey(&self, key: &>::Default) -> windows_core::Result; + fn HasKey(&self, key: >::Ref) -> windows_core::Result; fn GetView(&self) -> windows_core::Result>; - fn Insert(&self, key: &>::Default, value: &>::Default) -> windows_core::Result; - fn Remove(&self, key: &>::Default) -> windows_core::Result<()>; + fn Insert(&self, key: >::Ref, value: >::Ref) -> windows_core::Result; + fn Remove(&self, key: >::Ref) -> windows_core::Result<()>; fn Clear(&self) -> windows_core::Result<()>; } impl IMap_Vtbl { @@ -447,7 +447,7 @@ impl, const OFFSET: isize>(this: *mut core::ffi::c_void, key: windows_core::AbiType, result__: *mut windows_core::AbiType) -> windows_core::HRESULT { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - match IMap_Impl::Lookup(this, core::mem::transmute(&key)) { + match IMap_Impl::Lookup(this, core::mem::transmute_copy(&key)) { Ok(ok__) => { result__.write(core::mem::transmute_copy(&ok__)); core::mem::forget(ok__); @@ -472,7 +472,7 @@ impl, const OFFSET: isize>(this: *mut core::ffi::c_void, key: windows_core::AbiType, result__: *mut bool) -> windows_core::HRESULT { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - match IMap_Impl::HasKey(this, core::mem::transmute(&key)) { + match IMap_Impl::HasKey(this, core::mem::transmute_copy(&key)) { Ok(ok__) => { result__.write(core::mem::transmute_copy(&ok__)); windows_core::HRESULT(0) @@ -497,7 +497,7 @@ impl, const OFFSET: isize>(this: *mut core::ffi::c_void, key: windows_core::AbiType, value: windows_core::AbiType, result__: *mut bool) -> windows_core::HRESULT { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - match IMap_Impl::Insert(this, core::mem::transmute(&key), core::mem::transmute(&value)) { + match IMap_Impl::Insert(this, core::mem::transmute_copy(&key), core::mem::transmute_copy(&value)) { Ok(ok__) => { result__.write(core::mem::transmute_copy(&ok__)); windows_core::HRESULT(0) @@ -509,7 +509,7 @@ impl, const OFFSET: isize>(this: *mut core::ffi::c_void, key: windows_core::AbiType) -> windows_core::HRESULT { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IMap_Impl::Remove(this, core::mem::transmute(&key)).into() + IMap_Impl::Remove(this, core::mem::transmute_copy(&key)).into() } } unsafe extern "system" fn Clear, const OFFSET: isize>(this: *mut core::ffi::c_void) -> windows_core::HRESULT { @@ -720,9 +720,9 @@ where K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'static, { - fn Lookup(&self, key: &>::Default) -> windows_core::Result; + fn Lookup(&self, key: >::Ref) -> windows_core::Result; fn Size(&self) -> windows_core::Result; - fn HasKey(&self, key: &>::Default) -> windows_core::Result; + fn HasKey(&self, key: >::Ref) -> windows_core::Result; fn Split(&self, first: windows_core::OutRef<'_, IMapView>, second: windows_core::OutRef<'_, IMapView>) -> windows_core::Result<()>; } impl IMapView_Vtbl { @@ -730,7 +730,7 @@ impl, const OFFSET: isize>(this: *mut core::ffi::c_void, key: windows_core::AbiType, result__: *mut windows_core::AbiType) -> windows_core::HRESULT { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - match IMapView_Impl::Lookup(this, core::mem::transmute(&key)) { + match IMapView_Impl::Lookup(this, core::mem::transmute_copy(&key)) { Ok(ok__) => { result__.write(core::mem::transmute_copy(&ok__)); core::mem::forget(ok__); @@ -755,7 +755,7 @@ impl, const OFFSET: isize>(this: *mut core::ffi::c_void, key: windows_core::AbiType, result__: *mut bool) -> windows_core::HRESULT { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - match IMapView_Impl::HasKey(this, core::mem::transmute(&key)) { + match IMapView_Impl::HasKey(this, core::mem::transmute_copy(&key)) { Ok(ok__) => { result__.write(core::mem::transmute_copy(&ok__)); windows_core::HRESULT(0) @@ -1380,11 +1380,11 @@ where fn GetAt(&self, index: u32) -> windows_core::Result; fn Size(&self) -> windows_core::Result; fn GetView(&self) -> windows_core::Result>; - fn IndexOf(&self, value: &>::Default, index: &mut u32) -> windows_core::Result; - fn SetAt(&self, index: u32, value: &>::Default) -> windows_core::Result<()>; - fn InsertAt(&self, index: u32, value: &>::Default) -> windows_core::Result<()>; + fn IndexOf(&self, value: >::Ref, index: &mut u32) -> windows_core::Result; + fn SetAt(&self, index: u32, value: >::Ref) -> windows_core::Result<()>; + fn InsertAt(&self, index: u32, value: >::Ref) -> windows_core::Result<()>; fn RemoveAt(&self, index: u32) -> windows_core::Result<()>; - fn Append(&self, value: &>::Default) -> windows_core::Result<()>; + fn Append(&self, value: >::Ref) -> windows_core::Result<()>; fn RemoveAtEnd(&self) -> windows_core::Result<()>; fn Clear(&self) -> windows_core::Result<()>; fn GetMany(&self, startIndex: u32, items: &mut [>::Default]) -> windows_core::Result; @@ -1433,7 +1433,7 @@ impl IVector_Vtbl { unsafe extern "system" fn IndexOf, const OFFSET: isize>(this: *mut core::ffi::c_void, value: windows_core::AbiType, index: *mut u32, result__: *mut bool) -> windows_core::HRESULT { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - match IVector_Impl::IndexOf(this, core::mem::transmute(&value), core::mem::transmute_copy(&index)) { + match IVector_Impl::IndexOf(this, core::mem::transmute_copy(&value), core::mem::transmute_copy(&index)) { Ok(ok__) => { result__.write(core::mem::transmute_copy(&ok__)); windows_core::HRESULT(0) @@ -1445,13 +1445,13 @@ impl IVector_Vtbl { unsafe extern "system" fn SetAt, const OFFSET: isize>(this: *mut core::ffi::c_void, index: u32, value: windows_core::AbiType) -> windows_core::HRESULT { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::SetAt(this, index, core::mem::transmute(&value)).into() + IVector_Impl::SetAt(this, index, core::mem::transmute_copy(&value)).into() } } unsafe extern "system" fn InsertAt, const OFFSET: isize>(this: *mut core::ffi::c_void, index: u32, value: windows_core::AbiType) -> windows_core::HRESULT { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::InsertAt(this, index, core::mem::transmute(&value)).into() + IVector_Impl::InsertAt(this, index, core::mem::transmute_copy(&value)).into() } } unsafe extern "system" fn RemoveAt, const OFFSET: isize>(this: *mut core::ffi::c_void, index: u32) -> windows_core::HRESULT { @@ -1463,7 +1463,7 @@ impl IVector_Vtbl { unsafe extern "system" fn Append, const OFFSET: isize>(this: *mut core::ffi::c_void, value: windows_core::AbiType) -> windows_core::HRESULT { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::Append(this, core::mem::transmute(&value)).into() + IVector_Impl::Append(this, core::mem::transmute_copy(&value)).into() } } unsafe extern "system" fn RemoveAtEnd, const OFFSET: isize>(this: *mut core::ffi::c_void) -> windows_core::HRESULT { @@ -1687,7 +1687,7 @@ where { fn GetAt(&self, index: u32) -> windows_core::Result; fn Size(&self) -> windows_core::Result; - fn IndexOf(&self, value: &>::Default, index: &mut u32) -> windows_core::Result; + fn IndexOf(&self, value: >::Ref, index: &mut u32) -> windows_core::Result; fn GetMany(&self, startIndex: u32, items: &mut [>::Default]) -> windows_core::Result; } impl IVectorView_Vtbl { @@ -1720,7 +1720,7 @@ impl IVectorView_Vtbl { unsafe extern "system" fn IndexOf, const OFFSET: isize>(this: *mut core::ffi::c_void, value: windows_core::AbiType, index: *mut u32, result__: *mut bool) -> windows_core::HRESULT { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - match IVectorView_Impl::IndexOf(this, core::mem::transmute(&value), core::mem::transmute_copy(&index)) { + match IVectorView_Impl::IndexOf(this, core::mem::transmute_copy(&value), core::mem::transmute_copy(&index)) { Ok(ok__) => { result__.write(core::mem::transmute_copy(&ok__)); windows_core::HRESULT(0) diff --git a/crates/libs/windows/src/Windows/Foundation/mod.rs b/crates/libs/windows/src/Windows/Foundation/mod.rs index c3a45d030b..887ceaf5e9 100644 --- a/crates/libs/windows/src/Windows/Foundation/mod.rs +++ b/crates/libs/windows/src/Windows/Foundation/mod.rs @@ -87,7 +87,7 @@ impl windows_core::RuntimeType f const SIGNATURE: windows_core::imp::ConstBuffer = windows_core::imp::ConstBuffer::new().push_slice(b"pinterface({6d844858-0cff-4590-ae89-95a5a5c8b4b8}").push_slice(b";").push_other(TProgress::SIGNATURE).push_slice(b")"); } impl AsyncActionProgressHandler { - pub fn new>, &>::Default) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self { + pub fn new>, >::Ref) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self { let com = AsyncActionProgressHandlerBox { vtable: &AsyncActionProgressHandlerBox::::VTABLE, count: windows_core::imp::RefCount::new(1), invoke }; unsafe { core::mem::transmute(windows_core::imp::Box::new(com)) } } @@ -110,7 +110,7 @@ where TProgress: core::marker::PhantomData, } #[repr(C)] -struct AsyncActionProgressHandlerBox>, &>::Default) -> windows_core::Result<()> + Send + 'static> +struct AsyncActionProgressHandlerBox>, >::Ref) -> windows_core::Result<()> + Send + 'static> where TProgress: windows_core::RuntimeType + 'static, { @@ -118,7 +118,7 @@ where invoke: F, count: windows_core::imp::RefCount, } -impl>, &>::Default) -> windows_core::Result<()> + Send + 'static> AsyncActionProgressHandlerBox { +impl>, >::Ref) -> windows_core::Result<()> + Send + 'static> AsyncActionProgressHandlerBox { const VTABLE: AsyncActionProgressHandler_Vtbl = AsyncActionProgressHandler_Vtbl:: { base__: windows_core::IUnknown_Vtbl { QueryInterface: Self::QueryInterface, AddRef: Self::AddRef, Release: Self::Release }, Invoke: Self::Invoke, @@ -158,7 +158,7 @@ impl) -> windows_core::HRESULT { unsafe { let this = &mut *(this as *mut *mut core::ffi::c_void as *mut Self); - (this.invoke)(core::mem::transmute_copy(&asyncinfo), core::mem::transmute(&progressinfo)).into() + (this.invoke)(core::mem::transmute_copy(&asyncinfo), core::mem::transmute_copy(&progressinfo)).into() } } } @@ -350,7 +350,7 @@ impl AsyncOperationProgressHandler { - pub fn new>, &>::Default) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self { + pub fn new>, >::Ref) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self { let com = AsyncOperationProgressHandlerBox { vtable: &AsyncOperationProgressHandlerBox::::VTABLE, count: windows_core::imp::RefCount::new(1), invoke }; unsafe { core::mem::transmute(windows_core::imp::Box::new(com)) } } @@ -375,7 +375,7 @@ where TProgress: core::marker::PhantomData, } #[repr(C)] -struct AsyncOperationProgressHandlerBox>, &>::Default) -> windows_core::Result<()> + Send + 'static> +struct AsyncOperationProgressHandlerBox>, >::Ref) -> windows_core::Result<()> + Send + 'static> where TResult: windows_core::RuntimeType + 'static, TProgress: windows_core::RuntimeType + 'static, @@ -384,7 +384,7 @@ where invoke: F, count: windows_core::imp::RefCount, } -impl>, &>::Default) -> windows_core::Result<()> + Send + 'static> AsyncOperationProgressHandlerBox { +impl>, >::Ref) -> windows_core::Result<()> + Send + 'static> AsyncOperationProgressHandlerBox { const VTABLE: AsyncOperationProgressHandler_Vtbl = AsyncOperationProgressHandler_Vtbl:: { base__: windows_core::IUnknown_Vtbl { QueryInterface: Self::QueryInterface, AddRef: Self::AddRef, Release: Self::Release }, Invoke: Self::Invoke, @@ -425,7 +425,7 @@ impl) -> windows_core::HRESULT { unsafe { let this = &mut *(this as *mut *mut core::ffi::c_void as *mut Self); - (this.invoke)(core::mem::transmute_copy(&asyncinfo), core::mem::transmute(&progressinfo)).into() + (this.invoke)(core::mem::transmute_copy(&asyncinfo), core::mem::transmute_copy(&progressinfo)).into() } } } @@ -665,7 +665,7 @@ impl windows_core::RuntimeType for Event const SIGNATURE: windows_core::imp::ConstBuffer = windows_core::imp::ConstBuffer::new().push_slice(b"pinterface({9de1c535-6ae1-11e0-84e1-18a905bcc53f}").push_slice(b";").push_other(T::SIGNATURE).push_slice(b")"); } impl EventHandler { - pub fn new, &>::Default) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self { + pub fn new, >::Ref) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self { let com = EventHandlerBox { vtable: &EventHandlerBox::::VTABLE, count: windows_core::imp::RefCount::new(1), invoke }; unsafe { core::mem::transmute(windows_core::imp::Box::new(com)) } } @@ -688,7 +688,7 @@ where T: core::marker::PhantomData, } #[repr(C)] -struct EventHandlerBox, &>::Default) -> windows_core::Result<()> + Send + 'static> +struct EventHandlerBox, >::Ref) -> windows_core::Result<()> + Send + 'static> where T: windows_core::RuntimeType + 'static, { @@ -696,7 +696,7 @@ where invoke: F, count: windows_core::imp::RefCount, } -impl, &>::Default) -> windows_core::Result<()> + Send + 'static> EventHandlerBox { +impl, >::Ref) -> windows_core::Result<()> + Send + 'static> EventHandlerBox { const VTABLE: EventHandler_Vtbl = EventHandler_Vtbl:: { base__: windows_core::IUnknown_Vtbl { QueryInterface: Self::QueryInterface, AddRef: Self::AddRef, Release: Self::Release }, Invoke: Self::Invoke, @@ -736,7 +736,7 @@ impl) -> windows_core::HRESULT { unsafe { let this = &mut *(this as *mut *mut core::ffi::c_void as *mut Self); - (this.invoke)(core::mem::transmute_copy(&sender), core::mem::transmute(&args)).into() + (this.invoke)(core::mem::transmute_copy(&sender), core::mem::transmute_copy(&args)).into() } } } @@ -3685,7 +3685,7 @@ impl TypedEventHandler { - pub fn new>::Default, &>::Default) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self { + pub fn new>::Ref, >::Ref) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self { let com = TypedEventHandlerBox { vtable: &TypedEventHandlerBox::::VTABLE, count: windows_core::imp::RefCount::new(1), invoke }; unsafe { core::mem::transmute(windows_core::imp::Box::new(com)) } } @@ -3710,7 +3710,7 @@ where TResult: core::marker::PhantomData, } #[repr(C)] -struct TypedEventHandlerBox>::Default, &>::Default) -> windows_core::Result<()> + Send + 'static> +struct TypedEventHandlerBox>::Ref, >::Ref) -> windows_core::Result<()> + Send + 'static> where TSender: windows_core::RuntimeType + 'static, TResult: windows_core::RuntimeType + 'static, @@ -3719,7 +3719,7 @@ where invoke: F, count: windows_core::imp::RefCount, } -impl>::Default, &>::Default) -> windows_core::Result<()> + Send + 'static> TypedEventHandlerBox { +impl>::Ref, >::Ref) -> windows_core::Result<()> + Send + 'static> TypedEventHandlerBox { const VTABLE: TypedEventHandler_Vtbl = TypedEventHandler_Vtbl:: { base__: windows_core::IUnknown_Vtbl { QueryInterface: Self::QueryInterface, AddRef: Self::AddRef, Release: Self::Release }, Invoke: Self::Invoke, @@ -3760,7 +3760,7 @@ impl, args: windows_core::AbiType) -> windows_core::HRESULT { unsafe { let this = &mut *(this as *mut *mut core::ffi::c_void as *mut Self); - (this.invoke)(core::mem::transmute(&sender), core::mem::transmute(&args)).into() + (this.invoke)(core::mem::transmute_copy(&sender), core::mem::transmute_copy(&args)).into() } } } diff --git a/crates/libs/windows/src/extensions/Foundation/Collections/MapView.rs b/crates/libs/windows/src/extensions/Foundation/Collections/MapView.rs index f714e8563c..312a25662a 100644 --- a/crates/libs/windows/src/extensions/Foundation/Collections/MapView.rs +++ b/crates/libs/windows/src/extensions/Foundation/Collections/MapView.rs @@ -1,4 +1,4 @@ -use crate::Foundation::Collections::{IIterable, IIterable_Impl, IIterator, IIterator_Impl, IKeyValuePair, IKeyValuePair_Impl, IMapView, IMapView_Impl}; +use crate::Foundation::Collections::*; #[windows_core::implement(IMapView, IIterable>)] struct StockMapView @@ -32,15 +32,15 @@ where K::Default: Clone + Ord, V::Default: Clone, { - fn Lookup(&self, key: &K::Default) -> windows_core::Result { - let value = self.map.get(key).ok_or_else(|| windows_core::Error::from(windows_core::imp::E_BOUNDS))?; + fn Lookup(&self, key: K::Ref) -> windows_core::Result { + let value = self.map.get(K::deref(&key)).ok_or_else(|| windows_core::Error::from(windows_core::imp::E_BOUNDS))?; V::from_default(value) } fn Size(&self) -> windows_core::Result { Ok(self.map.len().try_into()?) } - fn HasKey(&self, key: &K::Default) -> windows_core::Result { - Ok(self.map.contains_key(key)) + fn HasKey(&self, key: K::Ref) -> windows_core::Result { + Ok(self.map.contains_key(K::deref(&key))) } fn Split(&self, first: windows_core::OutRef<'_, IMapView>, second: windows_core::OutRef<'_, IMapView>) -> windows_core::Result<()> { _ = first.write(None); diff --git a/crates/libs/windows/src/extensions/Foundation/Collections/VectorView.rs b/crates/libs/windows/src/extensions/Foundation/Collections/VectorView.rs index 6dc2077e2b..bc128b1606 100644 --- a/crates/libs/windows/src/extensions/Foundation/Collections/VectorView.rs +++ b/crates/libs/windows/src/extensions/Foundation/Collections/VectorView.rs @@ -1,4 +1,4 @@ -use crate::Foundation::Collections::{IIterable, IIterable_Impl, IIterator, IIterator_Impl, IVectorView, IVectorView_Impl}; +use crate::Foundation::Collections::*; #[windows_core::implement(IVectorView, IIterable)] struct StockVectorView @@ -33,8 +33,8 @@ where fn Size(&self) -> windows_core::Result { Ok(self.values.len().try_into()?) } - fn IndexOf(&self, value: &T::Default, result: &mut u32) -> windows_core::Result { - match self.values.iter().position(|element| element == value) { + fn IndexOf(&self, value: T::Ref, result: &mut u32) -> windows_core::Result { + match self.values.iter().position(|element| element == T::deref(&value)) { Some(index) => { *result = index as u32; Ok(true) diff --git a/crates/tests/bindgen/src/class_dep.rs b/crates/tests/bindgen/src/class_dep.rs index 7b83a75f0e..ca406e9026 100644 --- a/crates/tests/bindgen/src/class_dep.rs +++ b/crates/tests/bindgen/src/class_dep.rs @@ -464,7 +464,7 @@ where fn Size(&self) -> windows_core::Result; fn IndexOf( &self, - value: &>::Default, + value: >::Ref, index: &mut u32, ) -> windows_core::Result; fn GetMany( @@ -532,7 +532,7 @@ impl IVectorView_Vtbl { &*((this as *const *const ()).offset(OFFSET) as *const Identity); match IVectorView_Impl::IndexOf( this, - core::mem::transmute(&value), + core::mem::transmute_copy(&value), core::mem::transmute_copy(&index), ) { Ok(ok__) => { diff --git a/crates/tests/bindgen/src/delegate_generic.rs b/crates/tests/bindgen/src/delegate_generic.rs index aa31eb0e00..ce7be0f539 100644 --- a/crates/tests/bindgen/src/delegate_generic.rs +++ b/crates/tests/bindgen/src/delegate_generic.rs @@ -27,7 +27,7 @@ impl EventHandler { pub fn new< F: FnMut( windows_core::Ref, - &>::Default, + >::Ref, ) -> windows_core::Result<()> + Send + 'static, @@ -75,7 +75,7 @@ struct EventHandlerBox< T, F: FnMut( windows_core::Ref, - &>::Default, + >::Ref, ) -> windows_core::Result<()> + Send + 'static, @@ -90,7 +90,7 @@ impl< T: windows_core::RuntimeType + 'static, F: FnMut( windows_core::Ref, - &>::Default, + >::Ref, ) -> windows_core::Result<()> + Send + 'static, @@ -156,7 +156,7 @@ impl< let this = &mut *(this as *mut *mut core::ffi::c_void as *mut Self); (this.invoke)( core::mem::transmute_copy(&sender), - core::mem::transmute(&args), + core::mem::transmute_copy(&args), ) .into() } diff --git a/crates/tests/bindgen/src/interface_iterable.rs b/crates/tests/bindgen/src/interface_iterable.rs index 4cc02e0ba7..c0d89f51c9 100644 --- a/crates/tests/bindgen/src/interface_iterable.rs +++ b/crates/tests/bindgen/src/interface_iterable.rs @@ -545,21 +545,21 @@ where fn Size(&self) -> windows_core::Result; fn IndexOf( &self, - value: &>::Default, + value: >::Ref, index: &mut u32, ) -> windows_core::Result; fn SetAt( &self, index: u32, - value: &>::Default, + value: >::Ref, ) -> windows_core::Result<()>; fn InsertAt( &self, index: u32, - value: &>::Default, + value: >::Ref, ) -> windows_core::Result<()>; fn RemoveAt(&self, index: u32) -> windows_core::Result<()>; - fn Append(&self, value: &>::Default) -> windows_core::Result<()>; + fn Append(&self, value: >::Ref) -> windows_core::Result<()>; fn RemoveAtEnd(&self) -> windows_core::Result<()>; fn Clear(&self) -> windows_core::Result<()>; fn GetMany( @@ -631,7 +631,7 @@ impl IVector_Vtbl { &*((this as *const *const ()).offset(OFFSET) as *const Identity); match IVector_Impl::IndexOf( this, - core::mem::transmute(&value), + core::mem::transmute_copy(&value), core::mem::transmute_copy(&index), ) { Ok(ok__) => { @@ -654,7 +654,7 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::SetAt(this, index, core::mem::transmute(&value)).into() + IVector_Impl::SetAt(this, index, core::mem::transmute_copy(&value)).into() } } unsafe extern "system" fn InsertAt< @@ -669,7 +669,7 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::InsertAt(this, index, core::mem::transmute(&value)).into() + IVector_Impl::InsertAt(this, index, core::mem::transmute_copy(&value)).into() } } unsafe extern "system" fn RemoveAt< @@ -697,7 +697,7 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::Append(this, core::mem::transmute(&value)).into() + IVector_Impl::Append(this, core::mem::transmute_copy(&value)).into() } } unsafe extern "system" fn RemoveAtEnd< diff --git a/crates/tests/misc/implement/tests/generic_default.rs b/crates/tests/misc/implement/tests/generic_default.rs index b79c380939..4d56a1cb73 100644 --- a/crates/tests/misc/implement/tests/generic_default.rs +++ b/crates/tests/misc/implement/tests/generic_default.rs @@ -29,8 +29,12 @@ where Ok(self.0.len() as u32) } - fn IndexOf(&self, value: &T::Default, result: &mut u32) -> Result { - match self.0.iter().position(|element| element == value) { + fn IndexOf(&self, value: T::Ref, result: &mut u32) -> Result { + match self + .0 + .iter() + .position(|element| element == T::deref(&value)) + { Some(index) => { *result = index as u32; Ok(true) diff --git a/crates/tests/misc/implement/tests/generic_generic.rs b/crates/tests/misc/implement/tests/generic_generic.rs index 0b9271248d..a0aa0cd235 100644 --- a/crates/tests/misc/implement/tests/generic_generic.rs +++ b/crates/tests/misc/implement/tests/generic_generic.rs @@ -24,7 +24,7 @@ where panic!(); } - fn IndexOf(&self, _value: &T::Default, _index: &mut u32) -> Result { + fn IndexOf(&self, _value: T::Ref, _index: &mut u32) -> Result { panic!(); } diff --git a/crates/tests/misc/implement/tests/generic_primitive.rs b/crates/tests/misc/implement/tests/generic_primitive.rs index 82ba52c39a..e816060c68 100644 --- a/crates/tests/misc/implement/tests/generic_primitive.rs +++ b/crates/tests/misc/implement/tests/generic_primitive.rs @@ -19,8 +19,8 @@ impl IVectorView_Impl for Thing_Impl { Ok(123) } - fn IndexOf(&self, value: &i32, index: &mut u32) -> Result { - *index = *value as u32; + fn IndexOf(&self, value: i32, index: &mut u32) -> Result { + *index = value as u32; Ok(true) } diff --git a/crates/tests/misc/implement/tests/generic_stringable.rs b/crates/tests/misc/implement/tests/generic_stringable.rs index 5397c2feaa..430e46cece 100644 --- a/crates/tests/misc/implement/tests/generic_stringable.rs +++ b/crates/tests/misc/implement/tests/generic_stringable.rs @@ -17,7 +17,7 @@ impl IVectorView_Impl for Thing_Impl { panic!(); } - fn IndexOf(&self, _value: &Option, _index: &mut u32) -> Result { + fn IndexOf(&self, _value: Ref, _index: &mut u32) -> Result { panic!(); } diff --git a/crates/tests/misc/implement/tests/map.rs b/crates/tests/misc/implement/tests/map.rs index dfbac2971a..52229bde4e 100644 --- a/crates/tests/misc/implement/tests/map.rs +++ b/crates/tests/misc/implement/tests/map.rs @@ -46,10 +46,10 @@ struct MapView(); #[allow(non_snake_case)] impl IMapView_Impl for MapView_Impl { // TODO: shouldn't require & for primtiive - fn HasKey(&self, _key: &i32) -> Result { + fn HasKey(&self, _key: i32) -> Result { Ok(true) } - fn Lookup(&self, _key: &i32) -> Result { + fn Lookup(&self, _key: i32) -> Result { Ok(0.0) } fn Split( @@ -85,16 +85,16 @@ impl IMap_Impl for Map_Impl { fn GetView(&self) -> Result> { Ok(MapView().into()) } - fn HasKey(&self, _key: &i32) -> Result { + fn HasKey(&self, _key: i32) -> Result { Ok(true) } - fn Insert(&self, _key: &i32, _value: &f32) -> Result { + fn Insert(&self, _key: i32, _value: f32) -> Result { Ok(true) } - fn Lookup(&self, _key: &i32) -> Result { + fn Lookup(&self, _key: i32) -> Result { Ok(0.0) } - fn Remove(&self, _key: &i32) -> Result<()> { + fn Remove(&self, _key: i32) -> Result<()> { Ok(()) } fn Size(&self) -> Result { diff --git a/crates/tests/misc/implement/tests/vector.rs b/crates/tests/misc/implement/tests/vector.rs index c2ae2d58ee..baa581582c 100644 --- a/crates/tests/misc/implement/tests/vector.rs +++ b/crates/tests/misc/implement/tests/vector.rs @@ -48,9 +48,12 @@ where let reader = self.0.read().unwrap(); Ok(reader.len() as u32) } - fn IndexOf(&self, value: &T::Default, result: &mut u32) -> Result { + fn IndexOf(&self, value: T::Ref, result: &mut u32) -> Result { let reader = self.0.read().unwrap(); - match reader.iter().position(|element| element == value) { + match reader + .iter() + .position(|element| element == T::deref(&value)) + { Some(index) => { *result = index as u32; Ok(true) @@ -77,16 +80,16 @@ where fn GetView(&self) -> Result> { unsafe { self.cast() } } - fn IndexOf(&self, value: &T::Default, result: &mut u32) -> Result { + fn IndexOf(&self, value: T::Ref, result: &mut u32) -> Result { self.IndexOf(value, result) } - fn SetAt(&self, index: u32, value: &T::Default) -> Result<()> { + fn SetAt(&self, index: u32, value: T::Ref) -> Result<()> { let mut writer = self.0.write().unwrap(); let item = writer.get_mut(index as usize).ok_or_else(err_bounds)?; - *item = value.clone(); + *item = T::deref(&value).clone(); Ok(()) } - fn InsertAt(&self, index: u32, value: &T::Default) -> Result<()> { + fn InsertAt(&self, index: u32, value: T::Ref) -> Result<()> { let mut writer = self.0.write().unwrap(); let index = index as usize; if index > writer.len() { @@ -94,7 +97,7 @@ where } else { let len = writer.len(); writer.try_reserve(len + 1).map_err(|_| err_memory())?; - writer.insert(index, value.clone()); + writer.insert(index, T::deref(&value).clone()); Ok(()) } } @@ -108,11 +111,11 @@ where Err(err_bounds()) } } - fn Append(&self, value: &T::Default) -> Result<()> { + fn Append(&self, value: T::Ref) -> Result<()> { let mut writer = self.0.write().unwrap(); let len = writer.len(); writer.try_reserve(len + 1).map_err(|_| err_memory())?; - writer.insert(len, value.clone()); + writer.insert(len, T::deref(&value).clone()); Ok(()) } fn RemoveAtEnd(&self) -> Result<()> { @@ -154,7 +157,7 @@ where fn Size(&self) -> Result { self.Size() } - fn IndexOf(&self, value: &T::Default, result: &mut u32) -> Result { + fn IndexOf(&self, value: T::Ref, result: &mut u32) -> Result { self.IndexOf(value, result) } fn GetMany(&self, startindex: u32, items: &mut [T::Default]) -> Result { diff --git a/crates/tests/winrt/event_core/tests/tests.rs b/crates/tests/winrt/event_core/tests/tests.rs index 06b964766c..d042a23985 100644 --- a/crates/tests/winrt/event_core/tests/tests.rs +++ b/crates/tests/winrt/event_core/tests/tests.rs @@ -14,7 +14,7 @@ fn add_remove() -> Result<()> { // Add event handler. event.add(&EventHandler::::new(move |_, args| { - check_sender.store(*args, Ordering::Relaxed); + check_sender.store(args, Ordering::Relaxed); Ok(()) }))?; @@ -57,7 +57,7 @@ fn multiple() -> Result<()> { assert_eq!(c_check.load(Ordering::Relaxed), 0); let a_token = event.add(&EventHandler::::new(move |_, args| { - a_check_sender.store(*args, Ordering::Relaxed); + a_check_sender.store(args, Ordering::Relaxed); Ok(()) }))?; @@ -70,7 +70,7 @@ fn multiple() -> Result<()> { assert_eq!(c_check.load(Ordering::Relaxed), 0); let b_token = event.add(&EventHandler::::new(move |_, args| { - b_check_sender.store(*args, Ordering::Relaxed); + b_check_sender.store(args, Ordering::Relaxed); Ok(()) }))?; @@ -83,7 +83,7 @@ fn multiple() -> Result<()> { assert_eq!(c_check.load(Ordering::Relaxed), 0); let c_token = event.add(&EventHandler::::new(move |_, args| { - c_check_sender.store(*args, Ordering::Relaxed); + c_check_sender.store(args, Ordering::Relaxed); Ok(()) }))?; diff --git a/crates/tests/winrt/events_client/src/lib.rs b/crates/tests/winrt/events_client/src/lib.rs index c4b13e67dc..b2b2676d56 100644 --- a/crates/tests/winrt/events_client/src/lib.rs +++ b/crates/tests/winrt/events_client/src/lib.rs @@ -14,9 +14,9 @@ fn test() -> Result<()> { assert_eq!(0, class.Signal(1)?); let token = class.Event(&TypedEventHandler::new( - move |sender: &Option, args: &i32| { + move |sender: Ref, args: i32| { assert_eq!(sender.as_ref().unwrap(), class); - assert_eq!(*args, 2); + assert_eq!(args, 2); Ok(()) }, ))?; @@ -27,17 +27,17 @@ fn test() -> Result<()> { class.Event(&TypedEventHandler::new( // TODO: ideally generics also use Ref here - move |sender: &Option, args: &i32| { + move |sender: Ref, args: i32| { assert_eq!(sender.as_ref().unwrap(), class); - assert_eq!(*args, 4); + assert_eq!(args, 4); Ok(()) }, ))?; class.Event(&TypedEventHandler::new( - move |sender: &Option, args: &i32| { + move |sender: Ref, args: i32| { assert_eq!(sender.as_ref().unwrap(), class); - assert_eq!(*args, 4); + assert_eq!(args, 4); Ok(()) }, ))?; @@ -51,7 +51,7 @@ fn test_static() -> Result<()> { assert_eq!(0, Class::StaticSignal(1)?); let token = Class::StaticEvent(&EventHandler::new(move |_, args| { - assert_eq!(*args, 2); + assert_eq!(args, 2); Ok(()) }))?; @@ -60,12 +60,12 @@ fn test_static() -> Result<()> { assert_eq!(0, Class::StaticSignal(3)?); Class::StaticEvent(&EventHandler::new(move |_, args| { - assert_eq!(*args, 4); + assert_eq!(args, 4); Ok(()) }))?; Class::StaticEvent(&EventHandler::new(move |_, args| { - assert_eq!(*args, 4); + assert_eq!(args, 4); Ok(()) }))?; diff --git a/crates/tests/winrt/old/tests/delegates.rs b/crates/tests/winrt/old/tests/delegates.rs index d47a7a20f4..1af9b2ea15 100644 --- a/crates/tests/winrt/old/tests/delegates.rs +++ b/crates/tests/winrt/old/tests/delegates.rs @@ -47,7 +47,7 @@ fn generic() -> windows::core::Result<()> { tx.send(true).unwrap(); // TODO: ideally primitives would be passed by value - assert!(*port == 80); + assert!(port == 80); Ok(()) });