Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/kotlin/templates/Async.kt

raw
1 // Async return type handlers
2
3 internal const val UNIFFI_RUST_FUTURE_POLL_READY = 0.toByte()
4 internal const val UNIFFI_RUST_FUTURE_POLL_MAYBE_READY = 1.toByte()
5
6 internal val uniffiContinuationHandleMap = UniffiHandleMap<CancellableContinuation<Byte>>()
7
8 // FFI type for Rust future continuations
9 internal object uniffiRustFutureContinuationCallbackImpl: UniffiRustFutureContinuationCallback {
10 override fun callback(data: Long, pollResult: Byte) {
11 uniffiContinuationHandleMap.remove(data).resume(pollResult)
12 }
13 }
14
15 internal suspend fun<T, F, E: kotlin.Exception> uniffiRustCallAsync(
16 rustFuture: Long,
17 pollFunc: (Long, UniffiRustFutureContinuationCallback, Long) -> Unit,
18 completeFunc: (Long, UniffiRustCallStatus) -> F,
19 freeFunc: (Long) -> Unit,
20 liftFunc: (F) -> T,
21 errorHandler: UniffiRustCallStatusErrorHandler<E>
22 ): T {
23 try {
24 do {
25 val pollResult = suspendCancellableCoroutine<Byte> { continuation ->
26 pollFunc(
27 rustFuture,
28 uniffiRustFutureContinuationCallbackImpl,
29 uniffiContinuationHandleMap.insert(continuation)
30 )
31 }
32 } while (pollResult != UNIFFI_RUST_FUTURE_POLL_READY);
33
34 return liftFunc(
35 uniffiRustCallWithError(errorHandler, { status -> completeFunc(rustFuture, status) })
36 )
37 } finally {
38 freeFunc(rustFuture)
39 }
40 }
41
42 {%- if ci.has_async_callback_interface_definition() %}
43 internal inline fun<T> uniffiTraitInterfaceCallAsync(
44 crossinline makeCall: suspend () -> T,
45 crossinline handleSuccess: (T) -> Unit,
46 crossinline handleError: (UniffiRustCallStatus.ByValue) -> Unit,
47 ): UniffiForeignFuture {
48 // Using `GlobalScope` is labeled as a "delicate API" and generally discouraged in Kotlin programs, since it breaks structured concurrency.
49 // However, our parent task is a Rust future, so we're going to need to break structure concurrency in any case.
50 //
51 // Uniffi does its best to support structured concurrency across the FFI.
52 // If the Rust future is dropped, `uniffiForeignFutureFreeImpl` is called, which will cancel the Kotlin coroutine if it's still running.
53 @OptIn(DelicateCoroutinesApi::class)
54 val job = GlobalScope.launch {
55 try {
56 handleSuccess(makeCall())
57 } catch(e: Exception) {
58 handleError(
59 UniffiRustCallStatus.create(
60 UNIFFI_CALL_UNEXPECTED_ERROR,
61 {{ Type::String.borrow()|lower_fn }}(e.toString()),
62 )
63 )
64 }
65 }
66 val handle = uniffiForeignFutureHandleMap.insert(job)
67 return UniffiForeignFuture(handle, uniffiForeignFutureFreeImpl)
68 }
69
70 internal inline fun<T, reified E: Throwable> uniffiTraitInterfaceCallAsyncWithError(
71 crossinline makeCall: suspend () -> T,
72 crossinline handleSuccess: (T) -> Unit,
73 crossinline handleError: (UniffiRustCallStatus.ByValue) -> Unit,
74 crossinline lowerError: (E) -> RustBuffer.ByValue,
75 ): UniffiForeignFuture {
76 // See uniffiTraitInterfaceCallAsync for details on `DelicateCoroutinesApi`
77 @OptIn(DelicateCoroutinesApi::class)
78 val job = GlobalScope.launch {
79 try {
80 handleSuccess(makeCall())
81 } catch(e: Exception) {
82 if (e is E) {
83 handleError(
84 UniffiRustCallStatus.create(
85 UNIFFI_CALL_ERROR,
86 lowerError(e),
87 )
88 )
89 } else {
90 handleError(
91 UniffiRustCallStatus.create(
92 UNIFFI_CALL_UNEXPECTED_ERROR,
93 {{ Type::String.borrow()|lower_fn }}(e.toString()),
94 )
95 )
96 }
97 }
98 }
99 val handle = uniffiForeignFutureHandleMap.insert(job)
100 return UniffiForeignFuture(handle, uniffiForeignFutureFreeImpl)
101 }
102
103 internal val uniffiForeignFutureHandleMap = UniffiHandleMap<Job>()
104
105 internal object uniffiForeignFutureFreeImpl: UniffiForeignFutureFree {
106 override fun callback(handle: Long) {
107 val job = uniffiForeignFutureHandleMap.remove(handle)
108 if (!job.isCompleted) {
109 job.cancel()
110 }
111 }
112 }
113
114 // For testing
115 public fun uniffiForeignFutureHandleCount() = uniffiForeignFutureHandleMap.size
116
117 {%- endif %}