Skip to content

Commit

Permalink
Use Ref for generic type parameters (#3435)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jan 13, 2025
1 parent 83997fb commit 87f268a
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 97 deletions.
5 changes: 4 additions & 1 deletion crates/libs/bindgen/src/types/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(&param.0, Type::Param(_)) {
quote! { core::mem::transmute_copy(&#name) }
} else {
quote! { core::mem::transmute(&#name) }
Expand Down Expand Up @@ -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 }
}
Expand Down
8 changes: 8 additions & 0 deletions crates/libs/core/src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@ pub struct CopyType;
#[doc(hidden)]
pub trait Type<T: TypeKind, C = <T as TypeKind>::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<Self>;
fn from_default(default: &Self::Default) -> Result<Self>;

fn deref(param: &Self::Ref) -> &Self::Default {
unsafe { core::mem::transmute(param) }
}
}

impl<T> Type<T, InterfaceType> for T
where
T: TypeKind<TypeKind = InterfaceType> + Clone,
{
type Abi = *mut core::ffi::c_void;
type Ref = Ref<T>;
type Default = Option<Self>;

fn is_null(abi: &Self::Abi) -> bool {
Expand Down Expand Up @@ -60,6 +66,7 @@ where
T: TypeKind<TypeKind = CloneType> + Clone,
{
type Abi = core::mem::MaybeUninit<Self>;
type Ref = Ref<T>;
type Default = Self;

fn is_null(_: &Self::Abi) -> bool {
Expand All @@ -84,6 +91,7 @@ where
T: TypeKind<TypeKind = CopyType> + Clone,
{
type Abi = Self;
type Ref = Self;
type Default = Self;

fn is_null(_: &Self::Abi) -> bool {
Expand Down
44 changes: 22 additions & 22 deletions crates/libs/windows/src/Windows/Foundation/Collections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,20 +434,20 @@ where
K: windows_core::RuntimeType + 'static,
V: windows_core::RuntimeType + 'static,
{
fn Lookup(&self, key: &<K as windows_core::Type<K>>::Default) -> windows_core::Result<V>;
fn Lookup(&self, key: <K as windows_core::Type<K>>::Ref) -> windows_core::Result<V>;
fn Size(&self) -> windows_core::Result<u32>;
fn HasKey(&self, key: &<K as windows_core::Type<K>>::Default) -> windows_core::Result<bool>;
fn HasKey(&self, key: <K as windows_core::Type<K>>::Ref) -> windows_core::Result<bool>;
fn GetView(&self) -> windows_core::Result<IMapView<K, V>>;
fn Insert(&self, key: &<K as windows_core::Type<K>>::Default, value: &<V as windows_core::Type<V>>::Default) -> windows_core::Result<bool>;
fn Remove(&self, key: &<K as windows_core::Type<K>>::Default) -> windows_core::Result<()>;
fn Insert(&self, key: <K as windows_core::Type<K>>::Ref, value: <V as windows_core::Type<V>>::Ref) -> windows_core::Result<bool>;
fn Remove(&self, key: <K as windows_core::Type<K>>::Ref) -> windows_core::Result<()>;
fn Clear(&self) -> windows_core::Result<()>;
}
impl<K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'static> IMap_Vtbl<K, V> {
pub const fn new<Identity: IMap_Impl<K, V>, const OFFSET: isize>() -> Self {
unsafe extern "system" fn Lookup<K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'static, Identity: IMap_Impl<K, V>, const OFFSET: isize>(this: *mut core::ffi::c_void, key: windows_core::AbiType<K>, result__: *mut windows_core::AbiType<V>) -> 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__);
Expand All @@ -472,7 +472,7 @@ impl<K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'sta
unsafe extern "system" fn HasKey<K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'static, Identity: IMap_Impl<K, V>, const OFFSET: isize>(this: *mut core::ffi::c_void, key: windows_core::AbiType<K>, 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)
Expand All @@ -497,7 +497,7 @@ impl<K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'sta
unsafe extern "system" fn Insert<K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'static, Identity: IMap_Impl<K, V>, const OFFSET: isize>(this: *mut core::ffi::c_void, key: windows_core::AbiType<K>, value: windows_core::AbiType<V>, 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)
Expand All @@ -509,7 +509,7 @@ impl<K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'sta
unsafe extern "system" fn Remove<K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'static, Identity: IMap_Impl<K, V>, const OFFSET: isize>(this: *mut core::ffi::c_void, key: windows_core::AbiType<K>) -> 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<K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'static, Identity: IMap_Impl<K, V>, const OFFSET: isize>(this: *mut core::ffi::c_void) -> windows_core::HRESULT {
Expand Down Expand Up @@ -720,17 +720,17 @@ where
K: windows_core::RuntimeType + 'static,
V: windows_core::RuntimeType + 'static,
{
fn Lookup(&self, key: &<K as windows_core::Type<K>>::Default) -> windows_core::Result<V>;
fn Lookup(&self, key: <K as windows_core::Type<K>>::Ref) -> windows_core::Result<V>;
fn Size(&self) -> windows_core::Result<u32>;
fn HasKey(&self, key: &<K as windows_core::Type<K>>::Default) -> windows_core::Result<bool>;
fn HasKey(&self, key: <K as windows_core::Type<K>>::Ref) -> windows_core::Result<bool>;
fn Split(&self, first: windows_core::OutRef<'_, IMapView<K, V>>, second: windows_core::OutRef<'_, IMapView<K, V>>) -> windows_core::Result<()>;
}
impl<K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'static> IMapView_Vtbl<K, V> {
pub const fn new<Identity: IMapView_Impl<K, V>, const OFFSET: isize>() -> Self {
unsafe extern "system" fn Lookup<K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'static, Identity: IMapView_Impl<K, V>, const OFFSET: isize>(this: *mut core::ffi::c_void, key: windows_core::AbiType<K>, result__: *mut windows_core::AbiType<V>) -> 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__);
Expand All @@ -755,7 +755,7 @@ impl<K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'sta
unsafe extern "system" fn HasKey<K: windows_core::RuntimeType + 'static, V: windows_core::RuntimeType + 'static, Identity: IMapView_Impl<K, V>, const OFFSET: isize>(this: *mut core::ffi::c_void, key: windows_core::AbiType<K>, 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)
Expand Down Expand Up @@ -1380,11 +1380,11 @@ where
fn GetAt(&self, index: u32) -> windows_core::Result<T>;
fn Size(&self) -> windows_core::Result<u32>;
fn GetView(&self) -> windows_core::Result<IVectorView<T>>;
fn IndexOf(&self, value: &<T as windows_core::Type<T>>::Default, index: &mut u32) -> windows_core::Result<bool>;
fn SetAt(&self, index: u32, value: &<T as windows_core::Type<T>>::Default) -> windows_core::Result<()>;
fn InsertAt(&self, index: u32, value: &<T as windows_core::Type<T>>::Default) -> windows_core::Result<()>;
fn IndexOf(&self, value: <T as windows_core::Type<T>>::Ref, index: &mut u32) -> windows_core::Result<bool>;
fn SetAt(&self, index: u32, value: <T as windows_core::Type<T>>::Ref) -> windows_core::Result<()>;
fn InsertAt(&self, index: u32, value: <T as windows_core::Type<T>>::Ref) -> windows_core::Result<()>;
fn RemoveAt(&self, index: u32) -> windows_core::Result<()>;
fn Append(&self, value: &<T as windows_core::Type<T>>::Default) -> windows_core::Result<()>;
fn Append(&self, value: <T as windows_core::Type<T>>::Ref) -> windows_core::Result<()>;
fn RemoveAtEnd(&self) -> windows_core::Result<()>;
fn Clear(&self) -> windows_core::Result<()>;
fn GetMany(&self, startIndex: u32, items: &mut [<T as windows_core::Type<T>>::Default]) -> windows_core::Result<u32>;
Expand Down Expand Up @@ -1433,7 +1433,7 @@ impl<T: windows_core::RuntimeType + 'static> IVector_Vtbl<T> {
unsafe extern "system" fn IndexOf<T: windows_core::RuntimeType + 'static, Identity: IVector_Impl<T>, const OFFSET: isize>(this: *mut core::ffi::c_void, value: windows_core::AbiType<T>, 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)
Expand All @@ -1445,13 +1445,13 @@ impl<T: windows_core::RuntimeType + 'static> IVector_Vtbl<T> {
unsafe extern "system" fn SetAt<T: windows_core::RuntimeType + 'static, Identity: IVector_Impl<T>, const OFFSET: isize>(this: *mut core::ffi::c_void, index: u32, value: windows_core::AbiType<T>) -> 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<T: windows_core::RuntimeType + 'static, Identity: IVector_Impl<T>, const OFFSET: isize>(this: *mut core::ffi::c_void, index: u32, value: windows_core::AbiType<T>) -> 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<T: windows_core::RuntimeType + 'static, Identity: IVector_Impl<T>, const OFFSET: isize>(this: *mut core::ffi::c_void, index: u32) -> windows_core::HRESULT {
Expand All @@ -1463,7 +1463,7 @@ impl<T: windows_core::RuntimeType + 'static> IVector_Vtbl<T> {
unsafe extern "system" fn Append<T: windows_core::RuntimeType + 'static, Identity: IVector_Impl<T>, const OFFSET: isize>(this: *mut core::ffi::c_void, value: windows_core::AbiType<T>) -> 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<T: windows_core::RuntimeType + 'static, Identity: IVector_Impl<T>, const OFFSET: isize>(this: *mut core::ffi::c_void) -> windows_core::HRESULT {
Expand Down Expand Up @@ -1687,7 +1687,7 @@ where
{
fn GetAt(&self, index: u32) -> windows_core::Result<T>;
fn Size(&self) -> windows_core::Result<u32>;
fn IndexOf(&self, value: &<T as windows_core::Type<T>>::Default, index: &mut u32) -> windows_core::Result<bool>;
fn IndexOf(&self, value: <T as windows_core::Type<T>>::Ref, index: &mut u32) -> windows_core::Result<bool>;
fn GetMany(&self, startIndex: u32, items: &mut [<T as windows_core::Type<T>>::Default]) -> windows_core::Result<u32>;
}
impl<T: windows_core::RuntimeType + 'static> IVectorView_Vtbl<T> {
Expand Down Expand Up @@ -1720,7 +1720,7 @@ impl<T: windows_core::RuntimeType + 'static> IVectorView_Vtbl<T> {
unsafe extern "system" fn IndexOf<T: windows_core::RuntimeType + 'static, Identity: IVectorView_Impl<T>, const OFFSET: isize>(this: *mut core::ffi::c_void, value: windows_core::AbiType<T>, 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)
Expand Down
Loading

0 comments on commit 87f268a

Please sign in to comment.