Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate Vtable support from Interface trait #2051

Merged
merged 3 commits into from Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
14 changes: 7 additions & 7 deletions crates/libs/bindgen/src/com_methods.rs
Expand Up @@ -34,7 +34,7 @@ pub fn gen(gen: &Gen, def: TypeDef, kind: InterfaceKind, method: MethodDef, meth
#features
pub unsafe fn #name<#generics>(&self, #params) -> ::windows::core::Result<T> #where_clause {
let mut result__ = ::core::option::Option::None;
(::windows::core::Interface::vtable(self)#bases.#vname)(::windows::core::Interface::as_raw(self), #args).and_some(result__)
(::windows::core::Vtable::vtable(self)#bases.#vname)(::windows::core::Vtable::as_raw(self), #args).and_some(result__)
}
}
}
Expand All @@ -48,7 +48,7 @@ pub fn gen(gen: &Gen, def: TypeDef, kind: InterfaceKind, method: MethodDef, meth
#doc
#features
pub unsafe fn #name<#generics>(&self, #params result__: *mut ::core::option::Option<T>) -> ::windows::core::Result<()> #where_clause {
(::windows::core::Interface::vtable(self)#bases.#vname)(::windows::core::Interface::as_raw(self), #args).ok()
(::windows::core::Vtable::vtable(self)#bases.#vname)(::windows::core::Vtable::as_raw(self), #args).ok()
}
}
}
Expand All @@ -64,7 +64,7 @@ pub fn gen(gen: &Gen, def: TypeDef, kind: InterfaceKind, method: MethodDef, meth
#features
pub unsafe fn #name<#generics>(&self, #params) -> ::windows::core::Result<#return_type_tokens> #where_clause {
let mut result__ = ::core::mem::MaybeUninit::zeroed();
(::windows::core::Interface::vtable(self)#bases.#vname)(::windows::core::Interface::as_raw(self), #args ::core::mem::transmute(result__.as_mut_ptr()))
(::windows::core::Vtable::vtable(self)#bases.#vname)(::windows::core::Vtable::as_raw(self), #args ::core::mem::transmute(result__.as_mut_ptr()))
.from_abi::<#return_type_tokens>(result__ )
}
}
Expand All @@ -77,7 +77,7 @@ pub fn gen(gen: &Gen, def: TypeDef, kind: InterfaceKind, method: MethodDef, meth
#doc
#features
pub unsafe fn #name<#generics>(&self, #params) -> ::windows::core::Result<()> #where_clause {
(::windows::core::Interface::vtable(self)#bases.#vname)(::windows::core::Interface::as_raw(self), #args).ok()
(::windows::core::Vtable::vtable(self)#bases.#vname)(::windows::core::Vtable::as_raw(self), #args).ok()
}
}
}
Expand All @@ -91,7 +91,7 @@ pub fn gen(gen: &Gen, def: TypeDef, kind: InterfaceKind, method: MethodDef, meth
#features
pub unsafe fn #name<#generics>(&self, #params) -> #return_type #where_clause {
let mut result__: #return_type = :: core::mem::zeroed();
(::windows::core::Interface::vtable(self)#bases.#vname)(::windows::core::Interface::as_raw(self), &mut result__, #args);
(::windows::core::Vtable::vtable(self)#bases.#vname)(::windows::core::Vtable::as_raw(self), &mut result__, #args);
result__
}
}
Expand All @@ -105,7 +105,7 @@ pub fn gen(gen: &Gen, def: TypeDef, kind: InterfaceKind, method: MethodDef, meth
#doc
#features
pub unsafe fn #name<#generics>(&self, #params) #return_type #where_clause {
(::windows::core::Interface::vtable(self)#bases.#vname)(::windows::core::Interface::as_raw(self), #args)
(::windows::core::Vtable::vtable(self)#bases.#vname)(::windows::core::Vtable::as_raw(self), #args)
}
}
}
Expand All @@ -117,7 +117,7 @@ pub fn gen(gen: &Gen, def: TypeDef, kind: InterfaceKind, method: MethodDef, meth
#doc
#features
pub unsafe fn #name<#generics>(&self, #params) #where_clause {
(::windows::core::Interface::vtable(self)#bases.#vname)(::windows::core::Interface::as_raw(self), #args)
(::windows::core::Vtable::vtable(self)#bases.#vname)(::windows::core::Vtable::as_raw(self), #args)
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions crates/libs/bindgen/src/gen.rs
Expand Up @@ -713,8 +713,11 @@ impl<'a> Gen<'a> {
let vtbl = self.type_vtbl_name(&default);
quote! {
#features
unsafe impl ::windows::core::Interface for #ident {
unsafe impl ::windows::core::Vtable for #ident {
type Vtable = #vtbl;
}
#features
unsafe impl ::windows::core::Interface for #ident {
const IID: ::windows::core::GUID = <#default_name as ::windows::core::Interface>::IID;
}
}
Expand All @@ -736,8 +739,11 @@ impl<'a> Gen<'a> {
};
quote! {
#features
unsafe impl<#constraints> ::windows::core::Interface for #ident {
unsafe impl<#constraints> ::windows::core::Vtable for #ident {
type Vtable = #vtbl;
}
#features
unsafe impl<#constraints> ::windows::core::Interface for #ident {
const IID: ::windows::core::GUID = #guid;
}
}
Expand All @@ -752,7 +758,7 @@ impl<'a> Gen<'a> {

match self.reader.type_def_vtables(def).last() {
Some(Type::IUnknown) => methods.combine(&quote! { pub base__: ::windows::core::IUnknown_Vtbl, }),
Some(Type::IInspectable) => methods.combine(&quote! { pub base__: ::windows::core::IInspectableVtbl, }),
Some(Type::IInspectable) => methods.combine(&quote! { pub base__: ::windows::core::IInspectable_Vtbl, }),
Some(Type::TypeDef((def, _))) => {
let vtbl = self.type_def_vtbl_name(*def, &[]);
methods.combine(&quote! { pub base__: #vtbl, });
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/implements.rs
Expand Up @@ -93,7 +93,7 @@ pub fn gen(gen: &Gen, def: TypeDef) -> TokenStream {

match gen.reader.type_def_vtables(def).last() {
Some(Type::IUnknown) => methods.combine(&quote! { base__: ::windows::core::IUnknown_Vtbl::new::<Identity, OFFSET>(), }),
Some(Type::IInspectable) => methods.combine(&quote! { base__: ::windows::core::IInspectableVtbl::new::<Identity, #type_ident, OFFSET>(), }),
Some(Type::IInspectable) => methods.combine(&quote! { base__: ::windows::core::IInspectable_Vtbl::new::<Identity, #type_ident, OFFSET>(), }),
Some(Type::TypeDef((def, generics))) => {
let name = gen.type_def_name_imp(*def, generics, "_Vtbl");
methods.combine(&quote! { base__: #name::new::<Identity, Impl, OFFSET>(), });
Expand Down
8 changes: 4 additions & 4 deletions crates/libs/bindgen/src/winrt_methods.rs
Expand Up @@ -58,7 +58,7 @@ pub fn gen(gen: &Gen, def: TypeDef, generic_types: &[Type], kind: InterfaceKind,
(
quote! {
let mut result__ = ::core::mem::MaybeUninit::zeroed();
(::windows::core::Interface::vtable(this).#vname)(::windows::core::Interface::as_raw(this), #args #composable_args #return_arg)
(::windows::core::Vtable::vtable(this).#vname)(::windows::core::Vtable::as_raw(this), #args #composable_args #return_arg)
.and_then(|| result__.assume_init())
},
quote! {},
Expand All @@ -67,20 +67,20 @@ pub fn gen(gen: &Gen, def: TypeDef, generic_types: &[Type], kind: InterfaceKind,
(
quote! {
let mut result__ = ::core::mem::MaybeUninit::zeroed();
(::windows::core::Interface::vtable(this).#vname)(::windows::core::Interface::as_raw(this), #args #composable_args #return_arg)
(::windows::core::Vtable::vtable(this).#vname)(::windows::core::Vtable::as_raw(this), #args #composable_args #return_arg)
.from_abi::<#return_type_tokens>(result__ )
},
quote! {
let mut result__ = ::core::mem::MaybeUninit::zeroed();
(::windows::core::Interface::vtable(this).#vname)(::windows::core::Interface::as_raw(this), #args ::core::ptr::null_mut(), &mut ::core::option::Option::<::windows::core::IInspectable>::None as *mut _ as _, #return_arg)
(::windows::core::Vtable::vtable(this).#vname)(::windows::core::Vtable::as_raw(this), #args ::core::ptr::null_mut(), &mut ::core::option::Option::<::windows::core::IInspectable>::None as *mut _ as _, #return_arg)
.from_abi::<#return_type_tokens>(result__ )
},
)
}
} else {
(
quote! {
(::windows::core::Interface::vtable(this).#vname)(::windows::core::Interface::as_raw(this), #args #composable_args).ok()
(::windows::core::Vtable::vtable(this).#vname)(::windows::core::Vtable::as_raw(this), #args #composable_args).ok()
},
quote! {},
)
Expand Down
10 changes: 5 additions & 5 deletions crates/libs/implement/src/lib.rs
Expand Up @@ -55,7 +55,7 @@ pub fn implement(attributes: proc_macro::TokenStream, original_type: proc_macro:
}
impl <#constraints> ::windows::core::AsImpl<#original_ident::<#(#generics,)*>> for #interface_ident {
fn as_impl(&self) -> &#original_ident::<#(#generics,)*> {
let this = ::windows::core::Interface::as_raw(self);
let this = ::windows::core::Vtable::as_raw(self);
// SAFETY: the offset is guranteed to be in bounds, and the implementation struct
// is guaranteed to live at least as long as `self`.
unsafe {
Expand All @@ -73,14 +73,14 @@ pub fn implement(attributes: proc_macro::TokenStream, original_type: proc_macro:
#[repr(C)]
struct #impl_ident<#(#generics,)*> where #constraints {
base: ::core::option::Option<::windows::core::IInspectable>,
identity: *const ::windows::core::IInspectableVtbl,
identity: *const ::windows::core::IInspectable_Vtbl,
vtables: (#(*const #vtbl_idents,)*),
this: #original_ident::<#(#generics,)*>,
count: ::windows::core::WeakRefCount,
}
impl <#constraints> #impl_ident::<#(#generics,)*> {
const VTABLES: (#(#vtbl_idents2,)*) = (#(#vtable_news,)*);
const IDENTITY: ::windows::core::IInspectableVtbl = ::windows::core::IInspectableVtbl::new::<Self, #identity_type, -1>();
const IDENTITY: ::windows::core::IInspectable_Vtbl = ::windows::core::IInspectable_Vtbl::new::<Self, #identity_type, -1>();
fn new(this: #original_ident::<#(#generics,)*>) -> Self {
Self {
base: ::core::option::Option::None,
Expand Down Expand Up @@ -153,7 +153,7 @@ pub fn implement(attributes: proc_macro::TokenStream, original_type: proc_macro:
impl <#constraints> ::windows::core::Compose for #original_ident::<#(#generics,)*> {
unsafe fn compose<'a>(implementation: Self) -> (::windows::core::IInspectable, &'a mut ::core::option::Option<::windows::core::IInspectable>) {
let inspectable: ::windows::core::IInspectable = implementation.into();
let this: *mut ::core::ffi::c_void = ::windows::core::Interface::as_raw(&inspectable);
let this: *mut ::core::ffi::c_void = ::windows::core::Vtable::as_raw(&inspectable);
let this = (this as *mut *mut ::core::ffi::c_void).sub(1) as *mut #impl_ident::<#(#generics,)*>;
(inspectable, &mut (*this).base)
}
Expand Down Expand Up @@ -199,7 +199,7 @@ impl ImplementType {
fn to_vtbl_ident(&self) -> proc_macro2::TokenStream {
let ident = self.to_ident();
quote! {
<#ident as ::windows::core::Interface>::Vtable
<#ident as ::windows::core::Vtable>::Vtable
}
}
fn generics(&self) -> std::collections::BTreeSet<String> {
Expand Down
6 changes: 4 additions & 2 deletions crates/libs/interface/src/lib.rs
Expand Up @@ -81,8 +81,10 @@ impl Interface {
#vis struct #name(#parent);
#implementation

unsafe impl ::windows::core::Interface for #name {
unsafe impl ::windows::core::Vtable for #name {
type Vtable = #vtable_name;
}
unsafe impl ::windows::core::Interface for #name {
const IID: ::windows::core::GUID = #guid;
}
impl ::windows::core::RuntimeName for #name {}
Expand Down Expand Up @@ -115,7 +117,7 @@ impl Interface {
let ret = &m.ret;
quote! {
#vis unsafe fn #name(&self, #(#args),*) #ret {
(::windows::core::Interface::vtable(self).#name)(::windows::core::Interface::as_raw(self), #(#params),*)
(::windows::core::Vtable::vtable(self).#name)(::windows::core::Vtable::as_raw(self), #(#params),*)
}
}
})
Expand Down
Expand Up @@ -61,7 +61,7 @@ impl ILearningModelVariableDescriptorPreview_Vtbl {
}
}
Self {
base__: ::windows::core::IInspectableVtbl::new::<Identity, ILearningModelVariableDescriptorPreview, OFFSET>(),
base__: ::windows::core::IInspectable_Vtbl::new::<Identity, ILearningModelVariableDescriptorPreview, OFFSET>(),
Name: Name::<Identity, Impl, OFFSET>,
Description: Description::<Identity, Impl, OFFSET>,
ModelFeatureKind: ModelFeatureKind::<Identity, Impl, OFFSET>,
Expand Down