Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/swift/templates/Async.swift

raw
1 private let UNIFFI_RUST_FUTURE_POLL_READY: Int8 = 0
2 private let UNIFFI_RUST_FUTURE_POLL_MAYBE_READY: Int8 = 1
3
4 fileprivate let uniffiContinuationHandleMap = UniffiHandleMap<UnsafeContinuation<Int8, Never>>()
5
6 fileprivate func uniffiRustCallAsync<F, T>(
7 rustFutureFunc: () -> UInt64,
8 pollFunc: (UInt64, @escaping UniffiRustFutureContinuationCallback, UInt64) -> (),
9 completeFunc: (UInt64, UnsafeMutablePointer<RustCallStatus>) -> F,
10 freeFunc: (UInt64) -> (),
11 liftFunc: (F) throws -> T,
12 errorHandler: ((RustBuffer) throws -> Swift.Error)?
13 ) async throws -> T {
14 // Make sure to call the ensure init function since future creation doesn't have a
15 // RustCallStatus param, so doesn't use makeRustCall()
16 {{ ensure_init_fn_name }}()
17 let rustFuture = rustFutureFunc()
18 defer {
19 freeFunc(rustFuture)
20 }
21 var pollResult: Int8;
22 repeat {
23 pollResult = await withUnsafeContinuation {
24 pollFunc(
25 rustFuture,
26 uniffiFutureContinuationCallback,
27 uniffiContinuationHandleMap.insert(obj: $0)
28 )
29 }
30 } while pollResult != UNIFFI_RUST_FUTURE_POLL_READY
31
32 return try liftFunc(makeRustCall(
33 { completeFunc(rustFuture, $0) },
34 errorHandler: errorHandler
35 ))
36 }
37
38 // Callback handlers for an async calls. These are invoked by Rust when the future is ready. They
39 // lift the return value or error and resume the suspended function.
40 fileprivate func uniffiFutureContinuationCallback(handle: UInt64, pollResult: Int8) {
41 if let continuation = try? uniffiContinuationHandleMap.remove(handle: handle) {
42 continuation.resume(returning: pollResult)
43 } else {
44 print("uniffiFutureContinuationCallback invalid handle")
45 }
46 }
47
48 {%- if ci.has_async_callback_interface_definition() %}
49 private func uniffiTraitInterfaceCallAsync<T>(
50 makeCall: @escaping () async throws -> T,
51 handleSuccess: @escaping (T) -> (),
52 handleError: @escaping (Int8, RustBuffer) -> ()
53 ) -> UniffiForeignFuture {
54 let task = Task {
55 do {
56 handleSuccess(try await makeCall())
57 } catch {
58 handleError(CALL_UNEXPECTED_ERROR, {{ Type::String.borrow()|lower_fn }}(String(describing: error)))
59 }
60 }
61 let handle = UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.insert(obj: task)
62 return UniffiForeignFuture(handle: handle, free: uniffiForeignFutureFree)
63
64 }
65
66 private func uniffiTraitInterfaceCallAsyncWithError<T, E>(
67 makeCall: @escaping () async throws -> T,
68 handleSuccess: @escaping (T) -> (),
69 handleError: @escaping (Int8, RustBuffer) -> (),
70 lowerError: @escaping (E) -> RustBuffer
71 ) -> UniffiForeignFuture {
72 let task = Task {
73 do {
74 handleSuccess(try await makeCall())
75 } catch let error as E {
76 handleError(CALL_ERROR, lowerError(error))
77 } catch {
78 handleError(CALL_UNEXPECTED_ERROR, {{ Type::String.borrow()|lower_fn }}(String(describing: error)))
79 }
80 }
81 let handle = UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.insert(obj: task)
82 return UniffiForeignFuture(handle: handle, free: uniffiForeignFutureFree)
83 }
84
85 // Borrow the callback handle map implementation to store foreign future handles
86 // TODO: consolidate the handle-map code (https://github.com/mozilla/uniffi-rs/pull/1823)
87 fileprivate let UNIFFI_FOREIGN_FUTURE_HANDLE_MAP = UniffiHandleMap<UniffiForeignFutureTask>()
88
89 // Protocol for tasks that handle foreign futures.
90 //
91 // Defining a protocol allows all tasks to be stored in the same handle map. This can't be done
92 // with the task object itself, since has generic parameters.
93 fileprivate protocol UniffiForeignFutureTask {
94 func cancel()
95 }
96
97 extension Task: UniffiForeignFutureTask {}
98
99 private func uniffiForeignFutureFree(handle: UInt64) {
100 do {
101 let task = try UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.remove(handle: handle)
102 // Set the cancellation flag on the task. If it's still running, the code can check the
103 // cancellation flag or call `Task.checkCancellation()`. If the task has completed, this is
104 // a no-op.
105 task.cancel()
106 } catch {
107 print("uniffiForeignFutureFree: handle missing from handlemap")
108 }
109 }
110
111 // For testing
112 public func uniffiForeignFutureHandleCount{{ ci.namespace()|class_name }}() -> Int {
113 UNIFFI_FOREIGN_FUTURE_HANDLE_MAP.count
114 }
115
116 {%- endif %}