Skip to content

Commit

Permalink
chore(ffi): Merge export and export_async attribute macros
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte authored and bnjbvr committed Oct 11, 2024
1 parent 41a2ad0 commit e46e637
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 61 deletions.
65 changes: 26 additions & 39 deletions bindings/matrix-sdk-ffi-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,65 +14,52 @@

use proc_macro::TokenStream;
use quote::quote;
use syn::{spanned::Spanned as _, ImplItem, Item};
use syn::{ImplItem, Item, TraitItem};

/// Attribute to always specify the async runtime parameter for the `uniffi`
/// export macros.
#[proc_macro_attribute]
pub fn export_async(_attr: TokenStream, item: TokenStream) -> TokenStream {
let item = proc_macro2::TokenStream::from(item);

quote! {
#[uniffi::export(async_runtime = "tokio")]
#item
}
.into()
}

/// Attribute to always specify the async runtime parameter for the `uniffi`
/// export macros.
/// Attribute to specify the async runtime parameter for the `uniffi`
/// export macros if there any `async fn`s in the input.
#[proc_macro_attribute]
pub fn export(attr: TokenStream, item: TokenStream) -> TokenStream {
let run_checks = || {
let item: Item = syn::parse(item.clone())?;
let has_async_fn = |item| {
if let Item::Fn(fun) = &item {
// Fail compilation if the function is async.
if fun.sig.asyncness.is_some() {
let error = syn::Error::new(
fun.span(),
"async function must be exported with #[export_async]",
);
return Err(error);
return true;
}
} else if let Item::Impl(blk) = &item {
// Fail compilation if at least one function in the impl block is async.
for item in &blk.items {
if let ImplItem::Fn(fun) = item {
if fun.sig.asyncness.is_some() {
let error = syn::Error::new(
blk.span(),
"impl block with async functions must be exported with #[export_async]",
);
return Err(error);
return true;
}
}
}
} else if let Item::Trait(blk) = &item {
for item in &blk.items {
if let TraitItem::Fn(fun) = item {
if fun.sig.asyncness.is_some() {
return true;
}
}
}
}

Ok(())
false
};

let maybe_error =
if let Err(err) = run_checks() { Some(err.into_compile_error()) } else { None };
let attr2 = proc_macro2::TokenStream::from(attr);
let item2 = proc_macro2::TokenStream::from(item.clone());

let item = proc_macro2::TokenStream::from(item);
let attr = proc_macro2::TokenStream::from(attr);
let res = match syn::parse(item) {
Ok(item) => match has_async_fn(item) {
true => quote! { #[uniffi::export(async_runtime = "tokio", #attr2)] },
false => quote! { #[uniffi::export(#attr2)] },
},
Err(e) => e.into_compile_error(),
};

quote! {
#maybe_error

#[uniffi::export(#attr)]
#item
#res
#item2
}
.into()
}
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct SsoHandler {
pub(crate) url: String,
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl SsoHandler {
/// Returns the URL for starting SSO authentication. The URL should be
/// opened in a web view. Once the web view succeeds, call `finish` with
Expand Down
4 changes: 2 additions & 2 deletions bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl Client {
}
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl Client {
/// Information about login options for the client's homeserver.
pub async fn homeserver_login_details(&self) -> Arc<HomeserverLoginDetails> {
Expand Down Expand Up @@ -526,7 +526,7 @@ impl Client {
}
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl Client {
/// The sliding sync version.
pub fn sliding_sync_version(&self) -> SlidingSyncVersion {
Expand Down
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/client_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub struct ClientBuilder {
request_config: Option<RequestConfig>,
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl ClientBuilder {
#[uniffi::constructor]
pub fn new() -> Arc<Self> {
Expand Down
6 changes: 3 additions & 3 deletions bindings/matrix-sdk-ffi/src/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl From<encryption::VerificationState> for VerificationState {
}
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl Encryption {
/// Get the public ed25519 key of our own device. This is usually what is
/// called the fingerprint of the device.
Expand Down Expand Up @@ -432,7 +432,7 @@ pub struct UserIdentity {
inner: matrix_sdk::encryption::identities::UserIdentity,
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl UserIdentity {
/// Remember this identity, ensuring it does not result in a pin violation.
///
Expand Down Expand Up @@ -468,7 +468,7 @@ pub struct IdentityResetHandle {
pub(crate) inner: matrix_sdk::encryption::recovery::IdentityResetHandle,
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl IdentityResetHandle {
/// Get the underlying [`CrossSigningResetAuthType`] this identity reset
/// process is using.
Expand Down
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub struct NotificationClient {
pub(crate) _client: Arc<Client>,
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl NotificationClient {
/// See also documentation of
/// `MatrixNotificationClient::get_notification`.
Expand Down
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/notification_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Drop for NotificationSettings {
}
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl NotificationSettings {
pub fn set_delegate(&self, delegate: Option<Box<dyn NotificationSettingsDelegate>>) {
if let Some(delegate) = delegate {
Expand Down
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Room {
}
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl Room {
pub fn id(&self) -> String {
self.inner.room_id().to_string()
Expand Down
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/room_directory_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl RoomDirectorySearch {
}
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl RoomDirectorySearch {
pub async fn next_page(&self) -> Result<(), ClientError> {
let mut inner = self.inner.write().await;
Expand Down
4 changes: 2 additions & 2 deletions bindings/matrix-sdk-ffi/src/room_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub struct RoomListService {
pub(crate) utd_hook: Option<Arc<UtdHookManager>>,
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl RoomListService {
fn state(&self, listener: Box<dyn RoomListServiceStateListener>) -> Arc<TaskHandle> {
let state_stream = self.inner.state();
Expand Down Expand Up @@ -549,7 +549,7 @@ impl RoomListItem {
}
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl RoomListItem {
fn id(&self) -> String {
self.inner.id().to_string()
Expand Down
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/session_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub struct SessionVerificationController {
sas_verification: Arc<RwLock<Option<SasVerification>>>,
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl SessionVerificationController {
pub async fn is_verified(&self) -> Result<bool, ClientError> {
let device =
Expand Down
4 changes: 2 additions & 2 deletions bindings/matrix-sdk-ffi/src/sync_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct SyncService {
utd_hook: Option<Arc<UtdHookManager>>,
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl SyncService {
pub fn room_list_service(&self) -> Arc<RoomListService> {
Arc::new(RoomListService {
Expand Down Expand Up @@ -110,7 +110,7 @@ impl SyncServiceBuilder {
}
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl SyncServiceBuilder {
pub fn with_cross_process_lock(self: Arc<Self>, app_identifier: Option<String>) -> Arc<Self> {
let this = unwrap_or_clone_arc(self);
Expand Down
6 changes: 3 additions & 3 deletions bindings/matrix-sdk-ffi/src/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Timeline {
}
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl Timeline {
pub async fn add_listener(&self, listener: Box<dyn TimelineListener>) -> Arc<TaskHandle> {
let (timeline_items, timeline_stream) = self.inner.subscribe_batched().await;
Expand Down Expand Up @@ -688,7 +688,7 @@ pub struct SendHandle {
inner: Mutex<Option<matrix_sdk::send_queue::SendHandle>>,
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl SendHandle {
/// Try to abort the sending of the current event.
///
Expand Down Expand Up @@ -1182,7 +1182,7 @@ impl SendAttachmentJoinHandle {
}
}

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl SendAttachmentJoinHandle {
pub async fn join(&self) -> Result<(), RoomError> {
let join_hdl = self.join_hdl.clone();
Expand Down
6 changes: 3 additions & 3 deletions bindings/matrix-sdk-ffi/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn make_widget_driver(settings: WidgetSettings) -> Result<WidgetDriverAndHan
#[derive(uniffi::Object)]
pub struct WidgetDriver(Mutex<Option<matrix_sdk::widget::WidgetDriver>>);

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl WidgetDriver {
pub async fn run(
&self,
Expand Down Expand Up @@ -96,7 +96,7 @@ impl From<matrix_sdk::widget::WidgetSettings> for WidgetSettings {
/// * `room` - A matrix room which is used to query the logged in username
/// * `props` - Properties from the client that can be used by a widget to adapt
/// to the client. e.g. language, font-scale...
#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
pub async fn generate_webview_url(
widget_settings: WidgetSettings,
room: Arc<Room>,
Expand Down Expand Up @@ -354,7 +354,7 @@ impl From<ClientProperties> for matrix_sdk::widget::ClientProperties {
#[derive(uniffi::Object)]
pub struct WidgetDriverHandle(matrix_sdk::widget::WidgetDriverHandle);

#[matrix_sdk_ffi_macros::export_async]
#[matrix_sdk_ffi_macros::export]
impl WidgetDriverHandle {
/// Receive a message from the widget driver.
///
Expand Down

0 comments on commit e46e637

Please sign in to comment.