@syntropika/tembra-react-native 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +8 -2
  3. package/Tembra.podspec +21 -0
  4. package/android/build.gradle +47 -0
  5. package/android/src/main/AndroidManifest.xml +1 -0
  6. package/android/src/main/java/com/tembra/TembraModule.kt +52 -0
  7. package/android/src/main/java/com/tembra/TembraPackage.kt +26 -0
  8. package/ios/RCTTembra.h +10 -0
  9. package/ios/RCTTembra.mm +58 -0
  10. package/ios/Tembra.swift +55 -0
  11. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0-sources.jar +0 -0
  12. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0-sources.jar.md5 +1 -0
  13. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0-sources.jar.sha1 +1 -0
  14. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0-sources.jar.sha256 +1 -0
  15. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0-sources.jar.sha512 +1 -0
  16. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.aar +0 -0
  17. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.aar.md5 +1 -0
  18. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.aar.sha1 +1 -0
  19. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.aar.sha256 +1 -0
  20. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.aar.sha512 +1 -0
  21. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.module +116 -0
  22. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.module.md5 +1 -0
  23. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.module.sha1 +1 -0
  24. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.module.sha256 +1 -0
  25. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.module.sha512 +1 -0
  26. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.pom +35 -0
  27. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.pom.md5 +1 -0
  28. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.pom.sha1 +1 -0
  29. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.pom.sha256 +1 -0
  30. package/native/maven/dev/tembra/tembra-kotlin/0.1.0/tembra-kotlin-0.1.0.pom.sha512 +1 -0
  31. package/native/maven/dev/tembra/tembra-kotlin/maven-metadata.xml +13 -0
  32. package/native/maven/dev/tembra/tembra-kotlin/maven-metadata.xml.md5 +1 -0
  33. package/native/maven/dev/tembra/tembra-kotlin/maven-metadata.xml.sha1 +1 -0
  34. package/native/maven/dev/tembra/tembra-kotlin/maven-metadata.xml.sha256 +1 -0
  35. package/native/maven/dev/tembra/tembra-kotlin/maven-metadata.xml.sha512 +1 -0
  36. package/native/swift/Sources/TembraNative/Generated/TembraNative.swift +780 -0
  37. package/native/swift/Sources/TembraNative/TembraKernel.swift +24 -0
  38. package/native/swift/TembraFFI.xcframework/Info.plist +65 -0
  39. package/native/swift/TembraFFI.xcframework/ios-arm64/Headers/TembraFFI.h +556 -0
  40. package/native/swift/TembraFFI.xcframework/ios-arm64/Headers/module.modulemap +7 -0
  41. package/native/swift/TembraFFI.xcframework/ios-arm64/libtembra_mobile.a +0 -0
  42. package/native/swift/TembraFFI.xcframework/ios-arm64_x86_64-simulator/Headers/TembraFFI.h +556 -0
  43. package/native/swift/TembraFFI.xcframework/ios-arm64_x86_64-simulator/Headers/module.modulemap +7 -0
  44. package/native/swift/TembraFFI.xcframework/ios-arm64_x86_64-simulator/libtembra_mobile.a +4 -0
  45. package/native/swift/TembraFFI.xcframework/macos-arm64_x86_64/Headers/TembraFFI.h +556 -0
  46. package/native/swift/TembraFFI.xcframework/macos-arm64_x86_64/Headers/module.modulemap +7 -0
  47. package/native/swift/TembraFFI.xcframework/macos-arm64_x86_64/libtembra_mobile.a +4 -0
  48. package/package.json +49 -4
  49. package/react-native.config.js +12 -0
  50. package/src/NativeTembra.ts +13 -0
  51. package/src/index.ts +22 -0
  52. package/src/kernel.ts +25 -0
