Skip to content

Commit

Permalink
Improve ABI conversion (#1746)
Browse files Browse the repository at this point in the history
* Abi docs improvements

* Change to using MaybeUninit when convering from abi

* Run codegen

* Initialize result__ to zero because we may read it even on error

* Rerun codegen

* Remove need for ManuallyDrop around non-blittable types

* Run codegen once again

* Fix issue with non-blittable ABI types

* Revert manually drop change

* Final weaks to the docs

* Add clarifying note about no need for AddRef
  • Loading branch information
rylev authored May 11, 2022
1 parent c178210 commit 8281cc9
Show file tree
Hide file tree
Showing 534 changed files with 121,582 additions and 121,493 deletions.
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ fn gen_win_function(def: &MethodDef, gen: &Gen) -> TokenStream {
extern "system" {
fn #name(#(#abi_params),*) #abi_return_type;
}
let mut result__: #abi_return_type_tokens = ::core::mem::zeroed();
#name(#args ::core::mem::transmute(&mut result__)).from_abi::<#return_type_tokens>(result__)
let mut result__ = ::core::mem::MaybeUninit::<#abi_return_type_tokens>::zeroed();
#name(#args ::core::mem::transmute(result__.as_mut_ptr())).from_abi::<#return_type_tokens>(result__)
}
#[cfg(not(windows))]
unimplemented!("Unsupported target OS");
Expand Down
24 changes: 12 additions & 12 deletions crates/libs/bindgen/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ pub fn gen_winrt_method(def: &TypeDef, kind: InterfaceKind, method: &MethodDef,
let return_arg = if let Some(return_type) = &signature.return_type {
if return_type.is_winrt_array() {
let return_type = gen_element_name(return_type, gen);
quote! { ::windows::core::Array::<#return_type>::set_abi_len(&mut result__), &mut result__ as *mut _ as _ }
quote! { ::windows::core::Array::<#return_type>::set_abi_len(result__.assume_init_mut()), result__.as_mut_ptr() as *mut _ as _ }
} else {
quote! { &mut result__ }
quote! { result__.as_mut_ptr() }
}
} else {
quote! {}
Expand All @@ -56,9 +56,9 @@ pub fn gen_winrt_method(def: &TypeDef, kind: InterfaceKind, method: &MethodDef,
if return_type.is_winrt_array() {
(
quote! {
let mut result__: #return_type_tokens = ::core::mem::zeroed();
let mut result__ = ::core::mem::MaybeUninit::<#return_type_tokens>::zeroed();
(::windows::core::Interface::vtable(this).#vname)(::windows::core::Interface::as_raw(this), #(#args,)* #composable_args #return_arg)
.and_then(|| result__ )
.and_then(|| result__.assume_init() )
},
quote! {},
)
Expand All @@ -68,14 +68,14 @@ pub fn gen_winrt_method(def: &TypeDef, kind: InterfaceKind, method: &MethodDef,

(
quote! {
let mut result__: #abi_type_name = ::core::mem::zeroed();
(::windows::core::Interface::vtable(this).#vname)(::windows::core::Interface::as_raw(this), #args #composable_args #return_arg)
.from_abi::<#return_type_tokens>(result__ )
let mut result__ = ::core::mem::MaybeUninit::<#abi_type_name>::zeroed();
(::windows::core::Interface::vtable(this).#vname)(::windows::core::Interface::as_raw(this), #args #composable_args #return_arg)
.from_abi::<#return_type_tokens>(result__)
},
quote! {
let mut result__: #abi_type_name = ::core::mem::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)
.from_abi::<#return_type_tokens>(result__ )
let mut result__ = ::core::mem::MaybeUninit::<#abi_type_name>::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)
.from_abi::<#return_type_tokens>(result__)
},
)
}
Expand Down Expand Up @@ -201,8 +201,8 @@ pub fn gen_com_method(def: &TypeDef, kind: InterfaceKind, method: &MethodDef, me
#doc
#features
pub unsafe fn #name<#constraints>(&self, #params) -> ::windows::core::Result<#return_type_tokens> {
let mut result__: #abi_return_type_tokens = ::core::mem::zeroed();
(::windows::core::Interface::vtable(self)#bases.#vname)(::windows::core::Interface::as_raw(self), #args ::core::mem::transmute(&mut result__))
let mut result__ = ::core::mem::MaybeUninit::<#abi_return_type_tokens>::zeroed();
(::windows::core::Interface::vtable(self)#bases.#vname)(::windows::core::Interface::as_raw(self), #args ::core::mem::transmute(result__.as_mut_ptr()))
.from_abi::<#return_type_tokens>(result__ )
}
}
Expand Down
1 change: 0 additions & 1 deletion crates/libs/bindgen/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ fn gen_windows_traits(def: &TypeDef, name: &TokenStream, cfg: &Cfg, gen: &Gen) -
} else {
quote! { ::core::mem::ManuallyDrop<Self> }
};

let features = gen.cfg(cfg);

let mut tokens = quote! {
Expand Down
Loading

0 comments on commit 8281cc9

Please sign in to comment.