diff --git a/crates/libs/bindgen/src/types/method.rs b/crates/libs/bindgen/src/types/method.rs index 0924014bf6a..2fd621524b9 100644 --- a/crates/libs/bindgen/src/types/method.rs +++ b/crates/libs/bindgen/src/types/method.rs @@ -45,8 +45,11 @@ impl Method { quote! { core::slice::from_raw_parts(core::mem::transmute_copy(&#name), #abi_size_name as usize) } } else if param.is_primitive(reader) { quote! { #name } - } else if param.is_const_ref() || param.is_interface() || matches!(¶m.ty, Type::Generic(_)) { + } else if param.is_const_ref() || param.is_interface() { quote! { core::mem::transmute_copy(&#name) } + } else if let Type::Generic(g) = ¶m.ty { + let type_name = to_ident(g.name()); + quote! { <#type_name as windows_core::Type<#type_name>>::abi_to_param(&#name) } } else { quote! { core::mem::transmute(&#name) } } @@ -150,9 +153,12 @@ impl Method { quote! { &[#default_type] } } else if p.is_primitive(config.reader) { quote! { #default_type } - } else if p.is_interface() || matches!(&p.ty, Type::Generic(_)) { + } else if p.is_interface() { let type_name = p.write_name(config); quote! { windows_core::Ref<#type_name> } + } else if matches!(&p.ty, Type::Generic(_)) { + let type_name = p.write_name(config); + quote! { windows_core::ImplParam<'_, #type_name> } } else { quote! { &#default_type } } diff --git a/crates/libs/collections/src/bindings.rs b/crates/libs/collections/src/bindings.rs index 8aeeb777a31..4ccbdf0bc42 100644 --- a/crates/libs/collections/src/bindings.rs +++ b/crates/libs/collections/src/bindings.rs @@ -662,16 +662,16 @@ where K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'static, { - fn Lookup(&self, key: windows_core::Ref) -> windows_core::Result; + fn Lookup(&self, key: windows_core::ImplParam<'_, K>) -> windows_core::Result; fn Size(&self) -> windows_core::Result; - fn HasKey(&self, key: windows_core::Ref) -> windows_core::Result; + fn HasKey(&self, key: windows_core::ImplParam<'_, K>) -> windows_core::Result; fn GetView(&self) -> windows_core::Result>; fn Insert( &self, - key: windows_core::Ref, - value: windows_core::Ref, + key: windows_core::ImplParam<'_, K>, + value: windows_core::ImplParam<'_, V>, ) -> windows_core::Result; - fn Remove(&self, key: windows_core::Ref) -> windows_core::Result<()>; + fn Remove(&self, key: windows_core::ImplParam<'_, K>) -> windows_core::Result<()>; fn Clear(&self) -> windows_core::Result<()>; } impl @@ -691,7 +691,7 @@ impl>::abi_to_param(&key)) { Ok(ok__) => { result__.write(core::mem::transmute_copy(&ok__)); core::mem::forget(ok__); @@ -735,7 +735,7 @@ impl>::abi_to_param(&key)) { Ok(ok__) => { result__.write(core::mem::transmute_copy(&ok__)); windows_core::HRESULT(0) @@ -782,8 +782,8 @@ impl>::abi_to_param(&key), + >::abi_to_param(&value), ) { Ok(ok__) => { result__.write(core::mem::transmute_copy(&ok__)); @@ -805,7 +805,7 @@ impl>::abi_to_param(&key)).into() } } unsafe extern "system" fn Clear< @@ -1016,9 +1016,9 @@ where K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'static, { - fn Lookup(&self, key: windows_core::Ref) -> windows_core::Result; + fn Lookup(&self, key: windows_core::ImplParam<'_, K>) -> windows_core::Result; fn Size(&self) -> windows_core::Result; - fn HasKey(&self, key: windows_core::Ref) -> windows_core::Result; + fn HasKey(&self, key: windows_core::ImplParam<'_, K>) -> windows_core::Result; fn Split( &self, first: windows_core::OutRef>, @@ -1042,7 +1042,8 @@ impl>::abi_to_param(&key)) + { Ok(ok__) => { result__.write(core::mem::transmute_copy(&ok__)); core::mem::forget(ok__); @@ -1086,7 +1087,8 @@ impl>::abi_to_param(&key)) + { Ok(ok__) => { result__.write(core::mem::transmute_copy(&ok__)); windows_core::HRESULT(0) @@ -1373,11 +1375,19 @@ 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: windows_core::Ref, index: &mut u32) -> windows_core::Result; - fn SetAt(&self, index: u32, value: windows_core::Ref) -> windows_core::Result<()>; - fn InsertAt(&self, index: u32, value: windows_core::Ref) -> windows_core::Result<()>; + fn IndexOf( + &self, + value: windows_core::ImplParam<'_, T>, + index: &mut u32, + ) -> windows_core::Result; + fn SetAt(&self, index: u32, value: windows_core::ImplParam<'_, T>) -> windows_core::Result<()>; + fn InsertAt( + &self, + index: u32, + value: windows_core::ImplParam<'_, T>, + ) -> windows_core::Result<()>; fn RemoveAt(&self, index: u32) -> windows_core::Result<()>; - fn Append(&self, value: windows_core::Ref) -> windows_core::Result<()>; + fn Append(&self, value: windows_core::ImplParam<'_, T>) -> windows_core::Result<()>; fn RemoveAtEnd(&self) -> windows_core::Result<()>; fn Clear(&self) -> windows_core::Result<()>; fn GetMany( @@ -1470,7 +1480,7 @@ impl IVector_Vtbl { &*((this as *const *const ()).offset(OFFSET) as *const Identity); match IVector_Impl::IndexOf( this, - core::mem::transmute_copy(&value), + >::abi_to_param(&value), core::mem::transmute_copy(&index), ) { Ok(ok__) => { @@ -1493,7 +1503,12 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::SetAt(this, index, core::mem::transmute_copy(&value)).into() + IVector_Impl::SetAt( + this, + index, + >::abi_to_param(&value), + ) + .into() } } unsafe extern "system" fn InsertAt< @@ -1508,7 +1523,12 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::InsertAt(this, index, core::mem::transmute_copy(&value)).into() + IVector_Impl::InsertAt( + this, + index, + >::abi_to_param(&value), + ) + .into() } } unsafe extern "system" fn RemoveAt< @@ -1536,7 +1556,8 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::Append(this, core::mem::transmute_copy(&value)).into() + IVector_Impl::Append(this, >::abi_to_param(&value)) + .into() } } unsafe extern "system" fn RemoveAtEnd< @@ -1812,7 +1833,11 @@ where { fn GetAt(&self, index: u32) -> windows_core::Result; fn Size(&self) -> windows_core::Result; - fn IndexOf(&self, value: windows_core::Ref, index: &mut u32) -> windows_core::Result; + fn IndexOf( + &self, + value: windows_core::ImplParam<'_, T>, + index: &mut u32, + ) -> windows_core::Result; fn GetMany( &self, startIndex: u32, @@ -1878,7 +1903,7 @@ impl IVectorView_Vtbl { &*((this as *const *const ()).offset(OFFSET) as *const Identity); match IVectorView_Impl::IndexOf( this, - core::mem::transmute_copy(&value), + >::abi_to_param(&value), core::mem::transmute_copy(&index), ) { Ok(ok__) => { diff --git a/crates/libs/collections/src/map_view.rs b/crates/libs/collections/src/map_view.rs index abc85c83da7..a7b69b7b221 100644 --- a/crates/libs/collections/src/map_view.rs +++ b/crates/libs/collections/src/map_view.rs @@ -35,8 +35,11 @@ where K::Default: Clone + Ord, V::Default: Clone, { - fn Lookup(&self, key: Ref) -> Result { - let value = self.map.get(&*key).ok_or_else(|| Error::from(E_BOUNDS))?; + fn Lookup(&self, key: ImplParam<'_, K>) -> Result { + let value = self + .map + .get(K::param_as_default(&key)) + .ok_or_else(|| Error::from(E_BOUNDS))?; V::from_default(value) } @@ -45,8 +48,8 @@ where Ok(self.map.len().try_into()?) } - fn HasKey(&self, key: Ref) -> Result { - Ok(self.map.contains_key(&*key)) + fn HasKey(&self, key: ImplParam<'_, K>) -> Result { + Ok(self.map.contains_key(K::param_as_default(&key))) } fn Split(&self, first: OutRef>, second: OutRef>) -> Result<()> { diff --git a/crates/libs/collections/src/vector_view.rs b/crates/libs/collections/src/vector_view.rs index 7978bf9dd58..a3cf48c145c 100644 --- a/crates/libs/collections/src/vector_view.rs +++ b/crates/libs/collections/src/vector_view.rs @@ -42,8 +42,12 @@ where Ok(self.values.len().try_into()?) } - fn IndexOf(&self, value: Ref, result: &mut u32) -> Result { - match self.values.iter().position(|element| element == &*value) { + fn IndexOf(&self, value: ImplParam<'_, T>, result: &mut u32) -> Result { + match self + .values + .iter() + .position(|element| element == T::param_as_default(&value)) + { Some(index) => { *result = index as u32; Ok(true) diff --git a/crates/libs/core/src/type.rs b/crates/libs/core/src/type.rs index a64f191d0a2..28ca3f46295 100644 --- a/crates/libs/core/src/type.rs +++ b/crates/libs/core/src/type.rs @@ -18,11 +18,20 @@ pub struct CopyType; pub trait Type::TypeKind>: TypeKind + Sized + Clone { type Abi; type Default; + type Param<'a>: 'a + where + Self: 'a; 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; + unsafe fn abi_to_param<'a>(abi: &'a Self::Abi) -> Self::Param<'a> + where + Self: 'a; + fn param_as_default<'a, 'b>(param: &'a Self::Param<'b>) -> &'a Self::Default + where + Self: 'b; } impl Type for T @@ -31,6 +40,10 @@ where { type Abi = *mut core::ffi::c_void; type Default = Option; + type Param<'a> + = Ref<'a, Self> + where + Self: 'a; fn is_null(abi: &Self::Abi) -> bool { abi.is_null() @@ -53,6 +66,20 @@ where fn from_default(default: &Self::Default) -> Result { default.as_ref().cloned().ok_or(Error::empty()) } + + unsafe fn abi_to_param<'a>(abi: &'a Self::Abi) -> Ref<'a, Self> + where + Self: 'a, + { + unsafe { core::mem::transmute_copy(abi) } + } + + fn param_as_default<'a, 'b>(param: &'a Ref<'b, Self>) -> &'a Option + where + Self: 'b, + { + param + } } impl Type for T @@ -61,6 +88,10 @@ where { type Abi = core::mem::MaybeUninit; type Default = Self; + type Param<'a> + = Ref<'a, Self> + where + Self: 'a; fn is_null(_: &Self::Abi) -> bool { false @@ -77,6 +108,20 @@ where fn from_default(default: &Self::Default) -> Result { Ok(default.clone()) } + + unsafe fn abi_to_param<'a>(abi: &'a Self::Abi) -> Ref<'a, Self> + where + Self: 'a, + { + unsafe { core::mem::transmute_copy(abi) } + } + + fn param_as_default<'a, 'b>(param: &'a Ref<'b, Self>) -> &'a Self + where + Self: 'b, + { + param + } } impl Type for T @@ -85,6 +130,10 @@ where { type Abi = Self; type Default = Self; + type Param<'a> + = Self + where + Self: 'a; fn is_null(_: &Self::Abi) -> bool { false @@ -101,6 +150,20 @@ where fn from_default(default: &Self) -> Result { Ok(default.clone()) } + + unsafe fn abi_to_param<'a>(abi: &'a Self) -> Self + where + Self: 'a, + { + abi.clone() + } + + fn param_as_default<'a, 'b>(param: &'a Self) -> &'a Self + where + Self: 'b, + { + param + } } impl TypeKind for T { @@ -125,3 +188,9 @@ primitives!(bool, i8, u8, i16, u16, i32, u32, i64, u64, f32, f64, usize, isize); #[doc(hidden)] pub type AbiType = >::Abi; + +/// The parameter type used in `_Impl` trait methods for a given WinRT type. +/// +/// For copy types (primitives), this is the type itself. For other types, this is +/// [`Ref`], which allows borrowing across the ABI boundary without taking ownership. +pub type ImplParam<'a, T> = >::Param<'a>; diff --git a/crates/libs/future/src/bindings.rs b/crates/libs/future/src/bindings.rs index ca60743cdc5..bfc6b49c665 100644 --- a/crates/libs/future/src/bindings.rs +++ b/crates/libs/future/src/bindings.rs @@ -156,7 +156,7 @@ impl AsyncActionProgressHandler< pub fn new< F: Fn( windows_core::Ref>, - windows_core::Ref, + windows_core::ImplParam<'_, TProgress>, ) -> windows_core::Result<()> + Send + 'static, @@ -205,7 +205,7 @@ struct AsyncActionProgressHandlerBox< TProgress, F: Fn( windows_core::Ref>, - windows_core::Ref, + windows_core::ImplParam<'_, TProgress>, ) -> windows_core::Result<()> + Send + 'static, @@ -220,7 +220,7 @@ impl< TProgress: windows_core::RuntimeType + 'static, F: Fn( windows_core::Ref>, - windows_core::Ref, + windows_core::ImplParam<'_, TProgress>, ) -> windows_core::Result<()> + Send + 'static, @@ -294,7 +294,7 @@ impl< let this = &mut *(this as *mut *mut core::ffi::c_void as *mut Self); (this.invoke)( core::mem::transmute_copy(&asyncinfo), - core::mem::transmute_copy(&progressinfo), + >::abi_to_param(&progressinfo), ) .into() } @@ -653,7 +653,7 @@ impl< pub fn new< F: Fn( windows_core::Ref>, - windows_core::Ref, + windows_core::ImplParam<'_, TProgress>, ) -> windows_core::Result<()> + Send + 'static, @@ -705,7 +705,7 @@ struct AsyncOperationProgressHandlerBox< TProgress, F: Fn( windows_core::Ref>, - windows_core::Ref, + windows_core::ImplParam<'_, TProgress>, ) -> windows_core::Result<()> + Send + 'static, @@ -722,7 +722,7 @@ impl< TProgress: windows_core::RuntimeType + 'static, F: Fn( windows_core::Ref>, - windows_core::Ref, + windows_core::ImplParam<'_, TProgress>, ) -> windows_core::Result<()> + Send + 'static, @@ -783,7 +783,7 @@ impl< let this = &mut *(this as *mut *mut core::ffi::c_void as *mut Self); (this.invoke)( core::mem::transmute_copy(&asyncinfo), - core::mem::transmute_copy(&progressinfo), + >::abi_to_param(&progressinfo), ) .into() } diff --git a/crates/libs/windows/src/Windows/Foundation/mod.rs b/crates/libs/windows/src/Windows/Foundation/mod.rs index 06cb2328fbf..1ce11799974 100644 --- a/crates/libs/windows/src/Windows/Foundation/mod.rs +++ b/crates/libs/windows/src/Windows/Foundation/mod.rs @@ -143,7 +143,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, windows_core::Ref) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self { + pub fn new, windows_core::ImplParam<'_, T>) -> 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)) } } @@ -167,7 +167,7 @@ where T: core::marker::PhantomData, } #[repr(C)] -struct EventHandlerBox, windows_core::Ref) -> windows_core::Result<()> + Send + 'static> +struct EventHandlerBox, windows_core::ImplParam<'_, T>) -> windows_core::Result<()> + Send + 'static> where T: windows_core::RuntimeType + 'static, { @@ -175,7 +175,7 @@ where invoke: F, count: windows_core::imp::RefCount, } -impl, windows_core::Ref) -> windows_core::Result<()> + Send + 'static> EventHandlerBox { +impl, windows_core::ImplParam<'_, T>) -> 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, @@ -222,7 +222,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_copy(&args)).into() + (this.invoke)(core::mem::transmute_copy(&sender), >::abi_to_param(&args)).into() } } } @@ -2464,7 +2464,7 @@ impl TypedEventHandler { - pub fn new, windows_core::Ref) -> windows_core::Result<()> + Send + 'static>(invoke: F) -> Self { + pub fn new, windows_core::ImplParam<'_, TResult>) -> 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)) } } @@ -2490,7 +2490,7 @@ where TResult: core::marker::PhantomData, } #[repr(C)] -struct TypedEventHandlerBox, windows_core::Ref) -> windows_core::Result<()> + Send + 'static> +struct TypedEventHandlerBox, windows_core::ImplParam<'_, TResult>) -> windows_core::Result<()> + Send + 'static> where TSender: windows_core::RuntimeType + 'static, TResult: windows_core::RuntimeType + 'static, @@ -2499,7 +2499,7 @@ where invoke: F, count: windows_core::imp::RefCount, } -impl, windows_core::Ref) -> windows_core::Result<()> + Send + 'static> TypedEventHandlerBox { +impl, windows_core::ImplParam<'_, TResult>) -> 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, @@ -2547,7 +2547,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_copy(&sender), core::mem::transmute_copy(&args)).into() + (this.invoke)(>::abi_to_param(&sender), >::abi_to_param(&args)).into() } } } diff --git a/crates/tests/libs/bindgen/src/class_dep.rs b/crates/tests/libs/bindgen/src/class_dep.rs index 35b661a5e96..9168d5af768 100644 --- a/crates/tests/libs/bindgen/src/class_dep.rs +++ b/crates/tests/libs/bindgen/src/class_dep.rs @@ -468,7 +468,11 @@ where { fn GetAt(&self, index: u32) -> windows_core::Result; fn Size(&self) -> windows_core::Result; - fn IndexOf(&self, value: windows_core::Ref, index: &mut u32) -> windows_core::Result; + fn IndexOf( + &self, + value: windows_core::ImplParam<'_, T>, + index: &mut u32, + ) -> windows_core::Result; fn GetMany( &self, startIndex: u32, @@ -534,7 +538,7 @@ impl IVectorView_Vtbl { &*((this as *const *const ()).offset(OFFSET) as *const Identity); match IVectorView_Impl::IndexOf( this, - core::mem::transmute_copy(&value), + >::abi_to_param(&value), core::mem::transmute_copy(&index), ) { Ok(ok__) => { diff --git a/crates/tests/libs/bindgen/src/delegate_generic.rs b/crates/tests/libs/bindgen/src/delegate_generic.rs index eb5250db2ea..f534076764d 100644 --- a/crates/tests/libs/bindgen/src/delegate_generic.rs +++ b/crates/tests/libs/bindgen/src/delegate_generic.rs @@ -27,7 +27,7 @@ impl EventHandler { pub fn new< F: Fn( windows_core::Ref, - windows_core::Ref, + windows_core::ImplParam<'_, T>, ) -> windows_core::Result<()> + Send + 'static, @@ -76,7 +76,7 @@ struct EventHandlerBox< T, F: Fn( windows_core::Ref, - windows_core::Ref, + windows_core::ImplParam<'_, T>, ) -> windows_core::Result<()> + Send + 'static, @@ -91,7 +91,7 @@ impl< T: windows_core::RuntimeType + 'static, F: Fn( windows_core::Ref, - windows_core::Ref, + windows_core::ImplParam<'_, T>, ) -> windows_core::Result<()> + Send + 'static, @@ -163,7 +163,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_copy(&args), + >::abi_to_param(&args), ) .into() } diff --git a/crates/tests/libs/bindgen/src/interface_iterable.rs b/crates/tests/libs/bindgen/src/interface_iterable.rs index 2213067209c..0f6d8537b60 100644 --- a/crates/tests/libs/bindgen/src/interface_iterable.rs +++ b/crates/tests/libs/bindgen/src/interface_iterable.rs @@ -549,11 +549,19 @@ where { fn GetAt(&self, index: u32) -> windows_core::Result; fn Size(&self) -> windows_core::Result; - fn IndexOf(&self, value: windows_core::Ref, index: &mut u32) -> windows_core::Result; - fn SetAt(&self, index: u32, value: windows_core::Ref) -> windows_core::Result<()>; - fn InsertAt(&self, index: u32, value: windows_core::Ref) -> windows_core::Result<()>; + fn IndexOf( + &self, + value: windows_core::ImplParam<'_, T>, + index: &mut u32, + ) -> windows_core::Result; + fn SetAt(&self, index: u32, value: windows_core::ImplParam<'_, T>) -> windows_core::Result<()>; + fn InsertAt( + &self, + index: u32, + value: windows_core::ImplParam<'_, T>, + ) -> windows_core::Result<()>; fn RemoveAt(&self, index: u32) -> windows_core::Result<()>; - fn Append(&self, value: windows_core::Ref) -> windows_core::Result<()>; + fn Append(&self, value: windows_core::ImplParam<'_, T>) -> windows_core::Result<()>; fn RemoveAtEnd(&self) -> windows_core::Result<()>; fn Clear(&self) -> windows_core::Result<()>; fn GetMany( @@ -625,7 +633,7 @@ impl IVector_Vtbl { &*((this as *const *const ()).offset(OFFSET) as *const Identity); match IVector_Impl::IndexOf( this, - core::mem::transmute_copy(&value), + >::abi_to_param(&value), core::mem::transmute_copy(&index), ) { Ok(ok__) => { @@ -648,7 +656,12 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::SetAt(this, index, core::mem::transmute_copy(&value)).into() + IVector_Impl::SetAt( + this, + index, + >::abi_to_param(&value), + ) + .into() } } unsafe extern "system" fn InsertAt< @@ -663,7 +676,12 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::InsertAt(this, index, core::mem::transmute_copy(&value)).into() + IVector_Impl::InsertAt( + this, + index, + >::abi_to_param(&value), + ) + .into() } } unsafe extern "system" fn RemoveAt< @@ -691,7 +709,8 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::Append(this, core::mem::transmute_copy(&value)).into() + IVector_Impl::Append(this, >::abi_to_param(&value)) + .into() } } unsafe extern "system" fn RemoveAtEnd< diff --git a/crates/tests/libs/implement/tests/generic_default.rs b/crates/tests/libs/implement/tests/generic_default.rs index c72cd192328..d19421e1253 100644 --- a/crates/tests/libs/implement/tests/generic_default.rs +++ b/crates/tests/libs/implement/tests/generic_default.rs @@ -27,8 +27,12 @@ where Ok(self.0.len() as u32) } - fn IndexOf(&self, value: Ref, result: &mut u32) -> Result { - match self.0.iter().position(|element| element == &*value) { + fn IndexOf(&self, value: ImplParam<'_, T>, result: &mut u32) -> Result { + match self + .0 + .iter() + .position(|element| element == T::param_as_default(&value)) + { Some(index) => { *result = index as u32; Ok(true) diff --git a/crates/tests/libs/implement/tests/generic_generic.rs b/crates/tests/libs/implement/tests/generic_generic.rs index 44f6b37f886..008a468a0da 100644 --- a/crates/tests/libs/implement/tests/generic_generic.rs +++ b/crates/tests/libs/implement/tests/generic_generic.rs @@ -23,7 +23,7 @@ where panic!(); } - fn IndexOf(&self, _value: Ref, _index: &mut u32) -> Result { + fn IndexOf(&self, _value: ImplParam<'_, T>, _index: &mut u32) -> Result { panic!(); } diff --git a/crates/tests/libs/implement/tests/generic_primitive.rs b/crates/tests/libs/implement/tests/generic_primitive.rs index 877139aa09c..46551c22cc7 100644 --- a/crates/tests/libs/implement/tests/generic_primitive.rs +++ b/crates/tests/libs/implement/tests/generic_primitive.rs @@ -16,8 +16,8 @@ impl IVectorView_Impl for Thing_Impl { Ok(123) } - fn IndexOf(&self, value: Ref, 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/libs/implement/tests/map.rs b/crates/tests/libs/implement/tests/map.rs index e63859268e6..690306d1ad2 100644 --- a/crates/tests/libs/implement/tests/map.rs +++ b/crates/tests/libs/implement/tests/map.rs @@ -42,11 +42,10 @@ impl IIterator_Impl> for Iterator_Impl { struct MapView(); impl IMapView_Impl for MapView_Impl { - // TODO: shouldn't require `Ref` for primitive - fn HasKey(&self, _key: Ref) -> Result { + fn HasKey(&self, _key: i32) -> Result { Ok(true) } - fn Lookup(&self, _key: Ref) -> Result { + fn Lookup(&self, _key: i32) -> Result { Ok(0.0) } fn Split( @@ -80,16 +79,16 @@ impl IMap_Impl for Map_Impl { fn GetView(&self) -> Result> { Ok(MapView().into()) } - fn HasKey(&self, _key: Ref) -> Result { + fn HasKey(&self, _key: i32) -> Result { Ok(true) } - fn Insert(&self, _key: Ref, _value: Ref) -> Result { + fn Insert(&self, _key: i32, _value: f32) -> Result { Ok(true) } - fn Lookup(&self, _key: Ref) -> Result { + fn Lookup(&self, _key: i32) -> Result { Ok(0.0) } - fn Remove(&self, _key: Ref) -> Result<()> { + fn Remove(&self, _key: i32) -> Result<()> { Ok(()) } fn Size(&self) -> Result { diff --git a/crates/tests/libs/implement/tests/vector.rs b/crates/tests/libs/implement/tests/vector.rs index a907136f7a3..cfd1f42049e 100644 --- a/crates/tests/libs/implement/tests/vector.rs +++ b/crates/tests/libs/implement/tests/vector.rs @@ -48,9 +48,12 @@ where let reader = self.0.read().unwrap(); Ok(reader.len() as u32) } - fn IndexOf(&self, value: Ref, result: &mut u32) -> Result { + fn IndexOf(&self, value: ImplParam<'_, T>, result: &mut u32) -> Result { let reader = self.0.read().unwrap(); - match reader.iter().position(|element| element == &*value) { + match reader + .iter() + .position(|element| element == T::param_as_default(&value)) + { Some(index) => { *result = index as u32; Ok(true) @@ -77,16 +80,16 @@ where fn GetView(&self) -> Result> { Ok(self.to_interface()) } - fn IndexOf(&self, value: Ref, result: &mut u32) -> Result { + fn IndexOf(&self, value: ImplParam<'_, T>, result: &mut u32) -> Result { self.IndexOf(value, result) } - fn SetAt(&self, index: u32, value: Ref) -> Result<()> { + fn SetAt(&self, index: u32, value: ImplParam<'_, T>) -> 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::param_as_default(&value).clone(); Ok(()) } - fn InsertAt(&self, index: u32, value: Ref) -> Result<()> { + fn InsertAt(&self, index: u32, value: ImplParam<'_, T>) -> 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::param_as_default(&value).clone()); Ok(()) } } @@ -108,11 +111,11 @@ where Err(err_bounds()) } } - fn Append(&self, value: Ref) -> Result<()> { + fn Append(&self, value: ImplParam<'_, T>) -> 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::param_as_default(&value).clone()); Ok(()) } fn RemoveAtEnd(&self) -> Result<()> { @@ -154,7 +157,7 @@ where fn Size(&self) -> Result { self.Size() } - fn IndexOf(&self, value: Ref, result: &mut u32) -> Result { + fn IndexOf(&self, value: ImplParam<'_, T>, 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 06b964766c4..d042a239850 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 3852bf201d8..d2cf924a48e 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: Ref, args: Ref| { + move |sender: Ref, args: i32| { assert_eq!(sender.as_ref().unwrap(), class); - assert_eq!(*args, 2); + assert_eq!(args, 2); Ok(()) }, ))?; @@ -26,17 +26,17 @@ fn test() -> Result<()> { assert_eq!(0, class.Signal(3)?); class.Event(&TypedEventHandler::new( - move |sender: Ref, args: Ref| { + 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: Ref, args: Ref| { + move |sender: Ref, args: i32| { assert_eq!(sender.as_ref().unwrap(), class); - assert_eq!(*args, 4); + assert_eq!(args, 4); Ok(()) }, ))?; @@ -50,7 +50,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(()) }))?; @@ -59,12 +59,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 ae4d5cf2fc0..c0f78e0d65e 100644 --- a/crates/tests/winrt/old/tests/delegates.rs +++ b/crates/tests/winrt/old/tests/delegates.rs @@ -46,8 +46,7 @@ fn generic() -> windows::core::Result<()> { let d = Handler::new(move |_, port| { tx.send(true).unwrap(); - // TODO: ideally primitives would be passed by value - assert!(*port == 80); + assert!(port == 80); Ok(()) }); diff --git a/crates/tests/winrt/reference_custom/src/bindings.rs b/crates/tests/winrt/reference_custom/src/bindings.rs index 8a8b8eabaf7..d8fe63cd627 100644 --- a/crates/tests/winrt/reference_custom/src/bindings.rs +++ b/crates/tests/winrt/reference_custom/src/bindings.rs @@ -935,11 +935,19 @@ where { fn GetAt(&self, index: u32) -> windows_core::Result; fn Size(&self) -> windows_core::Result; - fn IndexOf(&self, value: windows_core::Ref, index: &mut u32) -> windows_core::Result; - fn SetAt(&self, index: u32, value: windows_core::Ref) -> windows_core::Result<()>; - fn InsertAt(&self, index: u32, value: windows_core::Ref) -> windows_core::Result<()>; + fn IndexOf( + &self, + value: windows_core::ImplParam<'_, T>, + index: &mut u32, + ) -> windows_core::Result; + fn SetAt(&self, index: u32, value: windows_core::ImplParam<'_, T>) -> windows_core::Result<()>; + fn InsertAt( + &self, + index: u32, + value: windows_core::ImplParam<'_, T>, + ) -> windows_core::Result<()>; fn RemoveAt(&self, index: u32) -> windows_core::Result<()>; - fn Append(&self, value: windows_core::Ref) -> windows_core::Result<()>; + fn Append(&self, value: windows_core::ImplParam<'_, T>) -> windows_core::Result<()>; fn RemoveAtEnd(&self) -> windows_core::Result<()>; fn Clear(&self) -> windows_core::Result<()>; fn GetMany( @@ -1011,7 +1019,7 @@ impl IVector_Vtbl { &*((this as *const *const ()).offset(OFFSET) as *const Identity); match IVector_Impl::IndexOf( this, - core::mem::transmute_copy(&value), + >::abi_to_param(&value), core::mem::transmute_copy(&index), ) { Ok(ok__) => { @@ -1034,7 +1042,12 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::SetAt(this, index, core::mem::transmute_copy(&value)).into() + IVector_Impl::SetAt( + this, + index, + >::abi_to_param(&value), + ) + .into() } } unsafe extern "system" fn InsertAt< @@ -1049,7 +1062,12 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::InsertAt(this, index, core::mem::transmute_copy(&value)).into() + IVector_Impl::InsertAt( + this, + index, + >::abi_to_param(&value), + ) + .into() } } unsafe extern "system" fn RemoveAt< @@ -1077,7 +1095,8 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::Append(this, core::mem::transmute_copy(&value)).into() + IVector_Impl::Append(this, >::abi_to_param(&value)) + .into() } } unsafe extern "system" fn RemoveAtEnd< diff --git a/crates/tests/winrt/reference_no_deps/src/bindings.rs b/crates/tests/winrt/reference_no_deps/src/bindings.rs index 2e456ecdf4d..b96690d0f9d 100644 --- a/crates/tests/winrt/reference_no_deps/src/bindings.rs +++ b/crates/tests/winrt/reference_no_deps/src/bindings.rs @@ -1002,11 +1002,19 @@ where { fn GetAt(&self, index: u32) -> windows_core::Result; fn Size(&self) -> windows_core::Result; - fn IndexOf(&self, value: windows_core::Ref, index: &mut u32) -> windows_core::Result; - fn SetAt(&self, index: u32, value: windows_core::Ref) -> windows_core::Result<()>; - fn InsertAt(&self, index: u32, value: windows_core::Ref) -> windows_core::Result<()>; + fn IndexOf( + &self, + value: windows_core::ImplParam<'_, T>, + index: &mut u32, + ) -> windows_core::Result; + fn SetAt(&self, index: u32, value: windows_core::ImplParam<'_, T>) -> windows_core::Result<()>; + fn InsertAt( + &self, + index: u32, + value: windows_core::ImplParam<'_, T>, + ) -> windows_core::Result<()>; fn RemoveAt(&self, index: u32) -> windows_core::Result<()>; - fn Append(&self, value: windows_core::Ref) -> windows_core::Result<()>; + fn Append(&self, value: windows_core::ImplParam<'_, T>) -> windows_core::Result<()>; fn RemoveAtEnd(&self) -> windows_core::Result<()>; fn Clear(&self) -> windows_core::Result<()>; fn GetMany( @@ -1078,7 +1086,7 @@ impl IVector_Vtbl { &*((this as *const *const ()).offset(OFFSET) as *const Identity); match IVector_Impl::IndexOf( this, - core::mem::transmute_copy(&value), + >::abi_to_param(&value), core::mem::transmute_copy(&index), ) { Ok(ok__) => { @@ -1101,7 +1109,12 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::SetAt(this, index, core::mem::transmute_copy(&value)).into() + IVector_Impl::SetAt( + this, + index, + >::abi_to_param(&value), + ) + .into() } } unsafe extern "system" fn InsertAt< @@ -1116,7 +1129,12 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::InsertAt(this, index, core::mem::transmute_copy(&value)).into() + IVector_Impl::InsertAt( + this, + index, + >::abi_to_param(&value), + ) + .into() } } unsafe extern "system" fn RemoveAt< @@ -1144,7 +1162,8 @@ impl IVector_Vtbl { unsafe { let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); - IVector_Impl::Append(this, core::mem::transmute_copy(&value)).into() + IVector_Impl::Append(this, >::abi_to_param(&value)) + .into() } } unsafe extern "system" fn RemoveAtEnd<