@@ -0,0 +1,780 @@
1
+ // This file was autogenerated by some hot garbage in the `uniffi` crate.
2
+ // Trust me, you don't want to mess with it!
3
+
4
+ // swiftlint:disable all
5
+ import Foundation
6
+
7
+ // Depending on the consumer's build setup, the low-level FFI code
8
+ // might be in a separate module, or it might be compiled inline into
9
+ // this module. This is a bit of light hackery to work with both.
10
+ #if canImport(TembraFFI)
11
+ import TembraFFI
12
+ #endif
13
+
14
+ fileprivate extension RustBuffer {
15
+ // Allocate a new buffer, copying the contents of a `UInt8` array.
16
+ init(bytes: [UInt8]) {
17
+ let rbuf = bytes.withUnsafeBufferPointer { ptr in
18
+ RustBuffer.from(ptr)
19
+ }
20
+ self.init(capacity: rbuf.capacity, len: rbuf.len, data: rbuf.data)
21
+ }
22
+
23
+ static func empty() -> RustBuffer {
24
+ RustBuffer(capacity: 0, len:0, data: nil)
25
+ }
26
+
27
+ static func from(_ ptr: UnsafeBufferPointer<UInt8>) -> RustBuffer {
28
+ try! rustCall { ffi_tembra_mobile_rustbuffer_from_bytes(ForeignBytes(bufferPointer: ptr), $0) }
29
+ }
30
+
31
+ // Frees the buffer in place.
32
+ // The buffer must not be used after this is called.
33
+ func deallocate() {
34
+ try! rustCall { ffi_tembra_mobile_rustbuffer_free(self, $0) }
35
+ }
36
+ }
37
+
38
+ fileprivate extension ForeignBytes {
39
+ init(bufferPointer: UnsafeBufferPointer<UInt8>) {
40
+ self.init(len: Int32(bufferPointer.count), data: bufferPointer.baseAddress)
41
+ }
42
+
43
+ init(rawBufferPointer: UnsafeRawBufferPointer) {
44
+ self.init(
45
+ len: Int32(rawBufferPointer.count),
46
+ data: rawBufferPointer.baseAddress?.assumingMemoryBound(to: UInt8.self)
47
+ )
48
+ }
49
+ }
50
+
51
+ // Converter for `&[u8]` / `[ByRef] bytes` arguments.
52
+ //
53
+ // Conforms to `FfiConverter` so the compiler enforces the full converter
54
+ // method set. Only the scope-bound `lower(_:_body:)` overload is sound —
55
+ // zero-copy byte buffers only flow foreign -> Rust, and only in argument
56
+ // position. The four protocol-witness methods (`lift`, `lower`, `read`,
57
+ // `write`) `fatalError` at runtime if anyone reaches them.
58
+ //
59
+ // The scope-bound `lower` takes a closure because the `ForeignBytes`
60
+ // pointer is only guaranteed valid for the duration of
61
+ // `Data.withUnsafeBytes`. Callers must run the full FFI call inside
62
+ // the closure body.
63
+ fileprivate enum FfiConverterByRefBytes: FfiConverter {
64
+ typealias SwiftType = Data
65
+ typealias FfiType = ForeignBytes
66
+
67
+ static func lower<R>(_ value: Data, _ body: (ForeignBytes) throws -> R) rethrows -> R {
68
+ return try value.withUnsafeBytes { rawBuf in
69
+ try body(ForeignBytes(rawBufferPointer: rawBuf))
70
+ }
71
+ }
72
+
73
+ static func lower(_ value: Data) -> ForeignBytes {
74
+ fatalError("ByRef bytes cannot use the plain lower: returning ForeignBytes escapes the Data.withUnsafeBytes scope. Use the scope-bound lower(_:_body:) overload instead.")
75
+ }
76
+
77
+ static func lift(_ value: ForeignBytes) throws -> Data {
78
+ fatalError("ByRef bytes cannot be lifted: zero-copy &[u8] only flows foreign->Rust")
79
+ }
80
+
81
+ static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Data {
82
+ fatalError("ByRef bytes cannot be read from a buffer: zero-copy &[u8] is only supported in argument position, not nested in records/options/etc.")
83
+ }
84
+
85
+ static func write(_ value: Data, into buf: inout [UInt8]) {
86
+ fatalError("ByRef bytes cannot be written to a buffer: zero-copy &[u8] is only supported in argument position, not nested in records/options/etc.")
87
+ }
88
+ }
89
+
90
+ // For every type used in the interface, we provide helper methods for conveniently
91
+ // lifting and lowering that type from C-compatible data, and for reading and writing
92
+ // values of that type in a buffer.
93
+
94
+ // Helper classes/extensions that don't change.
95
+ // Someday, this will be in a library of its own.
96
+
97
+ fileprivate extension Data {
98
+ init(rustBuffer: RustBuffer) {
99
+ self.init(
100
+ bytesNoCopy: rustBuffer.data!,
101
+ count: Int(rustBuffer.len),
102
+ deallocator: .none
103
+ )
104
+ }
105
+ }
106
+
107
+ // Define reader functionality. Normally this would be defined in a class or
108
+ // struct, but we use standalone functions instead in order to make external
109
+ // types work.
110
+ //
111
+ // With external types, one swift source file needs to be able to call the read
112
+ // method on another source file's FfiConverter, but then what visibility
113
+ // should Reader have?
114
+ // - If Reader is fileprivate, then this means the read() must also
115
+ // be fileprivate, which doesn't work with external types.
116
+ // - If Reader is internal/public, we'll get compile errors since both source
117
+ // files will try define the same type.
118
+ //
119
+ // Instead, the read() method and these helper functions input a tuple of data
120
+
121
+ fileprivate func createReader(data: Data) -> (data: Data, offset: Data.Index) {
122
+ (data: data, offset: 0)
123
+ }
124
+
125
+ // Reads an integer at the current offset, in big-endian order, and advances
126
+ // the offset on success. Throws if reading the integer would move the
127
+ // offset past the end of the buffer.
128
+ fileprivate func readInt<T: FixedWidthInteger>(_ reader: inout (data: Data, offset: Data.Index)) throws -> T {
129
+ let range = reader.offset..<reader.offset + MemoryLayout<T>.size
130
+ guard reader.data.count >= range.upperBound else {
131
+ throw UniffiInternalError.bufferOverflow
132
+ }
133
+ if T.self == UInt8.self {
134
+ let value = reader.data[reader.offset]
135
+ reader.offset += 1
136
+ return value as! T
137
+ }
138
+ var value: T = 0
139
+ let _ = withUnsafeMutableBytes(of: &value, { reader.data.copyBytes(to: $0, from: range)})
140
+ reader.offset = range.upperBound
141
+ return value.bigEndian
142
+ }
143
+
144
+ // Reads an arbitrary number of bytes, to be used to read
145
+ // raw bytes, this is useful when lifting strings
146
+ fileprivate func readBytes(_ reader: inout (data: Data, offset: Data.Index), count: Int) throws -> Array<UInt8> {
147
+ let range = reader.offset..<(reader.offset+count)
148
+ guard reader.data.count >= range.upperBound else {
149
+ throw UniffiInternalError.bufferOverflow
150
+ }
151
+ var value = [UInt8](repeating: 0, count: count)
152
+ value.withUnsafeMutableBufferPointer({ buffer in
153
+ reader.data.copyBytes(to: buffer, from: range)
154
+ })
155
+ reader.offset = range.upperBound
156
+ return value
157
+ }
158
+
159
+ // Reads a float at the current offset.
160
+ fileprivate func readFloat(_ reader: inout (data: Data, offset: Data.Index)) throws -> Float {
161
+ return Float(bitPattern: try readInt(&reader))
162
+ }
163
+
164
+ // Reads a float at the current offset.
165
+ fileprivate func readDouble(_ reader: inout (data: Data, offset: Data.Index)) throws -> Double {
166
+ return Double(bitPattern: try readInt(&reader))
167
+ }
168
+
169
+ // Indicates if the offset has reached the end of the buffer.
170
+ fileprivate func hasRemaining(_ reader: (data: Data, offset: Data.Index)) -> Bool {
171
+ return reader.offset < reader.data.count
172
+ }
173
+
174
+ // Define writer functionality. Normally this would be defined in a class or
175
+ // struct, but we use standalone functions instead in order to make external
176
+ // types work. See the above discussion on Readers for details.
177
+
178
+ fileprivate func createWriter() -> [UInt8] {
179
+ return []
180
+ }
181
+
182
+ fileprivate func writeBytes<S>(_ writer: inout [UInt8], _ byteArr: S) where S: Sequence, S.Element == UInt8 {
183
+ writer.append(contentsOf: byteArr)
184
+ }
185
+
186
+ // Writes an integer in big-endian order.
187
+ //
188
+ // Warning: make sure what you are trying to write
189
+ // is in the correct type!
190
+ fileprivate func writeInt<T: FixedWidthInteger>(_ writer: inout [UInt8], _ value: T) {
191
+ var value = value.bigEndian
192
+ withUnsafeBytes(of: &value) { writer.append(contentsOf: $0) }
193
+ }
194
+
195
+ fileprivate func writeFloat(_ writer: inout [UInt8], _ value: Float) {
196
+ writeInt(&writer, value.bitPattern)
197
+ }
198
+
199
+ fileprivate func writeDouble(_ writer: inout [UInt8], _ value: Double) {
200
+ writeInt(&writer, value.bitPattern)
201
+ }
202
+
203
+ // Protocol for types that transfer other types across the FFI. This is
204
+ // analogous to the Rust trait of the same name.
205
+ fileprivate protocol FfiConverter {
206
+ associatedtype FfiType
207
+ associatedtype SwiftType
208
+
209
+ static func lift(_ value: FfiType) throws -> SwiftType
210
+ static func lower(_ value: SwiftType) -> FfiType
211
+ static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> SwiftType
212
+ static func write(_ value: SwiftType, into buf: inout [UInt8])
213
+ }
214
+
215
+ // Types conforming to `Primitive` pass themselves directly over the FFI.
216
+ fileprivate protocol FfiConverterPrimitive: FfiConverter where FfiType == SwiftType { }
217
+
218
+ extension FfiConverterPrimitive {
219
+ #if swift(>=5.8)
220
+ @_documentation(visibility: private)
221
+ #endif
222
+ public static func lift(_ value: FfiType) throws -> SwiftType {
223
+ return value
224
+ }
225
+
226
+ #if swift(>=5.8)
227
+ @_documentation(visibility: private)
228
+ #endif
229
+ public static func lower(_ value: SwiftType) -> FfiType {
230
+ return value
231
+ }
232
+ }
233
+
234
+ // Types conforming to `FfiConverterRustBuffer` lift and lower into a `RustBuffer`.
235
+ // Used for complex types where it's hard to write a custom lift/lower.
236
+ fileprivate protocol FfiConverterRustBuffer: FfiConverter where FfiType == RustBuffer {}
237
+
238
+ extension FfiConverterRustBuffer {
239
+ #if swift(>=5.8)
240
+ @_documentation(visibility: private)
241
+ #endif
242
+ public static func lift(_ buf: RustBuffer) throws -> SwiftType {
243
+ var reader = createReader(data: Data(rustBuffer: buf))
244
+ let value = try read(from: &reader)
245
+ if hasRemaining(reader) {
246
+ throw UniffiInternalError.incompleteData
247
+ }
248
+ buf.deallocate()
249
+ return value
250
+ }
251
+
252
+ #if swift(>=5.8)
253
+ @_documentation(visibility: private)
254
+ #endif
255
+ public static func lower(_ value: SwiftType) -> RustBuffer {
256
+ var writer = createWriter()
257
+ write(value, into: &writer)
258
+ return RustBuffer(bytes: writer)
259
+ }
260
+ }
261
+ // An error type for FFI errors. These errors occur at the UniFFI level, not
262
+ // the library level.
263
+ fileprivate enum UniffiInternalError: LocalizedError {
264
+ case bufferOverflow
265
+ case incompleteData
266
+ case unexpectedOptionalTag
267
+ case unexpectedEnumCase
268
+ case unexpectedNullPointer
269
+ case unexpectedRustCallStatusCode
270
+ case unexpectedRustCallError
271
+ case unexpectedStaleHandle
272
+ case rustPanic(_ message: String)
273
+
274
+ public var errorDescription: String? {
275
+ switch self {
276
+ case .bufferOverflow: return "Reading the requested value would read past the end of the buffer"
277
+ case .incompleteData: return "The buffer still has data after lifting its containing value"
278
+ case .unexpectedOptionalTag: return "Unexpected optional tag; should be 0 or 1"
279
+ case .unexpectedEnumCase: return "Raw enum value doesn't match any cases"
280
+ case .unexpectedNullPointer: return "Raw pointer value was null"
281
+ case .unexpectedRustCallStatusCode: return "Unexpected RustCallStatus code"
282
+ case .unexpectedRustCallError: return "CALL_ERROR but no errorClass specified"
283
+ case .unexpectedStaleHandle: return "The object in the handle map has been dropped already"
284
+ case let .rustPanic(message): return message
285
+ }
286
+ }
287
+ }
288
+
289
+ fileprivate extension NSLock {
290
+ func withLock<T>(f: () throws -> T) rethrows -> T {
291
+ self.lock()
292
+ defer { self.unlock() }
293
+ return try f()
294
+ }
295
+ }
296
+
297
+ fileprivate let CALL_SUCCESS: Int8 = 0
298
+ fileprivate let CALL_ERROR: Int8 = 1
299
+ fileprivate let CALL_UNEXPECTED_ERROR: Int8 = 2
300
+ fileprivate let CALL_CANCELLED: Int8 = 3
301
+
302
+ fileprivate extension RustCallStatus {
303
+ init() {
304
+ self.init(
305
+ code: CALL_SUCCESS,
306
+ errorBuf: RustBuffer.init(
307
+ capacity: 0,
308
+ len: 0,
309
+ data: nil
310
+ )
311
+ )
312
+ }
313
+ }
314
+
315
+ private func rustCall<T>(_ callback: (UnsafeMutablePointer<RustCallStatus>) -> T) throws -> T {
316
+ let neverThrow: ((RustBuffer) throws -> Never)? = nil
317
+ return try makeRustCall(callback, errorHandler: neverThrow)
318
+ }
319
+
320
+ private func rustCallWithError<T, E: Swift.Error>(
321
+ _ errorHandler: @escaping (RustBuffer) throws -> E,
322
+ _ callback: (UnsafeMutablePointer<RustCallStatus>) -> T) throws -> T {
323
+ try makeRustCall(callback, errorHandler: errorHandler)
324
+ }
325
+
326
+ private func makeRustCall<T, E: Swift.Error>(
327
+ _ callback: (UnsafeMutablePointer<RustCallStatus>) -> T,
328
+ errorHandler: ((RustBuffer) throws -> E)?
329
+ ) throws -> T {
330
+ uniffiEnsureTembraMobileInitialized()
331
+ var callStatus = RustCallStatus.init()
332
+ let returnedVal = callback(&callStatus)
333
+ try uniffiCheckCallStatus(callStatus: callStatus, errorHandler: errorHandler)
334
+ return returnedVal
335
+ }
336
+
337
+ private func uniffiCheckCallStatus<E: Swift.Error>(
338
+ callStatus: RustCallStatus,
339
+ errorHandler: ((RustBuffer) throws -> E)?
340
+ ) throws {
341
+ switch callStatus.code {
342
+ case CALL_SUCCESS:
343
+ return
344
+
345
+ case CALL_ERROR:
346
+ if let errorHandler = errorHandler {
347
+ throw try errorHandler(callStatus.errorBuf)
348
+ } else {
349
+ callStatus.errorBuf.deallocate()
350
+ throw UniffiInternalError.unexpectedRustCallError
351
+ }
352
+
353
+ case CALL_UNEXPECTED_ERROR:
354
+ // When the rust code sees a panic, it tries to construct a RustBuffer
355
+ // with the message. But if that code panics, then it just sends back
356
+ // an empty buffer.
357
+ if callStatus.errorBuf.len > 0 {
358
+ throw UniffiInternalError.rustPanic(try FfiConverterString.lift(callStatus.errorBuf))
359
+ } else {
360
+ callStatus.errorBuf.deallocate()
361
+ throw UniffiInternalError.rustPanic("Rust panic")
362
+ }
363
+
364
+ case CALL_CANCELLED:
365
+ fatalError("Cancellation not supported yet")
366
+
367
+ default:
368
+ throw UniffiInternalError.unexpectedRustCallStatusCode
369
+ }
370
+ }
371
+
372
+ private func uniffiTraitInterfaceCall<T>(
373
+ callStatus: UnsafeMutablePointer<RustCallStatus>,
374
+ makeCall: () throws -> T,
375
+ writeReturn: (T) -> ()
376
+ ) {
377
+ do {
378
+ try writeReturn(makeCall())
379
+ } catch let error {
380
+ callStatus.pointee.code = CALL_UNEXPECTED_ERROR
381
+ callStatus.pointee.errorBuf = FfiConverterString.lower(String(describing: error))
382
+ }
383
+ }
384
+
385
+ private func uniffiTraitInterfaceCallWithError<T, E>(
386
+ callStatus: UnsafeMutablePointer<RustCallStatus>,
387
+ makeCall: () throws -> T,
388
+ writeReturn: (T) -> (),
389
+ lowerError: (E) -> RustBuffer
390
+ ) {
391
+ do {
392
+ try writeReturn(makeCall())
393
+ } catch let error as E {
394
+ callStatus.pointee.code = CALL_ERROR
395
+ callStatus.pointee.errorBuf = lowerError(error)
396
+ } catch {
397
+ callStatus.pointee.code = CALL_UNEXPECTED_ERROR
398
+ callStatus.pointee.errorBuf = FfiConverterString.lower(String(describing: error))
399
+ }
400
+ }
401
+ // Initial value and increment amount for handles.
402
+ // These ensure that SWIFT handles always have the lowest bit set
403
+ fileprivate let UNIFFI_HANDLEMAP_INITIAL: UInt64 = 1
404
+ fileprivate let UNIFFI_HANDLEMAP_DELTA: UInt64 = 2
405
+
406
+ fileprivate final class UniffiHandleMap<T>: @unchecked Sendable {
407
+ // All mutation happens with this lock held, which is why we implement @unchecked Sendable.
408
+ private let lock = NSLock()
409
+ private var map: [UInt64: T] = [:]
410
+ private var currentHandle: UInt64 = UNIFFI_HANDLEMAP_INITIAL
411
+
412
+ func insert(obj: T) -> UInt64 {
413
+ lock.withLock {
414
+ return doInsert(obj)
415
+ }
416
+ }
417
+
418
+ // Low-level insert function, this assumes `lock` is held.
419
+ private func doInsert(_ obj: T) -> UInt64 {
420
+ let handle = currentHandle
421
+ currentHandle += UNIFFI_HANDLEMAP_DELTA
422
+ map[handle] = obj
423
+ return handle
424
+ }
425
+
426
+ func get(handle: UInt64) throws -> T {
427
+ try lock.withLock {
428
+ guard let obj = map[handle] else {
429
+ throw UniffiInternalError.unexpectedStaleHandle
430
+ }
431
+ return obj
432
+ }
433
+ }
434
+
435
+ func clone(handle: UInt64) throws -> UInt64 {
436
+ try lock.withLock {
437
+ guard let obj = map[handle] else {
438
+ throw UniffiInternalError.unexpectedStaleHandle
439
+ }
440
+ return doInsert(obj)
441
+ }
442
+ }
443
+
444
+ @discardableResult
445
+ func remove(handle: UInt64) throws -> T {
446
+ try lock.withLock {
447
+ guard let obj = map.removeValue(forKey: handle) else {
448
+ throw UniffiInternalError.unexpectedStaleHandle
449
+ }
450
+ return obj
451
+ }
452
+ }
453
+
454
+ var count: Int {
455
+ get {
456
+ map.count
457
+ }
458
+ }
459
+ }
460
+
461
+
462
+ // Public interface members begin here.
463
+
464
+
465
+ #if swift(>=5.8)
466
+ @_documentation(visibility: private)
467
+ #endif
468
+ fileprivate struct FfiConverterString: FfiConverter {
469
+ typealias SwiftType = String
470
+ typealias FfiType = RustBuffer
471
+
472
+ public static func lift(_ value: RustBuffer) throws -> String {
473
+ defer {
474
+ value.deallocate()
475
+ }
476
+ if value.data == nil {
477
+ return String()
478
+ }
479
+ let bytes = UnsafeBufferPointer<UInt8>(start: value.data!, count: Int(value.len))
480
+ // Use Swift's native UTF-8 decoder; `String(bytes:encoding:.utf8)` goes
481
+ // through Foundation's NSString and silently strips a leading U+FEFF BOM.
482
+ // Invalid UTF-8 substitutes U+FFFD instead of trapping (unreachable
483
+ // given Rust's `String` invariant).
484
+ return String(decoding: bytes, as: UTF8.self)
485
+ }
486
+
487
+ public static func lower(_ value: String) -> RustBuffer {
488
+ return value.utf8CString.withUnsafeBufferPointer { ptr in
489
+ // The swift string gives us int8_t, we want uint8_t.
490
+ ptr.withMemoryRebound(to: UInt8.self) { ptr in
491
+ // The swift string gives us a trailing null byte, we don't want it.
492
+ let buf = UnsafeBufferPointer(rebasing: ptr.prefix(upTo: ptr.count - 1))
493
+ return RustBuffer.from(buf)
494
+ }
495
+ }
496
+ }
497
+
498
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> String {
499
+ let len: Int32 = try readInt(&buf)
500
+ // See `lift` above for why we avoid Foundation's NSString-backed decoder here.
501
+ return String(decoding: try readBytes(&buf, count: Int(len)), as: UTF8.self)
502
+ }
503
+
504
+ public static func write(_ value: String, into buf: inout [UInt8]) {
505
+ let len = Int32(value.utf8.count)
506
+ writeInt(&buf, len)
507
+ writeBytes(&buf, value.utf8)
508
+ }
509
+ }
510
+
511
+
512
+
513
+
514
+ public protocol NativeKernelProtocol: AnyObject, Sendable {
515
+
516
+ /**
517
+ * Exchange metadata synchronously while serializing requests with release.
518
+ */
519
+ func request(metadata: String) throws -> String
520
+
521
+ /**
522
+ * Release native storage after the language adapter has completed host shutdown.
523
+ */
524
+ func shutdown()
525
+
526
+ }
527
+ open class NativeKernel: NativeKernelProtocol, @unchecked Sendable {
528
+ fileprivate let handle: UInt64
529
+
530
+ /// Used to instantiate a [FFIObject] without an actual handle, for fakes in tests, mostly.
531
+ #if swift(>=5.8)
532
+ @_documentation(visibility: private)
533
+ #endif
534
+ public struct NoHandle {
535
+ public init() {}
536
+ }
537
+
538
+ // TODO: We'd like this to be `private` but for Swifty reasons,
539
+ // we can't implement `FfiConverter` without making this `required` and we can't
540
+ // make it `required` without making it `public`.
541
+ #if swift(>=5.8)
542
+ @_documentation(visibility: private)
543
+ #endif
544
+ required public init(unsafeFromHandle handle: UInt64) {
545
+ self.handle = handle
546
+ }
547
+
548
+ // This constructor can be used to instantiate a fake object.
549
+ // - Parameter noHandle: Placeholder value so we can have a constructor separate from the default empty one that may be implemented for classes extending [FFIObject].
550
+ //
551
+ // - Warning:
552
+ // Any object instantiated with this constructor cannot be passed to an actual Rust-backed object. Since there isn't a backing handle the FFI lower functions will crash.
553
+ #if swift(>=5.8)
554
+ @_documentation(visibility: private)
555
+ #endif
556
+ public init(noHandle: NoHandle) {
557
+ self.handle = 0
558
+ }
559
+
560
+ #if swift(>=5.8)
561
+ @_documentation(visibility: private)
562
+ #endif
563
+ public func uniffiCloneHandle() -> UInt64 {
564
+ return try! rustCall { uniffi_tembra_mobile_fn_clone_nativekernel(self.handle, $0) }
565
+ }
566
+ /**
567
+ * Create an isolated kernel without moving its thread-affine state across FFI threads.
568
+ */
569
+ public convenience init()throws {
570
+ let handle =
571
+ try rustCallWithError(FfiConverterTypeMobileError_lift) {
572
+ uniffiCallStatus in
573
+ uniffi_tembra_mobile_fn_constructor_nativekernel_new(uniffiCallStatus
574
+ )
575
+ }
576
+ self.init(unsafeFromHandle: handle)
577
+ }
578
+
579
+ deinit {
580
+ if handle == 0 {
581
+ // Mock objects have handle=0 don't try to free them
582
+ return
583
+ }
584
+
585
+ try! rustCall { uniffi_tembra_mobile_fn_free_nativekernel(handle, $0) }
586
+ }
587
+
588
+
589
+
590
+
591
+ /**
592
+ * Exchange metadata synchronously while serializing requests with release.
593
+ */
594
+ open func request(metadata: String)throws -> String {
595
+ return try FfiConverterString.lift(try rustCallWithError(FfiConverterTypeMobileError_lift) {
596
+ uniffiCallStatus in
597
+ uniffi_tembra_mobile_fn_method_nativekernel_request(
598
+ self.uniffiCloneHandle(),
599
+ FfiConverterString.lower(metadata),uniffiCallStatus
600
+ )
601
+ })
602
+ }
603
+
604
+ /**
605
+ * Release native storage after the language adapter has completed host shutdown.
606
+ */
607
+ open func shutdown() {try! rustCall() {
608
+ uniffiCallStatus in
609
+ uniffi_tembra_mobile_fn_method_nativekernel_shutdown(
610
+ self.uniffiCloneHandle(),uniffiCallStatus
611
+ )
612
+ }
613
+ }
614
+
615
+
616
+
617
+ }
618
+
619
+
620
+ #if swift(>=5.8)
621
+ @_documentation(visibility: private)
622
+ #endif
623
+ public struct FfiConverterTypeNativeKernel: FfiConverter {
624
+ typealias FfiType = UInt64
625
+ typealias SwiftType = NativeKernel
626
+
627
+ public static func lift(_ handle: UInt64) throws -> NativeKernel {
628
+ return NativeKernel(unsafeFromHandle: handle)
629
+ }
630
+
631
+ public static func lower(_ value: NativeKernel) -> UInt64 {
632
+ return value.uniffiCloneHandle()
633
+ }
634
+
635
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> NativeKernel {
636
+ let handle: UInt64 = try readInt(&buf)
637
+ return try lift(handle)
638
+ }
639
+
640
+ public static func write(_ value: NativeKernel, into buf: inout [UInt8]) {
641
+ writeInt(&buf, lower(value))
642
+ }
643
+ }
644
+
645
+
646
+ #if swift(>=5.8)
647
+ @_documentation(visibility: private)
648
+ #endif
649
+ public func FfiConverterTypeNativeKernel_lift(_ handle: UInt64) throws -> NativeKernel {
650
+ return try FfiConverterTypeNativeKernel.lift(handle)
651
+ }
652
+
653
+ #if swift(>=5.8)
654
+ @_documentation(visibility: private)
655
+ #endif
656
+ public func FfiConverterTypeNativeKernel_lower(_ value: NativeKernel) -> UInt64 {
657
+ return FfiConverterTypeNativeKernel.lower(value)
658
+ }
659
+
660
+
661
+
662
+
663
+ public
664
+ enum MobileError: Swift.Error, Equatable, Hashable, Foundation.LocalizedError {
665
+
666
+
667
+
668
+ case Closed
669
+ case WorkerUnavailable
670
+
671
+
672
+
673
+
674
+
675
+
676
+ public var errorDescription: String? {
677
+ String(reflecting: self)
678
+ }
679
+
680
+ }
681
+
682
+ #if compiler(>=6)
683
+ extension MobileError: Sendable {}
684
+ #endif
685
+
686
+ #if swift(>=5.8)
687
+ @_documentation(visibility: private)
688
+ #endif
689
+ public struct FfiConverterTypeMobileError: FfiConverterRustBuffer {
690
+ typealias SwiftType = MobileError
691
+
692
+ public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> MobileError {
693
+ let variant: Int32 = try readInt(&buf)
694
+ switch variant {
695
+
696
+
697
+
698
+
699
+ case 1: return .Closed
700
+ case 2: return .WorkerUnavailable
701
+
702
+ default: throw UniffiInternalError.unexpectedEnumCase
703
+ }
704
+ }
705
+
706
+ public static func write(_ value: MobileError, into buf: inout [UInt8]) {
707
+ switch value {
708
+
709
+
710
+
711
+
712
+
713
+ case .Closed:
714
+ writeInt(&buf, Int32(1))
715
+
716
+
717
+ case .WorkerUnavailable:
718
+ writeInt(&buf, Int32(2))
719
+
720
+ }
721
+ }
722
+ }
723
+
724
+
725
+ #if swift(>=5.8)
726
+ @_documentation(visibility: private)
727
+ #endif
728
+ public func FfiConverterTypeMobileError_lift(_ buf: RustBuffer) throws -> MobileError {
729
+ return try FfiConverterTypeMobileError.lift(buf)
730
+ }
731
+
732
+ #if swift(>=5.8)
733
+ @_documentation(visibility: private)
734
+ #endif
735
+ public func FfiConverterTypeMobileError_lower(_ value: MobileError) -> RustBuffer {
736
+ return FfiConverterTypeMobileError.lower(value)
737
+ }
738
+
739
+ private enum InitializationResult {
740
+ case ok
741
+ case contractVersionMismatch
742
+ case apiChecksumMismatch
743
+ }
744
+ // Use a global variable to perform the versioning checks. Swift ensures that
745
+ // the code inside is only computed once.
746
+ private let initializationResult: InitializationResult = {
747
+ // Get the bindings contract version from our ComponentInterface
748
+ let bindings_contract_version = 30
749
+ // Get the scaffolding contract version by calling the into the dylib
750
+ let scaffolding_contract_version = ffi_tembra_mobile_uniffi_contract_version()
751
+ if bindings_contract_version != scaffolding_contract_version {
752
+ return InitializationResult.contractVersionMismatch
753
+ }
754
+ if (uniffi_tembra_mobile_checksum_method_nativekernel_request() != 33307) {
755
+ return InitializationResult.apiChecksumMismatch
756
+ }
757
+ if (uniffi_tembra_mobile_checksum_method_nativekernel_shutdown() != 34980) {
758
+ return InitializationResult.apiChecksumMismatch
759
+ }
760
+ if (uniffi_tembra_mobile_checksum_constructor_nativekernel_new() != 9406) {
761
+ return InitializationResult.apiChecksumMismatch
762
+ }
763
+
764
+ return InitializationResult.ok
765
+ }()
766
+
767
+ // Make the ensure init function public so that other modules which have external type references to
768
+ // our types can call it.
769
+ public func uniffiEnsureTembraMobileInitialized() {
770
+ switch initializationResult {
771
+ case .ok:
772
+ break
773
+ case .contractVersionMismatch:
774
+ fatalError("UniFFI contract version mismatch: try cleaning and rebuilding your project")
775
+ case .apiChecksumMismatch:
776
+ fatalError("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
777
+ }
778
+ }
779
+
780
+ // swiftlint:enable all