@synonymdev/react-native-pubky 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +57 -0
  3. package/android/build.gradle +101 -0
  4. package/android/gradle.properties +5 -0
  5. package/android/src/main/AndroidManifest.xml +3 -0
  6. package/android/src/main/AndroidManifestNew.xml +2 -0
  7. package/android/src/main/java/com/pubky/PubkyModule.kt +43 -0
  8. package/android/src/main/java/com/pubky/PubkyPackage.kt +17 -0
  9. package/android/src/main/java/com/pubky/pubky.iml +11 -0
  10. package/android/src/main/java/uniffi/mobile/mobile.kt +688 -0
  11. package/android/src/main/jniLibs/arm64-v8a/libmobile.so +0 -0
  12. package/android/src/main/jniLibs/armeabi-v7a/libmobile.so +0 -0
  13. package/android/src/main/jniLibs/x86/libmobile.so +0 -0
  14. package/android/src/main/jniLibs/x86_64/libmobile.so +0 -0
  15. package/ios/Frameworks/Mobile.xcframework/Info.plist +47 -0
  16. package/ios/Frameworks/Mobile.xcframework/ios-arm64/Headers/mobileFFI.h +194 -0
  17. package/ios/Frameworks/Mobile.xcframework/ios-arm64/Headers/module.modulemap +6 -0
  18. package/ios/Frameworks/Mobile.xcframework/ios-arm64/libmobile.a +0 -0
  19. package/ios/Frameworks/Mobile.xcframework/ios-arm64-simulator/Headers/mobileFFI.h +194 -0
  20. package/ios/Frameworks/Mobile.xcframework/ios-arm64-simulator/Headers/module.modulemap +6 -0
  21. package/ios/Frameworks/Mobile.xcframework/ios-arm64-simulator/libmobile.a +0 -0
  22. package/ios/Pubky-Bridging-Header.h +2 -0
  23. package/ios/Pubky.mm +16 -0
  24. package/ios/Pubky.swift +16 -0
  25. package/ios/mobile.swift +484 -0
  26. package/lib/commonjs/index.js +25 -0
  27. package/lib/commonjs/index.js.map +1 -0
  28. package/lib/module/index.js +21 -0
  29. package/lib/module/index.js.map +1 -0
  30. package/lib/typescript/commonjs/package.json +1 -0
  31. package/lib/typescript/commonjs/src/index.d.ts +3 -0
  32. package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
  33. package/lib/typescript/module/package.json +1 -0
  34. package/lib/typescript/module/src/index.d.ts +3 -0
  35. package/lib/typescript/module/src/index.d.ts.map +1 -0
  36. package/package.json +198 -0
  37. package/react-native-pubky.podspec +50 -0
  38. package/src/index.tsx +30 -0
