Skip to content

Commit c7f6caa

Browse files
jhugmanbendk
authored andcommitted
Move single-threaded annotation for callback-interface and traits
1 parent d5e62bb commit c7f6caa

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed

uniffi_macros/src/export/callback_interface.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use crate::{
77
ffiops,
88
fnsig::{FnKind, FnSignature, ReceiverArg},
99
util::{
10-
create_metadata_items, derive_ffi_traits, ident_to_string, mod_path, tagged_impl_header,
10+
async_trait_annotation, create_metadata_items, derive_ffi_traits, ident_to_string,
11+
mod_path, tagged_impl_header, wasm_single_threaded_annotation,
1112
},
1213
};
1314
use proc_macro2::{Span, TokenStream};
@@ -77,7 +78,11 @@ pub(super) fn trait_impl(
7778
.map(|sig| gen_method_impl(sig, &vtable_cell))
7879
.collect::<syn::Result<Vec<_>>>()?;
7980
let has_async_method = methods.iter().any(|m| m.is_async);
80-
let impl_attributes = has_async_method.then(|| quote! { #[::async_trait::async_trait] });
81+
82+
// Conditionally apply the async_trait attribute with or without ?Send based on the target
83+
let impl_attributes = has_async_method.then(async_trait_annotation);
84+
85+
let single_threaded_annotation = wasm_single_threaded_annotation();
8186

8287
Ok(quote! {
8388
#[allow(missing_docs)]
@@ -105,6 +110,7 @@ pub(super) fn trait_impl(
105110
}
106111
}
107112

113+
#single_threaded_annotation
108114
::uniffi::deps::static_assertions::assert_impl_all!(#trait_impl_ident: ::core::marker::Send);
109115

110116
#impl_attributes

uniffi_macros/src/export/trait_interface.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
},
1414
ffiops,
1515
object::interface_meta_static_var,
16-
util::{ident_to_string, tagged_impl_header},
16+
util::{ident_to_string, tagged_impl_header, wasm_single_threaded_annotation},
1717
};
1818

1919
pub(super) fn gen_trait_scaffolding(
@@ -155,12 +155,14 @@ pub(crate) fn ffi_converter(
155155
};
156156
let lower_self = ffiops::lower(quote! { ::std::sync::Arc<Self> });
157157
let try_lift_self = ffiops::try_lift(quote! { ::std::sync::Arc<Self> });
158+
let single_threaded_annotation = wasm_single_threaded_annotation();
158159

159160
quote! {
160161
// All traits must be `Sync + Send`. The generated scaffolding will fail to compile
161162
// if they are not, but unfortunately it fails with an unactionably obscure error message.
162163
// By asserting the requirement explicitly, we help Rust produce a more scrutable error message
163164
// and thus help the user debug why the requirement isn't being met.
165+
#single_threaded_annotation
164166
::uniffi::deps::static_assertions::assert_impl_all!(
165167
dyn #trait_ident: ::core::marker::Sync, ::core::marker::Send
166168
);

uniffi_macros/src/object.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use syn::DeriveInput;
44

55
use crate::{
66
ffiops,
7-
util::{create_metadata_items, extract_docstring, ident_to_string, mod_path},
7+
util::{
8+
create_metadata_items, extract_docstring, ident_to_string, mod_path,
9+
wasm_single_threaded_annotation,
10+
},
811
DeriveOptions,
912
};
1013
use uniffi_meta::ObjectImpl;
@@ -96,19 +99,6 @@ pub fn expand_object(input: DeriveInput, options: DeriveOptions) -> syn::Result<
9699
})
97100
}
98101

99-
fn wasm_single_threaded_annotation() -> TokenStream {
100-
#[cfg(feature = "wasm-unstable-single-threaded")]
101-
{
102-
quote! {
103-
#[cfg(not(target_arch = "wasm32"))]
104-
}
105-
}
106-
#[cfg(not(feature = "wasm-unstable-single-threaded"))]
107-
{
108-
TokenStream::default()
109-
}
110-
}
111-
112102
fn interface_impl(object: &ObjectItem, options: &DeriveOptions) -> TokenStream {
113103
let name = object.name();
114104
let ident = object.ident();

uniffi_macros/src/util.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,32 @@ pub(crate) fn extract_docstring(attrs: &[Attribute]) -> syn::Result<String> {
302302
.collect::<syn::Result<Vec<_>>>()
303303
.map(|lines| lines.join("\n"))
304304
}
305+
306+
pub(crate) fn wasm_single_threaded_annotation() -> TokenStream {
307+
#[cfg(feature = "wasm-unstable-single-threaded")]
308+
{
309+
quote! {
310+
#[cfg(not(target_arch = "wasm32"))]
311+
}
312+
}
313+
#[cfg(not(feature = "wasm-unstable-single-threaded"))]
314+
{
315+
TokenStream::default()
316+
}
317+
}
318+
319+
pub(crate) fn async_trait_annotation() -> TokenStream {
320+
#[cfg(feature = "wasm-unstable-single-threaded")]
321+
{
322+
quote! {
323+
#[cfg_attr(not(target_arch = "wasm32"), ::async_trait::async_trait)]
324+
#[cfg_attr(target_arch = "wasm32", ::async_trait::async_trait(?Send))]
325+
}
326+
}
327+
#[cfg(not(feature = "wasm-unstable-single-threaded"))]
328+
{
329+
quote! {
330+
#[::async_trait::async_trait]
331+
}
332+
}
333+
}

0 commit comments

Comments
 (0)