From 221f71013e1a3c1c698fefd52aa81beb11fd2a39 Mon Sep 17 00:00:00 2001
From: "uniffi-docs[bot]" Rust async functions are implemplemented by wrapping the
Future into a For each async function, UniFFI generates 4 scaffolding functions:
* A scaffolding function that returns a Async callback/trait interface methods are the way that Rust makes async calls across the FFI.
These are implemented using a callback, which makes them quite a bit simpler than their counterparts. The UniFFI generated code for an async trait method performs these steps:Rust Async functions
uniffi::RustFuture
struct
and providing scaffolding functions so that the foreign bindings can drive the future to completion.sequenceDiagram
+
sequenceDiagram
participant User as User code
participant Foreign as Foreign Language Bindings
participant Rust as Rust Scaffolding
@@ -1902,8 +1902,7 @@
Rust Async functions
Foreign->>+Rust: call RustFuture complete fn
Rust-->>-Foreign: return result
Foreign->>Rust: call RustFuture free fn
- Foreign-->>-User: return from async function
-RustFuture
handle
* rust_future_poll
to poll the future
@@ -1937,7 +1936,7 @@ Why not have
rust_
Foreign async callback methods
sequenceDiagram
+
sequenceDiagram
participant User as User code
participant Rust as Rust Scaffolding
participant Foreign as Foreign Language Bindings
@@ -1947,8 +1946,7 @@
Foreign async callback methods
Rust-->>-User: return oneshot::Receiver
Foreign->>Rust: invoke callback, pass back oneshot::Sender pointer
- Note over Rust: send callback result to the oneshot::Sender
-
oneshot::Channel
(see oneshot crate for how these work, although we don't currently depend on that crate).Can you real
Furthermore, let's stipulate that the deserialization/validation is slow enough to be considered blocking.
To manage this, the Rust library also depends on a (synchronous) callback interface that runs a task in a worker queue where blocking is allowed.
Here's how the async API call could be handled:
sequenceDiagram
+sequenceDiagram
participant AsyncRust as Async Rust code
participant SyncRust as Sync Rust code
participant WorkerQueue as Work Queue Callback Interface
@@ -1995,8 +1993,7 @@ Can you real
Note over AsyncRust: await oneshot channel
WorkerQueue->>SyncRust: Run task (in background thread)
SyncRust-->>AsyncRust: Send deserialized response via oneshot channel
- AsyncRust-->>-Foreign: Return API result
-