Skip to content

Commit ba621e8

Browse files
committed
Address reviewer comments
1 parent 156fe6c commit ba621e8

File tree

2 files changed

+60
-15
lines changed

2 files changed

+60
-15
lines changed

uniffi_core/src/ffi/rustfuture/future.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ use std::{
8484
task::{Context, Poll, Wake},
8585
};
8686

87-
use super::{FutureResult, RustFutureContinuationCallback, RustFuturePoll, Scheduler};
87+
use super::{RustFutureContinuationCallback, RustFuturePoll, Scheduler, UniffiCompatibleFuture};
8888
use crate::{rust_call_with_out_status, FfiDefault, LiftArgsError, LowerReturn, RustCallStatus};
8989

9090
/// Wraps the actual future we're polling
9191
struct WrappedFuture<F, T, UT>
9292
where
9393
// See rust_future_new for an explanation of these trait bounds
94-
F: FutureResult<T, LiftArgsError>,
94+
F: UniffiCompatibleFuture<Result<T, LiftArgsError>> + 'static,
9595
T: LowerReturn<UT> + Send + 'static,
9696
UT: Send + 'static,
9797
{
@@ -105,7 +105,7 @@ where
105105
impl<F, T, UT> WrappedFuture<F, T, UT>
106106
where
107107
// See rust_future_new for an explanation of these trait bounds
108-
F: FutureResult<T, LiftArgsError>,
108+
F: UniffiCompatibleFuture<Result<T, LiftArgsError>> + 'static,
109109
T: LowerReturn<UT> + Send + 'static,
110110
UT: Send + 'static,
111111
{
@@ -185,7 +185,7 @@ where
185185
unsafe impl<F, T, UT> Send for WrappedFuture<F, T, UT>
186186
where
187187
// See rust_future_new for an explanation of these trait bounds
188-
F: FutureResult<T, LiftArgsError>,
188+
F: UniffiCompatibleFuture<Result<T, LiftArgsError>> + 'static,
189189
T: LowerReturn<UT> + Send + 'static,
190190
UT: Send + 'static,
191191
{
@@ -195,7 +195,7 @@ where
195195
pub(super) struct RustFuture<F, T, UT>
196196
where
197197
// See rust_future_new for an explanation of these trait bounds
198-
F: FutureResult<T, LiftArgsError>,
198+
F: UniffiCompatibleFuture<Result<T, LiftArgsError>> + 'static,
199199
T: LowerReturn<UT> + Send + 'static,
200200
UT: Send + 'static,
201201
{
@@ -211,7 +211,7 @@ where
211211
impl<F, T, UT> RustFuture<F, T, UT>
212212
where
213213
// See rust_future_new for an explanation of these trait bounds
214-
F: FutureResult<T, LiftArgsError>,
214+
F: UniffiCompatibleFuture<Result<T, LiftArgsError>> + 'static,
215215
T: LowerReturn<UT> + Send + 'static,
216216
UT: Send + 'static,
217217
{
@@ -266,7 +266,7 @@ where
266266
impl<F, T, UT> Wake for RustFuture<F, T, UT>
267267
where
268268
// See rust_future_new for an explanation of these trait bounds
269-
F: FutureResult<T, LiftArgsError>,
269+
F: UniffiCompatibleFuture<Result<T, LiftArgsError>> + 'static,
270270
T: LowerReturn<UT> + Send + 'static,
271271
UT: Send + 'static,
272272
{
@@ -301,7 +301,7 @@ pub trait RustFutureFfi<ReturnType>: Send + Sync {
301301
impl<F, T, UT> RustFutureFfi<T::ReturnType> for RustFuture<F, T, UT>
302302
where
303303
// See rust_future_new for an explanation of these trait bounds
304-
F: FutureResult<T, LiftArgsError>,
304+
F: UniffiCompatibleFuture<Result<T, LiftArgsError>> + 'static,
305305
T: LowerReturn<UT> + Send + 'static,
306306
UT: Send + 'static,
307307
{

uniffi_core/src/ffi/rustfuture/mod.rs

+52-7
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,61 @@ pub enum RustFuturePoll {
3030
/// to continue progress on the future.
3131
pub type RustFutureContinuationCallback = extern "C" fn(callback_data: u64, RustFuturePoll);
3232

33-
/// This marker trait allows us to put different bounds on the `Future`s we support, based on
34-
/// `#[cfg()]` configuration.
35-
pub trait FutureResult<T, E>: Future<Output = Result<T, E>> + 'static {}
33+
/// This marker trait allows us to put different bounds on the `Future`s we
34+
/// support, based on `#[cfg(..)]` configuration.
35+
///
36+
/// It should not be considered as a part of the public API, and as such as
37+
/// an implementation detail and subject to change.
38+
///
39+
/// It is _not_ intended to be implemented by libray users or bindings
40+
/// implementors.
41+
#[doc(hidden)]
42+
pub trait UniffiCompatibleFuture<T>: Future<Output = T> {}
3643

44+
/// The `Send` bound is required because the Foreign code may call the
45+
/// `rust_future_*` methods from different threads.
3746
#[cfg(not(target_arch = "wasm32"))]
38-
impl<T, F, E> FutureResult<T, E> for F where F: Future<Output = Result<T, E>> + Send + 'static {}
47+
impl<T, F> UniffiCompatibleFuture<T> for F where F: Future<Output = T> + Send {}
3948

40-
// `Promise`s cannot be sent to or from WebWorkers, but `JsValue` is not `Send`.
49+
/// `Future`'s on WASM32 are not `Send` because it's a single threaded environment.
50+
///
51+
/// # Safety:
52+
///
53+
/// WASM32 is a single threaded environment. However, in a browser there do
54+
/// exist [`WebWorker`][webworker]s which do not share memory or event-loop
55+
/// with the main browser context.
56+
///
57+
/// Communication between contexts is only possible by message passing,
58+
/// using a small number of ['transferable' object types][transferable].
59+
///
60+
/// The most common source of asynchrony in Rust compiled to WASM is
61+
/// [wasm-bindgen's `JsFuture`][jsfuture]. It is not `Send` because:
62+
///
63+
/// 1. `T` and `E` are both `JsValue`
64+
/// 2. `JsValue` may contain `JsFunction`s, either as a function themselves or
65+
/// an object containing functions.
66+
/// 3. Functions cannot be [serialized and sent][transferable] to `WebWorker`s.
67+
///
68+
/// Implementors of binding generators should be able to enumerate the
69+
/// combinations of Rust or JS communicating across different contexts (here
70+
/// using: <->), and in the same context (+) to account for why it is safe
71+
/// for UniFFI to support `Future`s that are not `Send`:
72+
///
73+
/// 1. JS + Rust in the same contexts: polling and waking happens in the same
74+
/// thread, no `Send` is needed.
75+
/// 2. Rust <-> Rust in different contexts: Futures cannot be sent between JS
76+
/// contexts within the same Rust crate (because they are not `Send`).
77+
/// 3. JS <-> Rust in different contexts: the `Promise` are [not transferable
78+
/// between contexts][transferable], so this is impossible.
79+
/// 4. JS <-> JS + Rust, this is possible, but safe since the Future is being
80+
/// driven by JS in the same thread. If a Promise metaphor is desired, then
81+
/// this must be built with JS talking to JS, because 3.
82+
///
83+
/// [webworker]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
84+
/// [jsfuture]: https://github.com/rustwasm/wasm-bindgen/blob/main/crates/futures/src/lib.rs
85+
/// [transferable]: (https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Transferable_objects
4186
#[cfg(target_arch = "wasm32")]
42-
impl<T, F, E> FutureResult<T, E> for F where F: Future<Output = Result<T, E>> + 'static {}
87+
impl<T, F> UniffiCompatibleFuture<T> for F where F: Future<Output = T> {}
4388

4489
// === Public FFI API ===
4590

@@ -55,7 +100,7 @@ where
55100
// since it will move between threads for an indeterminate amount of time as the foreign
56101
// executor calls polls it and the Rust executor wakes it. It does not need to by `Sync`,
57102
// since we synchronize all access to the values.
58-
F: FutureResult<T, LiftArgsError>,
103+
F: UniffiCompatibleFuture<Result<T, LiftArgsError>> + 'static,
59104
// T is the output of the Future. It needs to implement [LowerReturn]. Also it must be Send +
60105
// 'static for the same reason as F.
61106
T: LowerReturn<UT> + Send + 'static,

0 commit comments

Comments
 (0)