Skip to content

Commit

Permalink
Merge pull request #1532 from ijc/async-with-optional-object-returned
Browse files Browse the repository at this point in the history
Support returning an `Option<Object>` from an async function
  • Loading branch information
bendk authored May 10, 2023
2 parents 75b9df4 + 11cd947 commit a93713a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
10 changes: 10 additions & 0 deletions fixtures/futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ pub async fn async_new_megaphone() -> Arc<Megaphone> {
new_megaphone()
}

/// Async function that possibly generates a new `Megaphone`.
#[uniffi::export]
pub async fn async_maybe_new_megaphone(y: bool) -> Option<Arc<Megaphone>> {
if y {
Some(new_megaphone())
} else {
None
}
}

/// A megaphone. Be careful with the neighbours.
#[derive(uniffi::Object)]
pub struct Megaphone;
Expand Down
9 changes: 9 additions & 0 deletions fixtures/futures/tests/bindings/test_futures.kts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ runBlocking {
println(" ... ok")
}

// Test async method returning optional object
runBlocking {
val megaphone = asyncMaybeNewMegaphone(true)
assert(megaphone != null)

val not_megaphone = asyncMaybeNewMegaphone(false)
assert(not_megaphone == null)
}

// Test with the Tokio runtime.
runBlocking {
val time = measureTimeMillis {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal interface _UniFFILib : Library {
}

{%- if ci.has_async_fns() %}
internal val FUTURE_WAKER_ENVIRONMENTS: ConcurrentHashMap<Int, RustFutureWakerEnvironment<out Any>> by lazy {
internal val FUTURE_WAKER_ENVIRONMENTS: ConcurrentHashMap<Int, RustFutureWakerEnvironment<out Any?>> by lazy {
ConcurrentHashMap(8)
}
{%- endif %}
Expand Down

0 comments on commit a93713a

Please sign in to comment.