Debian Codegraph @

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

raw
1 // An error type for FFI errors. These errors occur at the UniFFI level, not
2 // the library level.
3 fileprivate enum UniffiInternalError: LocalizedError {
4 case bufferOverflow
5 case incompleteData
6 case unexpectedOptionalTag
7 case unexpectedEnumCase
8 case unexpectedNullPointer
9 case unexpectedRustCallStatusCode
10 case unexpectedRustCallError
11 case unexpectedStaleHandle
12 case rustPanic(_ message: String)
13
14 public var errorDescription: String? {
15 switch self {
16 case .bufferOverflow: return "Reading the requested value would read past the end of the buffer"
17 case .incompleteData: return "The buffer still has data after lifting its containing value"
18 case .unexpectedOptionalTag: return "Unexpected optional tag; should be 0 or 1"
19 case .unexpectedEnumCase: return "Raw enum value doesn't match any cases"
20 case .unexpectedNullPointer: return "Raw pointer value was null"
21 case .unexpectedRustCallStatusCode: return "Unexpected RustCallStatus code"
22 case .unexpectedRustCallError: return "CALL_ERROR but no errorClass specified"
23 case .unexpectedStaleHandle: return "The object in the handle map has been dropped already"
24 case let .rustPanic(message): return message
25 }
26 }
27 }
28
29 fileprivate extension NSLock {
30 func withLock<T>(f: () throws -> T) rethrows -> T {
31 self.lock()
32 defer { self.unlock() }
33 return try f()
34 }
35 }
36
37 fileprivate let CALL_SUCCESS: Int8 = 0
38 fileprivate let CALL_ERROR: Int8 = 1
39 fileprivate let CALL_UNEXPECTED_ERROR: Int8 = 2
40 fileprivate let CALL_CANCELLED: Int8 = 3
41
42 fileprivate extension RustCallStatus {
43 init() {
44 self.init(
45 code: CALL_SUCCESS,
46 errorBuf: RustBuffer.init(
47 capacity: 0,
48 len: 0,
49 data: nil
50 )
51 )
52 }
53 }
54
55 private func rustCall<T>(_ callback: (UnsafeMutablePointer<RustCallStatus>) -> T) throws -> T {
56 let neverThrow: ((RustBuffer) throws -> Never)? = nil
57 return try makeRustCall(callback, errorHandler: neverThrow)
58 }
59
60 private func rustCallWithError<T, E: Swift.Error>(
61 _ errorHandler: @escaping (RustBuffer) throws -> E,
62 _ callback: (UnsafeMutablePointer<RustCallStatus>) -> T) throws -> T {
63 try makeRustCall(callback, errorHandler: errorHandler)
64 }
65
66 private func makeRustCall<T, E: Swift.Error>(
67 _ callback: (UnsafeMutablePointer<RustCallStatus>) -> T,
68 errorHandler: ((RustBuffer) throws -> E)?
69 ) throws -> T {
70 {{ ensure_init_fn_name }}()
71 var callStatus = RustCallStatus.init()
72 let returnedVal = callback(&callStatus)
73 try uniffiCheckCallStatus(callStatus: callStatus, errorHandler: errorHandler)
74 return returnedVal
75 }
76
77 private func uniffiCheckCallStatus<E: Swift.Error>(
78 callStatus: RustCallStatus,
79 errorHandler: ((RustBuffer) throws -> E)?
80 ) throws {
81 switch callStatus.code {
82 case CALL_SUCCESS:
83 return
84
85 case CALL_ERROR:
86 if let errorHandler = errorHandler {
87 throw try errorHandler(callStatus.errorBuf)
88 } else {
89 callStatus.errorBuf.deallocate()
90 throw UniffiInternalError.unexpectedRustCallError
91 }
92
93 case CALL_UNEXPECTED_ERROR:
94 // When the rust code sees a panic, it tries to construct a RustBuffer
95 // with the message. But if that code panics, then it just sends back
96 // an empty buffer.
97 if callStatus.errorBuf.len > 0 {
98 throw UniffiInternalError.rustPanic(try {{ Type::String.borrow()|lift_fn }}(callStatus.errorBuf))
99 } else {
100 callStatus.errorBuf.deallocate()
101 throw UniffiInternalError.rustPanic("Rust panic")
102 }
103
104 case CALL_CANCELLED:
105 fatalError("Cancellation not supported yet")
106
107 default:
108 throw UniffiInternalError.unexpectedRustCallStatusCode
109 }
110 }
111
112 private func uniffiTraitInterfaceCall<T>(
113 callStatus: UnsafeMutablePointer<RustCallStatus>,
114 makeCall: () throws -> T,
115 writeReturn: (T) -> ()
116 ) {
117 do {
118 try writeReturn(makeCall())
119 } catch let error {
120 callStatus.pointee.code = CALL_UNEXPECTED_ERROR
121 callStatus.pointee.errorBuf = {{ Type::String.borrow()|lower_fn }}(String(describing: error))
122 }
123 }
124
125 private func uniffiTraitInterfaceCallWithError<T, E>(
126 callStatus: UnsafeMutablePointer<RustCallStatus>,
127 makeCall: () throws -> T,
128 writeReturn: (T) -> (),
129 lowerError: (E) -> RustBuffer
130 ) {
131 do {
132 try writeReturn(makeCall())
133 } catch let error as E {
134 callStatus.pointee.code = CALL_ERROR
135 callStatus.pointee.errorBuf = lowerError(error)
136 } catch {
137 callStatus.pointee.code = CALL_UNEXPECTED_ERROR
138 callStatus.pointee.errorBuf = {{ Type::String.borrow()|lower_fn }}(String(describing: error))
139 }
140 }