From 298b78e383bb0ba69604ea3a8867c661f2d47c51 Mon Sep 17 00:00:00 2001 From: Mark Hammond Date: Sat, 25 Nov 2023 22:04:39 -0500 Subject: [PATCH] Mostly works except a few tests which need minor adjustments. --- fixtures/ext-types/lib/src/lib.rs | 8 +++--- fixtures/ext-types/proc-macro-lib/src/lib.rs | 3 +++ fixtures/ext-types/sub-lib/src/lib.rs | 1 + uniffi_macros/src/lib.rs | 12 +++++++++ uniffi_macros/src/util.rs | 28 +++++--------------- uniffi_meta/src/group.rs | 6 ++--- uniffi_udl/src/attributes.rs | 9 ------- uniffi_udl/src/finder.rs | 3 +-- 8 files changed, 30 insertions(+), 40 deletions(-) diff --git a/fixtures/ext-types/lib/src/lib.rs b/fixtures/ext-types/lib/src/lib.rs index 4ace18b86d..2c0c5998f4 100644 --- a/fixtures/ext-types/lib/src/lib.rs +++ b/fixtures/ext-types/lib/src/lib.rs @@ -8,6 +8,9 @@ use uniffi_one::{ use uniffi_sublib::SubLibType; use url::Url; +uniffi::use_udl_record!(uniffi_sublib, SubLibType); +uniffi::use_udl_trait!(uniffi_one, UniffiOneTrait); + pub struct CombinedType { pub uoe: UniffiOneEnum, pub uot: UniffiOneType, @@ -66,10 +69,7 @@ fn get_combined_type(existing: Option) -> CombinedType { #[derive(Default, uniffi::Record)] pub struct ObjectsType { pub maybe_trait: Option>, - // XXX - can't refer to UniffiOneInterface here - #1854 - //pub maybe_interface: Option>, - // Use this in the meantime so the tests can still refer to it. - pub maybe_interface: Option, + pub maybe_interface: Option>, pub sub: SubLibType, } diff --git a/fixtures/ext-types/proc-macro-lib/src/lib.rs b/fixtures/ext-types/proc-macro-lib/src/lib.rs index ea576527e1..e976743f86 100644 --- a/fixtures/ext-types/proc-macro-lib/src/lib.rs +++ b/fixtures/ext-types/proc-macro-lib/src/lib.rs @@ -13,6 +13,9 @@ uniffi::use_udl_record!(ext_types_guid, Guid); uniffi::use_udl_record!(custom_types, Url); uniffi::use_udl_record!(custom_types, Handle); +uniffi::use_udl_trait!(uniffi_one, UniffiOneTrait); +uniffi::use_udl_record!(uniffi_one, UniffiOneProcMacroType); + #[derive(uniffi::Record)] pub struct CombinedType { pub uoe: UniffiOneEnum, diff --git a/fixtures/ext-types/sub-lib/src/lib.rs b/fixtures/ext-types/sub-lib/src/lib.rs index 565e7f4616..cb32a8670e 100644 --- a/fixtures/ext-types/sub-lib/src/lib.rs +++ b/fixtures/ext-types/sub-lib/src/lib.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use uniffi_one::{UniffiOneEnum, UniffiOneInterface, UniffiOneTrait}; uniffi::use_udl_object!(uniffi_one, UniffiOneInterface); +uniffi::use_udl_trait!(uniffi_one, UniffiOneTrait); uniffi::use_udl_enum!(uniffi_one, UniffiOneEnum); #[derive(Default, uniffi::Record)] diff --git a/uniffi_macros/src/lib.rs b/uniffi_macros/src/lib.rs index b7ba86ddc1..5a8fdf6617 100644 --- a/uniffi_macros/src/lib.rs +++ b/uniffi_macros/src/lib.rs @@ -354,6 +354,18 @@ pub fn use_udl_object(tokens: TokenStream) -> TokenStream { }.into() } +#[proc_macro] +pub fn use_udl_trait(tokens: TokenStream) -> TokenStream { + let util::ExternalTypeItem { + crate_ident, + type_ident, + .. + } = parse_macro_input!(tokens); + quote! { + ::uniffi::ffi_converter_arc_forward!(dyn #type_ident, #crate_ident::UniFfiTag, crate::UniFfiTag); + }.into() +} + /// A helper macro to generate and include component scaffolding. /// /// This is a convenience macro designed for writing `trybuild`-style tests and diff --git a/uniffi_macros/src/util.rs b/uniffi_macros/src/util.rs index 9f213ea1d7..37a72d26a7 100644 --- a/uniffi_macros/src/util.rs +++ b/uniffi_macros/src/util.rs @@ -209,37 +209,21 @@ pub(crate) fn tagged_impl_header( udl_mode: bool, ) -> TokenStream { let trait_name = Ident::new(trait_name, Span::call_site()); - if udl_mode { - quote! { impl ::uniffi::#trait_name for #ident } - } else { - quote! { impl ::uniffi::#trait_name for #ident } - } + quote! { impl ::uniffi::#trait_name for #ident } } pub(crate) fn derive_all_ffi_traits(ty: &Ident, udl_mode: bool) -> TokenStream { - if udl_mode { - quote! { ::uniffi::derive_ffi_traits!(local #ty); } - } else { - quote! { ::uniffi::derive_ffi_traits!(blanket #ty); } - } + quote! { ::uniffi::derive_ffi_traits!(local #ty); } } pub(crate) fn derive_ffi_traits(ty: &Ident, udl_mode: bool, trait_names: &[&str]) -> TokenStream { let trait_idents = trait_names .iter() .map(|name| Ident::new(name, Span::call_site())); - if udl_mode { - quote! { - #( - ::uniffi::derive_ffi_traits!(impl #trait_idents for #ty); - )* - } - } else { - quote! { - #( - ::uniffi::derive_ffi_traits!(impl #trait_idents for #ty); - )* - } + quote! { + #( + ::uniffi::derive_ffi_traits!(impl #trait_idents for #ty); + )* } } diff --git a/uniffi_meta/src/group.rs b/uniffi_meta/src/group.rs index ff011911fc..a1457755a3 100644 --- a/uniffi_meta/src/group.rs +++ b/uniffi_meta/src/group.rs @@ -189,7 +189,7 @@ impl<'a> ExternalTypeConverter<'a> { module_path, name, kind: ExternalKind::DataClass, - tagged: false, + tagged: true, } } Type::Custom { @@ -202,7 +202,7 @@ impl<'a> ExternalTypeConverter<'a> { module_path, name, kind: ExternalKind::DataClass, - tagged: false, + tagged: true, } } Type::Object { @@ -212,7 +212,7 @@ impl<'a> ExternalTypeConverter<'a> { module_path, name, kind: ExternalKind::Interface, - tagged: false, + tagged: true, }, Type::CallbackInterface { module_path, name } if self.is_module_path_external(&module_path) => diff --git a/uniffi_udl/src/attributes.rs b/uniffi_udl/src/attributes.rs index f341e4f007..bfb0145be6 100644 --- a/uniffi_udl/src/attributes.rs +++ b/uniffi_udl/src/attributes.rs @@ -550,15 +550,6 @@ impl TypedefAttributes { _ => None, }) } - - pub(super) fn external_tagged(&self) -> Option { - // If it was "exported" via a proc-macro the FfiConverter was not tagged. - self.0.iter().find_map(|attr| match attr { - Attribute::External { export, .. } => Some(!*export), - Attribute::Rust { .. } => Some(false), - _ => None, - }) - } } impl TryFrom<&weedle::attribute::ExtendedAttributeList<'_>> for TypedefAttributes { diff --git a/uniffi_udl/src/finder.rs b/uniffi_udl/src/finder.rs index fd369f7ca6..26b4ba37f4 100644 --- a/uniffi_udl/src/finder.rs +++ b/uniffi_udl/src/finder.rs @@ -148,13 +148,12 @@ impl TypeFinder for weedle::TypedefDefinition<'_> { // must be external None => { let kind = attrs.external_kind().expect("External missing kind"); - let tagged = attrs.external_tagged().expect("External missing tagged"); Type::External { name, namespace: "".to_string(), // we don't know this yet module_path: attrs.get_crate_name(), kind, - tagged, + tagged: true, } } };