Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Platte <[email protected]>
  • Loading branch information
bendk and jplatte authored Jun 14, 2023
1 parent 7d5c4f8 commit 708a858
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 26 deletions.
4 changes: 2 additions & 2 deletions docs/manual/src/proc_macro/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ pub trait Person {

// Corresponding UDL:
// callback interface Person {
// string name();
// u32 age();
// string name();
// u32 age();
// }
}
```
Expand Down
15 changes: 5 additions & 10 deletions uniffi_macros/src/enum_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,10 @@ pub fn variant_metadata(enum_: &DataEnum) -> syn::Result<Vec<TokenStream>> {
/// implementation which panics.
pub(crate) fn handle_callback_unexpected_error_fn(
handle_unknown_callback_error: bool,
) -> TokenStream {
if handle_unknown_callback_error {
quote! {
fn handle_callback_unexpected_error(e: ::uniffi::UnexpectedUniFFICallbackError) -> Self {
<Self as ::std::convert::From<::uniffi::UnexpectedUniFFICallbackError>>::from(e)
}
) -> Option<TokenStream> {
handle_unknown_callback_error.then(|| quote! {
fn handle_callback_unexpected_error(e: ::uniffi::UnexpectedUniFFICallbackError) -> Self {
<Self as ::std::convert::From<::uniffi::UnexpectedUniFFICallbackError>>::from(e)
}
} else {
// Use the default function
quote! {}
}
})
}
21 changes: 7 additions & 14 deletions uniffi_macros/src/export/callback_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(super) fn trait_impl(
ImplItem::Method(sig) => gen_method_impl(sig, internals_ident),
_ => unreachable!("traits have no constructors"),
})
.collect::<syn::Result<Vec<_>>>()?;
.collect::<syn::Result<TokenStream>>()?;

Ok(quote! {
#[doc(hidden)]
Expand All @@ -40,7 +40,7 @@ pub(super) fn trait_impl(
}
}

impl Drop for #ident {
impl ::std::mem::Drop for #ident {
fn drop(&mut self) {
#internals_ident.invoke_callback::<(), crate::UniFfiTag>(
self.handle, uniffi::IDX_CALLBACK_FREE, Default::default()
Expand All @@ -51,7 +51,7 @@ pub(super) fn trait_impl(
::uniffi::deps::static_assertions::assert_impl_all!(#ident: Send);

impl #trait_ident for #ident {
#(#trait_impl_methods)*
#trait_impl_methods
}

::uniffi::ffi_converter_callback_interface!(#trait_ident, #ident, #trait_name, crate::UniFfiTag);
Expand Down Expand Up @@ -87,20 +87,13 @@ fn gen_method_impl(sig: &FnSignature, internals_ident: &Ident) -> syn::Result<To
}
let params = sig.params();
let buf_ident = Ident::new("uniffi_args_buf", Span::call_site());
let mut write_exprs = sig.write_exprs(&buf_ident).peekable();

let construct_args_buf = if write_exprs.peek().is_some() {
quote! {
let mut #buf_ident = ::std::vec::Vec::new();
#(#write_exprs;)*
}
} else {
quote! { let #buf_ident = ::std::vec::Vec::new(); }
};
let mut write_exprs = sig.write_exprs(&buf_ident);

Ok(quote! {
fn #ident(&self, #(#params),*) -> #return_ty {
#construct_args_buf
#[allow(unused_mut)]
let mut #buf_ident = ::std::vec::Vec::new();
#(#write_exprs;)*
let uniffi_args_rbuf = uniffi::RustBuffer::from_vec(#buf_ident);

#internals_ident.invoke_callback::<#return_ty, crate::UniFfiTag>(self.handle, #index, uniffi_args_rbuf)
Expand Down

0 comments on commit 708a858

Please sign in to comment.