Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle async-traits with external types #2423

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fixtures/ext-types/proc-macro-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ name = "uniffi_ext_types_proc_macro_lib"

[dependencies]
anyhow = "1"
async-trait = "0.1"
bytes = "1.3"
uniffi = { workspace = true }

Expand Down
35 changes: 34 additions & 1 deletion fixtures/ext-types/proc-macro-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use custom_types::Handle;
use ext_types_custom::{Guid, Ouid2};
use std::sync::Arc;
use uniffi_one::{
UniffiOneEnum, UniffiOneInterface, UniffiOneProcMacroType, UniffiOneTrait, UniffiOneType,
UniffiOneAsyncTrait, UniffiOneEnum, UniffiOneInterface, UniffiOneProcMacroType, UniffiOneTrait,
UniffiOneTraitError, UniffiOneType,
};
use url::Url;

Expand Down Expand Up @@ -157,6 +158,38 @@ fn get_uniffi_one_trait(t: Option<Arc<dyn UniffiOneTrait>>) -> Option<Arc<dyn Un
t
}

#[derive(uniffi::Object)]
pub struct UniffiOneTraitImpl;

#[uniffi::export]
impl UniffiOneTrait for UniffiOneTraitImpl {
fn hello(&self) -> String {
"uniffi-one-trait-impl".to_string()
}
}

#[derive(uniffi::Object)]
pub struct UniffiOneAsyncTraitImpl;

#[uniffi::export]
#[async_trait::async_trait]
impl UniffiOneAsyncTrait for UniffiOneAsyncTraitImpl {
async fn hello(&self) -> String {
"uniffi-one-trait-impl".to_string()
}

async fn try_hello(&self) -> Result<String, UniffiOneTraitError> {
Ok("uniffi-one-trait-impl".to_string())
}
}

#[uniffi::export]
fn get_uniffi_one_async_trait(
t: Option<Arc<dyn UniffiOneAsyncTrait>>,
) -> Option<Arc<dyn UniffiOneAsyncTrait>> {
t
}

// Some custom types via macros.
// Another guid - here we use a regular struct.
pub struct Uuid {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ async def test():
self.assertEqual(await get_uniffi_one_async(), UniffiOneEnum.ONE)
# This async function comes from the `proc-macro-lib` crate
self.assertEqual(t1, await get_uniffi_one_type_async(t1))

t2 = UniffiOneTraitImpl()
self.assertEqual(await t2.hello(), "uniffi-one-trait-impl")
self.assertEqual(await t2.try_hello(), "uniffi-one-trait-impl")

asyncio.run(test())

def test_get_uniffi_one_proc_macro_type(self):
Expand Down
1 change: 1 addition & 0 deletions fixtures/ext-types/uniffi-one/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ name = "uniffi_one"

[dependencies]
anyhow = "1"
async-trait = "0.1"
bytes = "1.3"
thiserror = "1"
uniffi = { workspace = true }
Expand Down
19 changes: 19 additions & 0 deletions fixtures/ext-types/uniffi-one/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ pub trait UniffiOneTrait: Send + Sync {
fn hello(&self) -> String;
}

#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum UniffiOneTraitError {
#[error("Callback failed")]
Error,
}

#[uniffi::export(with_foreign)]
#[async_trait::async_trait]
pub trait UniffiOneAsyncTrait: Send + Sync {
async fn hello(&self) -> String;
async fn try_hello(&self) -> Result<String, UniffiOneTraitError>;
}

// We *must* have this so support is generated. We should fix that and remove this. See #2423.
#[uniffi::export]
fn _just_to_get_trait_support() -> std::sync::Arc<dyn UniffiOneAsyncTrait> {
panic!()
}

// A couple of errors used as external types.
#[derive(thiserror::Error, uniffi::Error, Debug)]
pub enum UniffiOneError {
Expand Down