7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | // Copyright 2024 The Matrix.org Foundation C.I.C. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | // you may not use this file except in compliance with the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | // You may obtain a copy of the License at |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | // Unless required by applicable law or agreed to in writing, software |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | // See the License for the specific language governing permissions and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | // limitations under the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | use proc_macro::TokenStream; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | use quote::quote; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | use syn::{ImplItem, Item, TraitItem}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | /// Attribute to specify the async runtime parameter for the `uniffi` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | /// export macros if there any `async fn`s in the input. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | #[proc_macro_attribute] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | pub fn export(attr: TokenStream, item: TokenStream) -> TokenStream { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | let has_async_fn = |item| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | if let Item::Fn(fun) = &item { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | if fun.sig.asyncness.is_some() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | return true; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | } else if let Item::Impl(blk) = &item { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | for item in &blk.items { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | if let ImplItem::Fn(fun) = item |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | && fun.sig.asyncness.is_some() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | return true; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | } else if let Item::Trait(blk) = &item { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | for item in &blk.items { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | if let TraitItem::Fn(fun) = item |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | && fun.sig.asyncness.is_some() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | return true; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | false |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | let attr2 = proc_macro2::TokenStream::from(attr); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | let item2 = proc_macro2::TokenStream::from(item.clone()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | let res = match syn::parse(item) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | Ok(item) => match has_async_fn(item) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | true => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | quote! { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | #[cfg_attr(target_family = "wasm", uniffi::export(#attr2))] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | #[cfg_attr(not(target_family = "wasm"), uniffi::export(async_runtime = "tokio", #attr2))] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | false => quote! { #[uniffi::export(#attr2)] }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | Err(e) => e.into_compile_error(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | quote! { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | #res |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | #item2 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | .into() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | } |