mirror of
https://github.com/aljazceru/react-native-pubky.git
synced 2025-12-17 22:54:29 +01:00
test: async fix
Updates async functions in lib.rs. Updates example app. Update examples in README.md. Add sign-up/in/out examples to README.md. Bumps version to 0.7.1
This commit is contained in:
@@ -356,68 +356,6 @@ fileprivate struct FfiConverterSequenceString: FfiConverterRustBuffer {
|
||||
return seq
|
||||
}
|
||||
}
|
||||
private let UNIFFI_RUST_FUTURE_POLL_READY: Int8 = 0
|
||||
private let UNIFFI_RUST_FUTURE_POLL_MAYBE_READY: Int8 = 1
|
||||
|
||||
fileprivate func uniffiRustCallAsync<F, T>(
|
||||
rustFutureFunc: () -> UnsafeMutableRawPointer,
|
||||
pollFunc: (UnsafeMutableRawPointer, UnsafeMutableRawPointer) -> (),
|
||||
completeFunc: (UnsafeMutableRawPointer, UnsafeMutablePointer<RustCallStatus>) -> F,
|
||||
freeFunc: (UnsafeMutableRawPointer) -> (),
|
||||
liftFunc: (F) throws -> T,
|
||||
errorHandler: ((RustBuffer) throws -> Error)?
|
||||
) async throws -> T {
|
||||
// Make sure to call uniffiEnsureInitialized() since future creation doesn't have a
|
||||
// RustCallStatus param, so doesn't use makeRustCall()
|
||||
uniffiEnsureInitialized()
|
||||
let rustFuture = rustFutureFunc()
|
||||
defer {
|
||||
freeFunc(rustFuture)
|
||||
}
|
||||
var pollResult: Int8;
|
||||
repeat {
|
||||
pollResult = await withUnsafeContinuation {
|
||||
pollFunc(rustFuture, ContinuationHolder($0).toOpaque())
|
||||
}
|
||||
} while pollResult != UNIFFI_RUST_FUTURE_POLL_READY
|
||||
|
||||
return try liftFunc(makeRustCall(
|
||||
{ completeFunc(rustFuture, $0) },
|
||||
errorHandler: errorHandler
|
||||
))
|
||||
}
|
||||
|
||||
// Callback handlers for an async calls. These are invoked by Rust when the future is ready. They
|
||||
// lift the return value or error and resume the suspended function.
|
||||
fileprivate func uniffiFutureContinuationCallback(ptr: UnsafeMutableRawPointer, pollResult: Int8) {
|
||||
ContinuationHolder.fromOpaque(ptr).resume(pollResult)
|
||||
}
|
||||
|
||||
// Wraps UnsafeContinuation in a class so that we can use reference counting when passing it across
|
||||
// the FFI
|
||||
fileprivate class ContinuationHolder {
|
||||
let continuation: UnsafeContinuation<Int8, Never>
|
||||
|
||||
init(_ continuation: UnsafeContinuation<Int8, Never>) {
|
||||
self.continuation = continuation
|
||||
}
|
||||
|
||||
func resume(_ pollResult: Int8) {
|
||||
self.continuation.resume(returning: pollResult)
|
||||
}
|
||||
|
||||
func toOpaque() -> UnsafeMutableRawPointer {
|
||||
return Unmanaged<ContinuationHolder>.passRetained(self).toOpaque()
|
||||
}
|
||||
|
||||
static func fromOpaque(_ ptr: UnsafeRawPointer) -> ContinuationHolder {
|
||||
return Unmanaged<ContinuationHolder>.fromOpaque(ptr).takeRetainedValue()
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func uniffiInitContinuationCallback() {
|
||||
ffi_pubkymobile_rust_future_continuation_callback_set(uniffiFutureContinuationCallback)
|
||||
}
|
||||
|
||||
public func auth(url: String, secretKey: String) -> [String] {
|
||||
return try! FfiConverterSequenceString.lift(
|
||||
@@ -437,24 +375,15 @@ public func generateSecretKey() -> [String] {
|
||||
)
|
||||
}
|
||||
|
||||
public func get(url: String) async -> [String] {
|
||||
return try! await uniffiRustCallAsync(
|
||||
rustFutureFunc: {
|
||||
uniffi_pubkymobile_fn_func_get(
|
||||
FfiConverterString.lower(url)
|
||||
)
|
||||
},
|
||||
pollFunc: ffi_pubkymobile_rust_future_poll_rust_buffer,
|
||||
completeFunc: ffi_pubkymobile_rust_future_complete_rust_buffer,
|
||||
freeFunc: ffi_pubkymobile_rust_future_free_rust_buffer,
|
||||
liftFunc: FfiConverterSequenceString.lift,
|
||||
errorHandler: nil
|
||||
|
||||
public func get(url: String) -> [String] {
|
||||
return try! FfiConverterSequenceString.lift(
|
||||
try! rustCall() {
|
||||
uniffi_pubkymobile_fn_func_get(
|
||||
FfiConverterString.lower(url),$0)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
public func getPublicKeyFromSecretKey(secretKey: String) -> [String] {
|
||||
return try! FfiConverterSequenceString.lift(
|
||||
try! rustCall() {
|
||||
@@ -504,25 +433,16 @@ public func publishHttps(recordName: String, target: String, secretKey: String)
|
||||
)
|
||||
}
|
||||
|
||||
public func put(url: String, content: String) async -> [String] {
|
||||
return try! await uniffiRustCallAsync(
|
||||
rustFutureFunc: {
|
||||
uniffi_pubkymobile_fn_func_put(
|
||||
FfiConverterString.lower(url),
|
||||
FfiConverterString.lower(content)
|
||||
)
|
||||
},
|
||||
pollFunc: ffi_pubkymobile_rust_future_poll_rust_buffer,
|
||||
completeFunc: ffi_pubkymobile_rust_future_complete_rust_buffer,
|
||||
freeFunc: ffi_pubkymobile_rust_future_free_rust_buffer,
|
||||
liftFunc: FfiConverterSequenceString.lift,
|
||||
errorHandler: nil
|
||||
|
||||
public func put(url: String, content: String) -> [String] {
|
||||
return try! FfiConverterSequenceString.lift(
|
||||
try! rustCall() {
|
||||
uniffi_pubkymobile_fn_func_put(
|
||||
FfiConverterString.lower(url),
|
||||
FfiConverterString.lower(content),$0)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
public func resolve(publicKey: String) -> [String] {
|
||||
return try! FfiConverterSequenceString.lift(
|
||||
try! rustCall() {
|
||||
@@ -541,61 +461,34 @@ public func resolveHttps(publicKey: String) -> [String] {
|
||||
)
|
||||
}
|
||||
|
||||
public func signIn(secretKey: String) async -> [String] {
|
||||
return try! await uniffiRustCallAsync(
|
||||
rustFutureFunc: {
|
||||
uniffi_pubkymobile_fn_func_sign_in(
|
||||
FfiConverterString.lower(secretKey)
|
||||
)
|
||||
},
|
||||
pollFunc: ffi_pubkymobile_rust_future_poll_rust_buffer,
|
||||
completeFunc: ffi_pubkymobile_rust_future_complete_rust_buffer,
|
||||
freeFunc: ffi_pubkymobile_rust_future_free_rust_buffer,
|
||||
liftFunc: FfiConverterSequenceString.lift,
|
||||
errorHandler: nil
|
||||
|
||||
public func signIn(secretKey: String) -> [String] {
|
||||
return try! FfiConverterSequenceString.lift(
|
||||
try! rustCall() {
|
||||
uniffi_pubkymobile_fn_func_sign_in(
|
||||
FfiConverterString.lower(secretKey),$0)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
public func signOut(secretKey: String) async -> [String] {
|
||||
return try! await uniffiRustCallAsync(
|
||||
rustFutureFunc: {
|
||||
uniffi_pubkymobile_fn_func_sign_out(
|
||||
FfiConverterString.lower(secretKey)
|
||||
)
|
||||
},
|
||||
pollFunc: ffi_pubkymobile_rust_future_poll_rust_buffer,
|
||||
completeFunc: ffi_pubkymobile_rust_future_complete_rust_buffer,
|
||||
freeFunc: ffi_pubkymobile_rust_future_free_rust_buffer,
|
||||
liftFunc: FfiConverterSequenceString.lift,
|
||||
errorHandler: nil
|
||||
|
||||
public func signOut(secretKey: String) -> [String] {
|
||||
return try! FfiConverterSequenceString.lift(
|
||||
try! rustCall() {
|
||||
uniffi_pubkymobile_fn_func_sign_out(
|
||||
FfiConverterString.lower(secretKey),$0)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
public func signUp(secretKey: String, homeserver: String) async -> [String] {
|
||||
return try! await uniffiRustCallAsync(
|
||||
rustFutureFunc: {
|
||||
uniffi_pubkymobile_fn_func_sign_up(
|
||||
FfiConverterString.lower(secretKey),
|
||||
FfiConverterString.lower(homeserver)
|
||||
)
|
||||
},
|
||||
pollFunc: ffi_pubkymobile_rust_future_poll_rust_buffer,
|
||||
completeFunc: ffi_pubkymobile_rust_future_complete_rust_buffer,
|
||||
freeFunc: ffi_pubkymobile_rust_future_free_rust_buffer,
|
||||
liftFunc: FfiConverterSequenceString.lift,
|
||||
errorHandler: nil
|
||||
|
||||
public func signUp(secretKey: String, homeserver: String) -> [String] {
|
||||
return try! FfiConverterSequenceString.lift(
|
||||
try! rustCall() {
|
||||
uniffi_pubkymobile_fn_func_sign_up(
|
||||
FfiConverterString.lower(secretKey),
|
||||
FfiConverterString.lower(homeserver),$0)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
private enum InitializationResult {
|
||||
case ok
|
||||
case contractVersionMismatch
|
||||
@@ -617,7 +510,7 @@ private var initializationResult: InitializationResult {
|
||||
if (uniffi_pubkymobile_checksum_func_generate_secret_key() != 63116) {
|
||||
return InitializationResult.apiChecksumMismatch
|
||||
}
|
||||
if (uniffi_pubkymobile_checksum_func_get() != 5395) {
|
||||
if (uniffi_pubkymobile_checksum_func_get() != 21596) {
|
||||
return InitializationResult.apiChecksumMismatch
|
||||
}
|
||||
if (uniffi_pubkymobile_checksum_func_get_public_key_from_secret_key() != 23603) {
|
||||
@@ -635,7 +528,7 @@ private var initializationResult: InitializationResult {
|
||||
if (uniffi_pubkymobile_checksum_func_publish_https() != 14705) {
|
||||
return InitializationResult.apiChecksumMismatch
|
||||
}
|
||||
if (uniffi_pubkymobile_checksum_func_put() != 47594) {
|
||||
if (uniffi_pubkymobile_checksum_func_put() != 51107) {
|
||||
return InitializationResult.apiChecksumMismatch
|
||||
}
|
||||
if (uniffi_pubkymobile_checksum_func_resolve() != 18303) {
|
||||
@@ -644,17 +537,16 @@ private var initializationResult: InitializationResult {
|
||||
if (uniffi_pubkymobile_checksum_func_resolve_https() != 34593) {
|
||||
return InitializationResult.apiChecksumMismatch
|
||||
}
|
||||
if (uniffi_pubkymobile_checksum_func_sign_in() != 53969) {
|
||||
if (uniffi_pubkymobile_checksum_func_sign_in() != 21006) {
|
||||
return InitializationResult.apiChecksumMismatch
|
||||
}
|
||||
if (uniffi_pubkymobile_checksum_func_sign_out() != 32961) {
|
||||
if (uniffi_pubkymobile_checksum_func_sign_out() != 59116) {
|
||||
return InitializationResult.apiChecksumMismatch
|
||||
}
|
||||
if (uniffi_pubkymobile_checksum_func_sign_up() != 28083) {
|
||||
if (uniffi_pubkymobile_checksum_func_sign_up() != 58756) {
|
||||
return InitializationResult.apiChecksumMismatch
|
||||
}
|
||||
|
||||
uniffiInitContinuationCallback()
|
||||
return InitializationResult.ok
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user