@@ -0,0 +1,688 @@
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
+ @file:Suppress("NAME_SHADOWING")
5
+
6
+ package uniffi.mobile;
7
+
8
+ // Common helper code.
9
+ //
10
+ // Ideally this would live in a separate .kt file where it can be unittested etc
11
+ // in isolation, and perhaps even published as a re-useable package.
12
+ //
13
+ // However, it's important that the details of how this helper code works (e.g. the
14
+ // way that different builtin types are passed across the FFI) exactly match what's
15
+ // expected by the Rust code on the other side of the interface. In practice right
16
+ // now that means coming from the exact some version of `uniffi` that was used to
17
+ // compile the Rust component. The easiest way to ensure this is to bundle the Kotlin
18
+ // helpers directly inline like we're doing here.
19
+
20
+ import com.sun.jna.Library
21
+ import com.sun.jna.IntegerType
22
+ import com.sun.jna.Native
23
+ import com.sun.jna.Pointer
24
+ import com.sun.jna.Structure
25
+ import com.sun.jna.Callback
26
+ import com.sun.jna.ptr.*
27
+ import java.nio.ByteBuffer
28
+ import java.nio.ByteOrder
29
+ import java.nio.CharBuffer
30
+ import java.nio.charset.CodingErrorAction
31
+ import java.util.concurrent.ConcurrentHashMap
32
+ import kotlin.coroutines.resume
33
+ import kotlinx.coroutines.CancellableContinuation
34
+ import kotlinx.coroutines.suspendCancellableCoroutine
35
+
36
+ // This is a helper for safely working with byte buffers returned from the Rust code.
37
+ // A rust-owned buffer is represented by its capacity, its current length, and a
38
+ // pointer to the underlying data.
39
+
40
+ @Structure.FieldOrder("capacity", "len", "data")
41
+ open class RustBuffer : Structure() {
42
+ @JvmField var capacity: Int = 0
43
+ @JvmField var len: Int = 0
44
+ @JvmField var data: Pointer? = null
45
+
46
+ class ByValue: RustBuffer(), Structure.ByValue
47
+ class ByReference: RustBuffer(), Structure.ByReference
48
+
49
+ companion object {
50
+ internal fun alloc(size: Int = 0) = rustCall() { status ->
51
+ _UniFFILib.INSTANCE.ffi_mobile_rustbuffer_alloc(size, status)
52
+ }.also {
53
+ if(it.data == null) {
54
+ throw RuntimeException("RustBuffer.alloc() returned null data pointer (size=${size})")
55
+ }
56
+ }
57
+
58
+ internal fun create(capacity: Int, len: Int, data: Pointer?): RustBuffer.ByValue {
59
+ var buf = RustBuffer.ByValue()
60
+ buf.capacity = capacity
61
+ buf.len = len
62
+ buf.data = data
63
+ return buf
64
+ }
65
+
66
+ internal fun free(buf: RustBuffer.ByValue) = rustCall() { status ->
67
+ _UniFFILib.INSTANCE.ffi_mobile_rustbuffer_free(buf, status)
68
+ }
69
+ }
70
+
71
+ @Suppress("TooGenericExceptionThrown")
72
+ fun asByteBuffer() =
73
+ this.data?.getByteBuffer(0, this.len.toLong())?.also {
74
+ it.order(ByteOrder.BIG_ENDIAN)
75
+ }
76
+ }
77
+
78
+ /**
79
+ * The equivalent of the `*mut RustBuffer` type.
80
+ * Required for callbacks taking in an out pointer.
81
+ *
82
+ * Size is the sum of all values in the struct.
83
+ */
84
+ class RustBufferByReference : ByReference(16) {
85
+ /**
86
+ * Set the pointed-to `RustBuffer` to the given value.
87
+ */
88
+ fun setValue(value: RustBuffer.ByValue) {
89
+ // NOTE: The offsets are as they are in the C-like struct.
90
+ val pointer = getPointer()
91
+ pointer.setInt(0, value.capacity)
92
+ pointer.setInt(4, value.len)
93
+ pointer.setPointer(8, value.data)
94
+ }
95
+
96
+ /**
97
+ * Get a `RustBuffer.ByValue` from this reference.
98
+ */
99
+ fun getValue(): RustBuffer.ByValue {
100
+ val pointer = getPointer()
101
+ val value = RustBuffer.ByValue()
102
+ value.writeField("capacity", pointer.getInt(0))
103
+ value.writeField("len", pointer.getInt(4))
104
+ value.writeField("data", pointer.getPointer(8))
105
+
106
+ return value
107
+ }
108
+ }
109
+
110
+ // This is a helper for safely passing byte references into the rust code.
111
+ // It's not actually used at the moment, because there aren't many things that you
112
+ // can take a direct pointer to in the JVM, and if we're going to copy something
113
+ // then we might as well copy it into a `RustBuffer`. But it's here for API
114
+ // completeness.
115
+
116
+ @Structure.FieldOrder("len", "data")
117
+ open class ForeignBytes : Structure() {
118
+ @JvmField var len: Int = 0
119
+ @JvmField var data: Pointer? = null
120
+
121
+ class ByValue : ForeignBytes(), Structure.ByValue
122
+ }
123
+ // The FfiConverter interface handles converter types to and from the FFI
124
+ //
125
+ // All implementing objects should be public to support external types. When a
126
+ // type is external we need to import it's FfiConverter.
127
+ public interface FfiConverter<KotlinType, FfiType> {
128
+ // Convert an FFI type to a Kotlin type
129
+ fun lift(value: FfiType): KotlinType
130
+
131
+ // Convert an Kotlin type to an FFI type
132
+ fun lower(value: KotlinType): FfiType
133
+
134
+ // Read a Kotlin type from a `ByteBuffer`
135
+ fun read(buf: ByteBuffer): KotlinType
136
+
137
+ // Calculate bytes to allocate when creating a `RustBuffer`
138
+ //
139
+ // This must return at least as many bytes as the write() function will
140
+ // write. It can return more bytes than needed, for example when writing
141
+ // Strings we can't know the exact bytes needed until we the UTF-8
142
+ // encoding, so we pessimistically allocate the largest size possible (3
143
+ // bytes per codepoint). Allocating extra bytes is not really a big deal
144
+ // because the `RustBuffer` is short-lived.
145
+ fun allocationSize(value: KotlinType): Int
146
+
147
+ // Write a Kotlin type to a `ByteBuffer`
148
+ fun write(value: KotlinType, buf: ByteBuffer)
149
+
150
+ // Lower a value into a `RustBuffer`
151
+ //
152
+ // This method lowers a value into a `RustBuffer` rather than the normal
153
+ // FfiType. It's used by the callback interface code. Callback interface
154
+ // returns are always serialized into a `RustBuffer` regardless of their
155
+ // normal FFI type.
156
+ fun lowerIntoRustBuffer(value: KotlinType): RustBuffer.ByValue {
157
+ val rbuf = RustBuffer.alloc(allocationSize(value))
158
+ try {
159
+ val bbuf = rbuf.data!!.getByteBuffer(0, rbuf.capacity.toLong()).also {
160
+ it.order(ByteOrder.BIG_ENDIAN)
161
+ }
162
+ write(value, bbuf)
163
+ rbuf.writeField("len", bbuf.position())
164
+ return rbuf
165
+ } catch (e: Throwable) {
166
+ RustBuffer.free(rbuf)
167
+ throw e
168
+ }
169
+ }
170
+
171
+ // Lift a value from a `RustBuffer`.
172
+ //
173
+ // This here mostly because of the symmetry with `lowerIntoRustBuffer()`.
174
+ // It's currently only used by the `FfiConverterRustBuffer` class below.
175
+ fun liftFromRustBuffer(rbuf: RustBuffer.ByValue): KotlinType {
176
+ val byteBuf = rbuf.asByteBuffer()!!
177
+ try {
178
+ val item = read(byteBuf)
179
+ if (byteBuf.hasRemaining()) {
180
+ throw RuntimeException("junk remaining in buffer after lifting, something is very wrong!!")
181
+ }
182
+ return item
183
+ } finally {
184
+ RustBuffer.free(rbuf)
185
+ }
186
+ }
187
+ }
188
+
189
+ // FfiConverter that uses `RustBuffer` as the FfiType
190
+ public interface FfiConverterRustBuffer<KotlinType>: FfiConverter<KotlinType, RustBuffer.ByValue> {
191
+ override fun lift(value: RustBuffer.ByValue) = liftFromRustBuffer(value)
192
+ override fun lower(value: KotlinType) = lowerIntoRustBuffer(value)
193
+ }
194
+ // A handful of classes and functions to support the generated data structures.
195
+ // This would be a good candidate for isolating in its own ffi-support lib.
196
+ // Error runtime.
197
+ @Structure.FieldOrder("code", "error_buf")
198
+ internal open class RustCallStatus : Structure() {
199
+ @JvmField var code: Byte = 0
200
+ @JvmField var error_buf: RustBuffer.ByValue = RustBuffer.ByValue()
201
+
202
+ class ByValue: RustCallStatus(), Structure.ByValue
203
+
204
+ fun isSuccess(): Boolean {
205
+ return code == 0.toByte()
206
+ }
207
+
208
+ fun isError(): Boolean {
209
+ return code == 1.toByte()
210
+ }
211
+
212
+ fun isPanic(): Boolean {
213
+ return code == 2.toByte()
214
+ }
215
+ }
216
+
217
+ class InternalException(message: String) : Exception(message)
218
+
219
+ // Each top-level error class has a companion object that can lift the error from the call status's rust buffer
220
+ interface CallStatusErrorHandler<E> {
221
+ fun lift(error_buf: RustBuffer.ByValue): E;
222
+ }
223
+
224
+ // Helpers for calling Rust
225
+ // In practice we usually need to be synchronized to call this safely, so it doesn't
226
+ // synchronize itself
227
+
228
+ // Call a rust function that returns a Result<>. Pass in the Error class companion that corresponds to the Err
229
+ private inline fun <U, E: Exception> rustCallWithError(errorHandler: CallStatusErrorHandler<E>, callback: (RustCallStatus) -> U): U {
230
+ var status = RustCallStatus();
231
+ val return_value = callback(status)
232
+ checkCallStatus(errorHandler, status)
233
+ return return_value
234
+ }
235
+
236
+ // Check RustCallStatus and throw an error if the call wasn't successful
237
+ private fun<E: Exception> checkCallStatus(errorHandler: CallStatusErrorHandler<E>, status: RustCallStatus) {
238
+ if (status.isSuccess()) {
239
+ return
240
+ } else if (status.isError()) {
241
+ throw errorHandler.lift(status.error_buf)
242
+ } else if (status.isPanic()) {
243
+ // when the rust code sees a panic, it tries to construct a rustbuffer
244
+ // with the message. but if that code panics, then it just sends back
245
+ // an empty buffer.
246
+ if (status.error_buf.len > 0) {
247
+ throw InternalException(FfiConverterString.lift(status.error_buf))
248
+ } else {
249
+ throw InternalException("Rust panic")
250
+ }
251
+ } else {
252
+ throw InternalException("Unknown rust call status: $status.code")
253
+ }
254
+ }
255
+
256
+ // CallStatusErrorHandler implementation for times when we don't expect a CALL_ERROR
257
+ object NullCallStatusErrorHandler: CallStatusErrorHandler<InternalException> {
258
+ override fun lift(error_buf: RustBuffer.ByValue): InternalException {
259
+ RustBuffer.free(error_buf)
260
+ return InternalException("Unexpected CALL_ERROR")
261
+ }
262
+ }
263
+
264
+ // Call a rust function that returns a plain value
265
+ private inline fun <U> rustCall(callback: (RustCallStatus) -> U): U {
266
+ return rustCallWithError(NullCallStatusErrorHandler, callback);
267
+ }
268
+
269
+ // IntegerType that matches Rust's `usize` / C's `size_t`
270
+ public class USize(value: Long = 0) : IntegerType(Native.SIZE_T_SIZE, value, true) {
271
+ // This is needed to fill in the gaps of IntegerType's implementation of Number for Kotlin.
272
+ override fun toByte() = toInt().toByte()
273
+ // Needed until https://youtrack.jetbrains.com/issue/KT-47902 is fixed.
274
+ @Deprecated("`toInt().toChar()` is deprecated")
275
+ override fun toChar() = toInt().toChar()
276
+ override fun toShort() = toInt().toShort()
277
+
278
+ fun writeToBuffer(buf: ByteBuffer) {
279
+ // Make sure we always write usize integers using native byte-order, since they may be
280
+ // casted to pointer values
281
+ buf.order(ByteOrder.nativeOrder())
282
+ try {
283
+ when (Native.SIZE_T_SIZE) {
284
+ 4 -> buf.putInt(toInt())
285
+ 8 -> buf.putLong(toLong())
286
+ else -> throw RuntimeException("Invalid SIZE_T_SIZE: ${Native.SIZE_T_SIZE}")
287
+ }
288
+ } finally {
289
+ buf.order(ByteOrder.BIG_ENDIAN)
290
+ }
291
+ }
292
+
293
+ companion object {
294
+ val size: Int
295
+ get() = Native.SIZE_T_SIZE
296
+
297
+ fun readFromBuffer(buf: ByteBuffer) : USize {
298
+ // Make sure we always read usize integers using native byte-order, since they may be
299
+ // casted from pointer values
300
+ buf.order(ByteOrder.nativeOrder())
301
+ try {
302
+ return when (Native.SIZE_T_SIZE) {
303
+ 4 -> USize(buf.getInt().toLong())
304
+ 8 -> USize(buf.getLong())
305
+ else -> throw RuntimeException("Invalid SIZE_T_SIZE: ${Native.SIZE_T_SIZE}")
306
+ }
307
+ } finally {
308
+ buf.order(ByteOrder.BIG_ENDIAN)
309
+ }
310
+ }
311
+ }
312
+ }
313
+
314
+
315
+ // Map handles to objects
316
+ //
317
+ // This is used when the Rust code expects an opaque pointer to represent some foreign object.
318
+ // Normally we would pass a pointer to the object, but JNA doesn't support getting a pointer from an
319
+ // object reference , nor does it support leaking a reference to Rust.
320
+ //
321
+ // Instead, this class maps USize values to objects so that we can pass a pointer-sized type to
322
+ // Rust when it needs an opaque pointer.
323
+ //
324
+ // TODO: refactor callbacks to use this class
325
+ internal class UniFfiHandleMap<T: Any> {
326
+ private val map = ConcurrentHashMap<USize, T>()
327
+ // Use AtomicInteger for our counter, since we may be on a 32-bit system. 4 billion possible
328
+ // values seems like enough. If somehow we generate 4 billion handles, then this will wrap
329
+ // around back to zero and we can assume the first handle generated will have been dropped by
330
+ // then.
331
+ private val counter = java.util.concurrent.atomic.AtomicInteger(0)
332
+
333
+ val size: Int
334
+ get() = map.size
335
+
336
+ fun insert(obj: T): USize {
337
+ val handle = USize(counter.getAndAdd(1).toLong())
338
+ map.put(handle, obj)
339
+ return handle
340
+ }
341
+
342
+ fun get(handle: USize): T? {
343
+ return map.get(handle)
344
+ }
345
+
346
+ fun remove(handle: USize): T? {
347
+ return map.remove(handle)
348
+ }
349
+ }
350
+
351
+ // FFI type for Rust future continuations
352
+ internal interface UniFffiRustFutureContinuationCallbackType : com.sun.jna.Callback {
353
+ fun callback(continuationHandle: USize, pollResult: Short);
354
+ }
355
+
356
+ // Contains loading, initialization code,
357
+ // and the FFI Function declarations in a com.sun.jna.Library.
358
+ @Synchronized
359
+ private fun findLibraryName(componentName: String): String {
360
+ val libOverride = System.getProperty("uniffi.component.$componentName.libraryOverride")
361
+ if (libOverride != null) {
362
+ return libOverride
363
+ }
364
+ return "mobile"
365
+ }
366
+
367
+ private inline fun <reified Lib : Library> loadIndirect(
368
+ componentName: String
369
+ ): Lib {
370
+ return Native.load<Lib>(findLibraryName(componentName), Lib::class.java)
371
+ }
372
+
373
+ // A JNA Library to expose the extern-C FFI definitions.
374
+ // This is an implementation detail which will be called internally by the public API.
375
+
376
+ internal interface _UniFFILib : Library {
377
+ companion object {
378
+ internal val INSTANCE: _UniFFILib by lazy {
379
+ loadIndirect<_UniFFILib>(componentName = "mobile")
380
+ .also { lib: _UniFFILib ->
381
+ uniffiCheckContractApiVersion(lib)
382
+ uniffiCheckApiChecksums(lib)
383
+ uniffiRustFutureContinuationCallback.register(lib)
384
+ }
385
+ }
386
+ }
387
+
388
+ fun uniffi_mobile_fn_func_auth(`url`: RustBuffer.ByValue,`secretKey`: RustBuffer.ByValue,
389
+ ): Pointer
390
+ fun uniffi_mobile_fn_func_myexample(_uniffi_out_err: RustCallStatus,
391
+ ): RustBuffer.ByValue
392
+ fun ffi_mobile_rustbuffer_alloc(`size`: Int,_uniffi_out_err: RustCallStatus,
393
+ ): RustBuffer.ByValue
394
+ fun ffi_mobile_rustbuffer_from_bytes(`bytes`: ForeignBytes.ByValue,_uniffi_out_err: RustCallStatus,
395
+ ): RustBuffer.ByValue
396
+ fun ffi_mobile_rustbuffer_free(`buf`: RustBuffer.ByValue,_uniffi_out_err: RustCallStatus,
397
+ ): Unit
398
+ fun ffi_mobile_rustbuffer_reserve(`buf`: RustBuffer.ByValue,`additional`: Int,_uniffi_out_err: RustCallStatus,
399
+ ): RustBuffer.ByValue
400
+ fun ffi_mobile_rust_future_continuation_callback_set(`callback`: UniFffiRustFutureContinuationCallbackType,
401
+ ): Unit
402
+ fun ffi_mobile_rust_future_poll_u8(`handle`: Pointer,`uniffiCallback`: USize,
403
+ ): Unit
404
+ fun ffi_mobile_rust_future_cancel_u8(`handle`: Pointer,
405
+ ): Unit
406
+ fun ffi_mobile_rust_future_free_u8(`handle`: Pointer,
407
+ ): Unit
408
+ fun ffi_mobile_rust_future_complete_u8(`handle`: Pointer,_uniffi_out_err: RustCallStatus,
409
+ ): Byte
410
+ fun ffi_mobile_rust_future_poll_i8(`handle`: Pointer,`uniffiCallback`: USize,
411
+ ): Unit
412
+ fun ffi_mobile_rust_future_cancel_i8(`handle`: Pointer,
413
+ ): Unit
414
+ fun ffi_mobile_rust_future_free_i8(`handle`: Pointer,
415
+ ): Unit
416
+ fun ffi_mobile_rust_future_complete_i8(`handle`: Pointer,_uniffi_out_err: RustCallStatus,
417
+ ): Byte
418
+ fun ffi_mobile_rust_future_poll_u16(`handle`: Pointer,`uniffiCallback`: USize,
419
+ ): Unit
420
+ fun ffi_mobile_rust_future_cancel_u16(`handle`: Pointer,
421
+ ): Unit
422
+ fun ffi_mobile_rust_future_free_u16(`handle`: Pointer,
423
+ ): Unit
424
+ fun ffi_mobile_rust_future_complete_u16(`handle`: Pointer,_uniffi_out_err: RustCallStatus,
425
+ ): Short
426
+ fun ffi_mobile_rust_future_poll_i16(`handle`: Pointer,`uniffiCallback`: USize,
427
+ ): Unit
428
+ fun ffi_mobile_rust_future_cancel_i16(`handle`: Pointer,
429
+ ): Unit
430
+ fun ffi_mobile_rust_future_free_i16(`handle`: Pointer,
431
+ ): Unit
432
+ fun ffi_mobile_rust_future_complete_i16(`handle`: Pointer,_uniffi_out_err: RustCallStatus,
433
+ ): Short
434
+ fun ffi_mobile_rust_future_poll_u32(`handle`: Pointer,`uniffiCallback`: USize,
435
+ ): Unit
436
+ fun ffi_mobile_rust_future_cancel_u32(`handle`: Pointer,
437
+ ): Unit
438
+ fun ffi_mobile_rust_future_free_u32(`handle`: Pointer,
439
+ ): Unit
440
+ fun ffi_mobile_rust_future_complete_u32(`handle`: Pointer,_uniffi_out_err: RustCallStatus,
441
+ ): Int
442
+ fun ffi_mobile_rust_future_poll_i32(`handle`: Pointer,`uniffiCallback`: USize,
443
+ ): Unit
444
+ fun ffi_mobile_rust_future_cancel_i32(`handle`: Pointer,
445
+ ): Unit
446
+ fun ffi_mobile_rust_future_free_i32(`handle`: Pointer,
447
+ ): Unit
448
+ fun ffi_mobile_rust_future_complete_i32(`handle`: Pointer,_uniffi_out_err: RustCallStatus,
449
+ ): Int
450
+ fun ffi_mobile_rust_future_poll_u64(`handle`: Pointer,`uniffiCallback`: USize,
451
+ ): Unit
452
+ fun ffi_mobile_rust_future_cancel_u64(`handle`: Pointer,
453
+ ): Unit
454
+ fun ffi_mobile_rust_future_free_u64(`handle`: Pointer,
455
+ ): Unit
456
+ fun ffi_mobile_rust_future_complete_u64(`handle`: Pointer,_uniffi_out_err: RustCallStatus,
457
+ ): Long
458
+ fun ffi_mobile_rust_future_poll_i64(`handle`: Pointer,`uniffiCallback`: USize,
459
+ ): Unit
460
+ fun ffi_mobile_rust_future_cancel_i64(`handle`: Pointer,
461
+ ): Unit
462
+ fun ffi_mobile_rust_future_free_i64(`handle`: Pointer,
463
+ ): Unit
464
+ fun ffi_mobile_rust_future_complete_i64(`handle`: Pointer,_uniffi_out_err: RustCallStatus,
465
+ ): Long
466
+ fun ffi_mobile_rust_future_poll_f32(`handle`: Pointer,`uniffiCallback`: USize,
467
+ ): Unit
468
+ fun ffi_mobile_rust_future_cancel_f32(`handle`: Pointer,
469
+ ): Unit
470
+ fun ffi_mobile_rust_future_free_f32(`handle`: Pointer,
471
+ ): Unit
472
+ fun ffi_mobile_rust_future_complete_f32(`handle`: Pointer,_uniffi_out_err: RustCallStatus,
473
+ ): Float
474
+ fun ffi_mobile_rust_future_poll_f64(`handle`: Pointer,`uniffiCallback`: USize,
475
+ ): Unit
476
+ fun ffi_mobile_rust_future_cancel_f64(`handle`: Pointer,
477
+ ): Unit
478
+ fun ffi_mobile_rust_future_free_f64(`handle`: Pointer,
479
+ ): Unit
480
+ fun ffi_mobile_rust_future_complete_f64(`handle`: Pointer,_uniffi_out_err: RustCallStatus,
481
+ ): Double
482
+ fun ffi_mobile_rust_future_poll_pointer(`handle`: Pointer,`uniffiCallback`: USize,
483
+ ): Unit
484
+ fun ffi_mobile_rust_future_cancel_pointer(`handle`: Pointer,
485
+ ): Unit
486
+ fun ffi_mobile_rust_future_free_pointer(`handle`: Pointer,
487
+ ): Unit
488
+ fun ffi_mobile_rust_future_complete_pointer(`handle`: Pointer,_uniffi_out_err: RustCallStatus,
489
+ ): Pointer
490
+ fun ffi_mobile_rust_future_poll_rust_buffer(`handle`: Pointer,`uniffiCallback`: USize,
491
+ ): Unit
492
+ fun ffi_mobile_rust_future_cancel_rust_buffer(`handle`: Pointer,
493
+ ): Unit
494
+ fun ffi_mobile_rust_future_free_rust_buffer(`handle`: Pointer,
495
+ ): Unit
496
+ fun ffi_mobile_rust_future_complete_rust_buffer(`handle`: Pointer,_uniffi_out_err: RustCallStatus,
497
+ ): RustBuffer.ByValue
498
+ fun ffi_mobile_rust_future_poll_void(`handle`: Pointer,`uniffiCallback`: USize,
499
+ ): Unit
500
+ fun ffi_mobile_rust_future_cancel_void(`handle`: Pointer,
501
+ ): Unit
502
+ fun ffi_mobile_rust_future_free_void(`handle`: Pointer,
503
+ ): Unit
504
+ fun ffi_mobile_rust_future_complete_void(`handle`: Pointer,_uniffi_out_err: RustCallStatus,
505
+ ): Unit
506
+ fun uniffi_mobile_checksum_func_auth(
507
+ ): Short
508
+ fun uniffi_mobile_checksum_func_myexample(
509
+ ): Short
510
+ fun ffi_mobile_uniffi_contract_version(
511
+ ): Int
512
+
513
+ }
514
+
515
+ private fun uniffiCheckContractApiVersion(lib: _UniFFILib) {
516
+ // Get the bindings contract version from our ComponentInterface
517
+ val bindings_contract_version = 24
518
+ // Get the scaffolding contract version by calling the into the dylib
519
+ val scaffolding_contract_version = lib.ffi_mobile_uniffi_contract_version()
520
+ if (bindings_contract_version != scaffolding_contract_version) {
521
+ throw RuntimeException("UniFFI contract version mismatch: try cleaning and rebuilding your project")
522
+ }
523
+ }
524
+
525
+ @Suppress("UNUSED_PARAMETER")
526
+ private fun uniffiCheckApiChecksums(lib: _UniFFILib) {
527
+ if (lib.uniffi_mobile_checksum_func_auth() != 55720.toShort()) {
528
+ throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
529
+ }
530
+ if (lib.uniffi_mobile_checksum_func_myexample() != 65225.toShort()) {
531
+ throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
532
+ }
533
+ }
534
+
535
+ // Async support
536
+ // Async return type handlers
537
+
538
+ internal const val UNIFFI_RUST_FUTURE_POLL_READY = 0.toShort()
539
+ internal const val UNIFFI_RUST_FUTURE_POLL_MAYBE_READY = 1.toShort()
540
+
541
+ internal val uniffiContinuationHandleMap = UniFfiHandleMap<CancellableContinuation<Short>>()
542
+
543
+ // FFI type for Rust future continuations
544
+ internal object uniffiRustFutureContinuationCallback: UniFffiRustFutureContinuationCallbackType {
545
+ override fun callback(continuationHandle: USize, pollResult: Short) {
546
+ uniffiContinuationHandleMap.remove(continuationHandle)?.resume(pollResult)
547
+ }
548
+
549
+ internal fun register(lib: _UniFFILib) {
550
+ lib.ffi_mobile_rust_future_continuation_callback_set(this)
551
+ }
552
+ }
553
+
554
+ internal suspend fun<T, F, E: Exception> uniffiRustCallAsync(
555
+ rustFuture: Pointer,
556
+ pollFunc: (Pointer, USize) -> Unit,
557
+ completeFunc: (Pointer, RustCallStatus) -> F,
558
+ freeFunc: (Pointer) -> Unit,
559
+ liftFunc: (F) -> T,
560
+ errorHandler: CallStatusErrorHandler<E>
561
+ ): T {
562
+ try {
563
+ do {
564
+ val pollResult = suspendCancellableCoroutine<Short> { continuation ->
565
+ pollFunc(
566
+ rustFuture,
567
+ uniffiContinuationHandleMap.insert(continuation)
568
+ )
569
+ }
570
+ } while (pollResult != UNIFFI_RUST_FUTURE_POLL_READY);
571
+
572
+ return liftFunc(
573
+ rustCallWithError(errorHandler, { status -> completeFunc(rustFuture, status) })
574
+ )
575
+ } finally {
576
+ freeFunc(rustFuture)
577
+ }
578
+ }
579
+
580
+
581
+ // Public interface members begin here.
582
+
583
+
584
+ public object FfiConverterString: FfiConverter<String, RustBuffer.ByValue> {
585
+ // Note: we don't inherit from FfiConverterRustBuffer, because we use a
586
+ // special encoding when lowering/lifting. We can use `RustBuffer.len` to
587
+ // store our length and avoid writing it out to the buffer.
588
+ override fun lift(value: RustBuffer.ByValue): String {
589
+ try {
590
+ val byteArr = ByteArray(value.len)
591
+ value.asByteBuffer()!!.get(byteArr)
592
+ return byteArr.toString(Charsets.UTF_8)
593
+ } finally {
594
+ RustBuffer.free(value)
595
+ }
596
+ }
597
+
598
+ override fun read(buf: ByteBuffer): String {
599
+ val len = buf.getInt()
600
+ val byteArr = ByteArray(len)
601
+ buf.get(byteArr)
602
+ return byteArr.toString(Charsets.UTF_8)
603
+ }
604
+
605
+ fun toUtf8(value: String): ByteBuffer {
606
+ // Make sure we don't have invalid UTF-16, check for lone surrogates.
607
+ return Charsets.UTF_8.newEncoder().run {
608
+ onMalformedInput(CodingErrorAction.REPORT)
609
+ encode(CharBuffer.wrap(value))
610
+ }
611
+ }
612
+
613
+ override fun lower(value: String): RustBuffer.ByValue {
614
+ val byteBuf = toUtf8(value)
615
+ // Ideally we'd pass these bytes to `ffi_bytebuffer_from_bytes`, but doing so would require us
616
+ // to copy them into a JNA `Memory`. So we might as well directly copy them into a `RustBuffer`.
617
+ val rbuf = RustBuffer.alloc(byteBuf.limit())
618
+ rbuf.asByteBuffer()!!.put(byteBuf)
619
+ return rbuf
620
+ }
621
+
622
+ // We aren't sure exactly how many bytes our string will be once it's UTF-8
623
+ // encoded. Allocate 3 bytes per UTF-16 code unit which will always be
624
+ // enough.
625
+ override fun allocationSize(value: String): Int {
626
+ val sizeForLength = 4
627
+ val sizeForString = value.length * 3
628
+ return sizeForLength + sizeForString
629
+ }
630
+
631
+ override fun write(value: String, buf: ByteBuffer) {
632
+ val byteBuf = toUtf8(value)
633
+ buf.putInt(byteBuf.limit())
634
+ buf.put(byteBuf)
635
+ }
636
+ }
637
+
638
+
639
+
640
+
641
+ public object FfiConverterSequenceString: FfiConverterRustBuffer<List<String>> {
642
+ override fun read(buf: ByteBuffer): List<String> {
643
+ val len = buf.getInt()
644
+ return List<String>(len) {
645
+ FfiConverterString.read(buf)
646
+ }
647
+ }
648
+
649
+ override fun allocationSize(value: List<String>): Int {
650
+ val sizeForLength = 4
651
+ val sizeForItems = value.map { FfiConverterString.allocationSize(it) }.sum()
652
+ return sizeForLength + sizeForItems
653
+ }
654
+
655
+ override fun write(value: List<String>, buf: ByteBuffer) {
656
+ buf.putInt(value.size)
657
+ value.forEach {
658
+ FfiConverterString.write(it, buf)
659
+ }
660
+ }
661
+ }
662
+
663
+
664
+
665
+
666
+
667
+ @Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE")
668
+ suspend fun `auth`(`url`: String, `secretKey`: String) : List<String> {
669
+ return uniffiRustCallAsync(
670
+ _UniFFILib.INSTANCE.uniffi_mobile_fn_func_auth(FfiConverterString.lower(`url`),FfiConverterString.lower(`secretKey`),),
671
+ { future, continuation -> _UniFFILib.INSTANCE.ffi_mobile_rust_future_poll_rust_buffer(future, continuation) },
672
+ { future, continuation -> _UniFFILib.INSTANCE.ffi_mobile_rust_future_complete_rust_buffer(future, continuation) },
673
+ { future -> _UniFFILib.INSTANCE.ffi_mobile_rust_future_free_rust_buffer(future) },
674
+ // lift function
675
+ { FfiConverterSequenceString.lift(it) },
676
+ // Error FFI converter
677
+ NullCallStatusErrorHandler,
678
+ )
679
+ }
680
+
681
+ fun `myexample`(): List<String> {
682
+ return FfiConverterSequenceString.lift(
683
+ rustCall() { _status ->
684
+ _UniFFILib.INSTANCE.uniffi_mobile_fn_func_myexample(_status)
685
+ })
686
+ }
687
+
688
+