-
Notifications
You must be signed in to change notification settings - Fork 14
Add reproduction for uniffi arc objects #241
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
Draft
zzorba
wants to merge
4
commits into
main
Choose a base branch
from
arc_object_reproduction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "uniffi-fixtures-wasm-arc-futures" | ||
version = "0.21.0" | ||
authors = ["zzorba"] | ||
edition = "2021" | ||
license = "MPL-2.0" | ||
publish = false | ||
|
||
[lib] | ||
name = "wasm_arc_futures" | ||
crate-type = ["lib", "cdylib"] | ||
|
||
[dependencies] | ||
uniffi = { workspace = true, features = ["cli", "wasm-unstable-single-threaded"] } | ||
async-trait = "0.1" | ||
ubrn_testing = { path = "../../crates/ubrn_testing" } | ||
|
||
[build-dependencies] | ||
uniffi = { workspace = true, features = ["build", "wasm-unstable-single-threaded"] } | ||
|
||
[dev-dependencies] | ||
uniffi = { workspace = true, features = ["bindgen-tests", "wasm-unstable-single-threaded"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# A basic test for uniffi components | ||
|
||
This test covers async functions and methods. It also provides examples. | ||
|
||
## Run the tests | ||
|
||
Simply use `cargo`: | ||
|
||
```sh | ||
$ cargo test | ||
``` | ||
|
||
It is possible to filter by test names, like `cargo test -- swift` to only run | ||
Swift's tests. | ||
|
||
## Run the examples | ||
|
||
At the time of writing, each `examples/*` directory has a `Makefile`. They are | ||
mostly designed for Unix-ish systems, sorry for that. | ||
|
||
To run the examples, first `uniffi` must be compiled: | ||
|
||
```sh | ||
$ cargo build --release -p uniffi` | ||
``` | ||
|
||
Then, each `Makefile` has 2 targets: `build` and `run`: | ||
|
||
```sh | ||
$ # Build the examples. | ||
$ make build | ||
$ | ||
$ # Run the example. | ||
$ make run | ||
``` | ||
|
||
One note for `examples/kotlin/`, some JAR files must be present, so please | ||
run `make install-jar` first: It will just download the appropriated JAR files | ||
directly inside the directory from Maven. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
*/ | ||
|
||
use std::fmt; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::sync::{Arc, Mutex}; | ||
use std::time::Duration; | ||
|
||
use ubrn_testing::timer::{TimerFuture, TimerService}; | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
type EventHandlerFut = Pin<Box<dyn Future<Output = ()> + Send>>; | ||
#[cfg(target_arch = "wasm32")] | ||
type EventHandlerFut = Pin<Box<dyn Future<Output = ()>>>; | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
type EventHandlerFn = dyn Fn(String, String) -> EventHandlerFut + Send + Sync; | ||
#[cfg(target_arch = "wasm32")] | ||
type EventHandlerFn = dyn Fn(String, String) -> EventHandlerFut; | ||
|
||
#[derive(uniffi::Object)] | ||
pub struct SimpleObject { | ||
inner: Mutex<String>, | ||
callbacks: Vec<Box<EventHandlerFn>>, | ||
} | ||
|
||
impl fmt::Debug for SimpleObject { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "SimpleObject") | ||
} | ||
} | ||
|
||
impl fmt::Display for SimpleObject { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{:?}", self) | ||
} | ||
} | ||
|
||
impl SimpleObject { | ||
#[cfg_attr(target_arch = "wasm32", allow(clippy::arc_with_non_send_sync))] | ||
fn new_with_callback(cb: Box<EventHandlerFn>) -> Arc<Self> { | ||
Arc::new(SimpleObject { | ||
inner: Mutex::new("key".to_string()), | ||
callbacks: vec![cb], | ||
}) | ||
} | ||
} | ||
|
||
#[uniffi::export] | ||
impl SimpleObject { | ||
pub async fn update(self: Arc<Self>, updated: String) { | ||
let old = { | ||
let mut data = self.inner.lock().unwrap(); | ||
let old = data.clone(); | ||
*data = updated.clone(); | ||
old | ||
}; | ||
for callback in self.callbacks.iter() { | ||
callback(old.clone(), updated.clone()).await; | ||
} | ||
} | ||
} | ||
|
||
pub async fn wait(_old: String, _new: String) { | ||
TimerFuture::sleep(Duration::from_millis(200)).await; | ||
} | ||
|
||
fn from_static() -> Box<EventHandlerFn> { | ||
Box::new(|old, new| Box::pin(wait(old, new))) | ||
} | ||
|
||
// Make an object, with no callbacks. | ||
// This relies on a timer, which is implemented for wasm using gloo. | ||
// This is not Send, so EventHandlerFn and EventHandlerFut are different | ||
// for wasm. | ||
#[uniffi::export] | ||
async fn make_object() -> Arc<SimpleObject> { | ||
SimpleObject::new_with_callback(from_static()) | ||
} | ||
|
||
#[uniffi::export] | ||
async fn throw_object() -> Result<(), Arc<SimpleObject>> { | ||
let obj = make_object().await; | ||
Err(obj) | ||
} | ||
|
||
// Simple callback interface object, with a synchronous method. | ||
// The foreign trait isn't asynchronous, so we shouldn't be seeing | ||
// any problem here. | ||
#[uniffi::export(with_foreign)] | ||
pub trait SimpleCallback: Sync + Send { | ||
fn on_update(&self, previous: String, current: String); | ||
} | ||
|
||
#[uniffi::export] | ||
async fn simple_callback(callback: Arc<dyn SimpleCallback>) -> Arc<dyn SimpleCallback> { | ||
callback | ||
} | ||
|
||
fn from_simple_callback(callback: Arc<dyn SimpleCallback>) -> Box<EventHandlerFn> { | ||
Box::new(move |old: String, new: String| { | ||
let callback = callback.clone(); | ||
Box::pin(async move { | ||
callback.on_update(old, new); | ||
}) | ||
}) | ||
} | ||
|
||
#[uniffi::export] | ||
async fn make_object_with_callback(callback: Arc<dyn SimpleCallback>) -> Arc<SimpleObject> { | ||
SimpleObject::new_with_callback(from_simple_callback(callback)) | ||
} | ||
|
||
// An async callback interface; the async foreign trait will be | ||
// a Send and Sync, so this shouldn't be testing anything new. | ||
#[cfg(target_arch = "wasm32")] | ||
#[uniffi::export(with_foreign)] | ||
#[async_trait::async_trait(?Send)] | ||
pub trait AsyncCallback { | ||
async fn on_update(&self, previous: String, current: String); | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
#[uniffi::export(with_foreign)] | ||
#[async_trait::async_trait] | ||
pub trait AsyncCallback: Send + Sync { | ||
async fn on_update(&self, previous: String, current: String); | ||
} | ||
|
||
#[uniffi::export] | ||
async fn async_callback(callback: Arc<dyn AsyncCallback>) -> Arc<dyn AsyncCallback> { | ||
callback | ||
} | ||
|
||
fn from_async_callback(callback: Arc<dyn AsyncCallback>) -> Box<EventHandlerFn> { | ||
Box::new(move |old: String, new: String| { | ||
let callback = callback.clone(); | ||
Box::pin(async move { | ||
// Look, there's an .await here. | ||
callback.on_update(old, new).await; | ||
}) | ||
}) | ||
} | ||
|
||
#[uniffi::export] | ||
async fn make_object_with_async_callback(callback: Arc<dyn AsyncCallback>) -> Arc<SimpleObject> { | ||
SimpleObject::new_with_callback(from_async_callback(callback)) | ||
} | ||
|
||
// Rust only trait | ||
#[cfg(not(target_arch = "wasm32"))] | ||
#[uniffi::export(with_foreign)] | ||
#[async_trait::async_trait] | ||
pub trait RustCallback: Sync + Send { | ||
async fn on_update(&self, previous: String, current: String) -> String; | ||
} | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
#[uniffi::export(with_foreign)] | ||
#[async_trait::async_trait(?Send)] | ||
pub trait RustCallback { | ||
async fn on_update(&self, previous: String, current: String) -> String; | ||
} | ||
|
||
struct NoopRustCallback; | ||
|
||
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] | ||
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] | ||
impl RustCallback for NoopRustCallback { | ||
async fn on_update(&self, previous: String, current: String) -> String { | ||
use std::time::Duration; | ||
use ubrn_testing::timer::{TimerFuture, TimerService}; | ||
TimerFuture::sleep(Duration::from_millis(200)).await; | ||
format!("{previous} -> {current}") | ||
} | ||
} | ||
|
||
#[uniffi::export] | ||
async fn rust_callback() -> Arc<dyn RustCallback> { | ||
Arc::new(NoopRustCallback) | ||
} | ||
|
||
uniffi::setup_scaffolding!(); |
2 changes: 2 additions & 0 deletions
2
fixtures/wasm-arc-futures/tests/bindings/.supported-flavors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
jsi | ||
wasm |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There appears to be a stray backtick at the end of the cargo build command, which could cause confusion. Consider removing the extra backtick for clarity.
Copilot uses AI. Check for mistakes.