Backed out 5 changesets (bug 1890092, bug 1888683) for causing build bustages & crash...
[gecko.git] / third_party / rust / uniffi_bindgen / src / bindings / python / templates / Async.py
blob82aa534b46ecb6949824d43da36fb8704b3a73d1
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):
20 try:
21 eventloop = asyncio.get_running_loop()
23 # Loop and poll until we see a _UNIFFI_RUST_FUTURE_POLL_READY value
24 while True:
25 future = eventloop.create_future()
26 ffi_poll(
27 rust_future,
28 _UniffiContinuationPointerManager.new_pointer((eventloop, future)),
30 poll_code = await future
31 if poll_code == _UNIFFI_RUST_FUTURE_POLL_READY:
32 break
34 return lift_func(
35 _rust_call_with_error(error_ffi_converter, ffi_complete, rust_future)
37 finally:
38 ffi_free(rust_future)
40 _UniffiLib.{{ ci.ffi_rust_future_continuation_callback_set().name() }}(_uniffi_continuation_callback)