1 # RustFuturePoll values
2 _UNIFFI_RUST_FUTURE_POLL_READY
= 0
3 _UNIFFI_RUST_FUTURE_POLL_MAYBE_READY
= 1
5 # Stores futures for _uniffi_continuation_callback
6 _UniffiContinuationPointerManager
= _UniffiPointerManager()
8 # Continuation callback for async functions
9 # lift the return value or error and resolve the future, causing the async function to resume.
10 @_UNIFFI_FUTURE_CONTINUATION_T
11 def _uniffi_continuation_callback(future_ptr
, poll_code
):
12 (eventloop
, future
) = _UniffiContinuationPointerManager
.release_pointer(future_ptr
)
13 eventloop
.call_soon_threadsafe(_uniffi_set_future_result
, future
, poll_code
)
15 def _uniffi_set_future_result(future
, poll_code
):
16 if not future
.cancelled():
17 future
.set_result(poll_code
)
19 async def _uniffi_rust_call_async(rust_future
, ffi_poll
, ffi_complete
, ffi_free
, lift_func
, error_ffi_converter
):
21 eventloop
= asyncio
.get_running_loop()
23 # Loop and poll until we see a _UNIFFI_RUST_FUTURE_POLL_READY value
25 future
= eventloop
.create_future()
28 _UniffiContinuationPointerManager
.new_pointer((eventloop
, future
)),
30 poll_code
= await future
31 if poll_code
== _UNIFFI_RUST_FUTURE_POLL_READY
:
35 _rust_call_with_error(error_ffi_converter
, ffi_complete
, rust_future
)
40 _UniffiLib
.{{ ci
.ffi_rust_future_continuation_callback_set().name() }}(_uniffi_continuation_callback
)