fn from_trait()

in uniffi_macros/src/export/item.rs [134:192]


    fn from_trait(item: syn::ItemTrait, attr_args: TokenStream) -> syn::Result<Self> {
        let args: ExportTraitArgs = syn::parse(attr_args)?;
        let with_foreign = args.callback_interface.is_some() || args.with_foreign.is_some();
        let callback_interface_only = args.callback_interface.is_some();

        if !item.generics.params.is_empty() || item.generics.where_clause.is_some() {
            return Err(syn::Error::new_spanned(
                &item.generics,
                "generic impls are not currently supported by uniffi::export",
            ));
        }

        let self_ident = item.ident.to_owned();
        let docstring = extract_docstring(&item.attrs)?;
        let items = item
            .items
            .into_iter()
            .enumerate()
            .map(|(i, item)| {
                let tim = match item {
                    syn::TraitItem::Fn(tim) => tim,
                    _ => {
                        return Err(syn::Error::new_spanned(
                            item,
                            "only fn's are supported in traits annotated with uniffi::export",
                        ));
                    }
                };

                let docstring = extract_docstring(&tim.attrs)?;
                let attrs = ExportedImplFnAttributes::new(&tim.attrs)?;
                let item = if attrs.constructor {
                    return Err(syn::Error::new_spanned(
                        tim,
                        "exported traits can not have constructors",
                    ));
                } else {
                    ImplItem::Method(FnSignature::new_trait_method(
                        self_ident.clone(),
                        tim.sig,
                        ExportFnArgs::default(),
                        i as u32,
                        docstring,
                    )?)
                };

                Ok(item)
            })
            .collect::<syn::Result<_>>()?;

        Ok(Self::Trait {
            items,
            self_ident,
            with_foreign,
            callback_interface_only,
            docstring,
            args,
        })
    }