uniffiRustCallAsync<T, F> function
Future<T>
uniffiRustCallAsync<T, F>(
- Pointer<
Void> rustFutureFunc(), - void pollFunc(),
- F completeFunc(),
- void freeFunc(),
- T liftFunc(
- F
- UniffiRustCallStatusErrorHandler? errorHandler,
Implementation
Future<T> uniffiRustCallAsync<T, F>(
Pointer<Void> Function() rustFutureFunc,
void Function(
Pointer<Void>,
Pointer<NativeFunction<UniffiRustFutureContinuationCallback>>,
Pointer<Void>,
)
pollFunc,
F Function(Pointer<Void>, Pointer<RustCallStatus>) completeFunc,
void Function(Pointer<Void>) freeFunc,
T Function(F) liftFunc, [
UniffiRustCallStatusErrorHandler? errorHandler,
]) async {
final rustFuture = rustFutureFunc();
final completer = Completer<int>();
final handle = _uniffiRustFutureContinuationHandles.insert(completer);
final callbackData = Pointer<Void>.fromAddress(handle);
late final NativeCallable<UniffiRustFutureContinuationCallback> callback;
void repoll() {
pollFunc(rustFuture, callback.nativeFunction, callbackData);
}
void onResponse(int data, int pollResult) {
if (pollResult == UNIFFI_RUST_FUTURE_POLL_READY) {
final readyCompleter = _uniffiRustFutureContinuationHandles.maybeRemove(
data,
);
if (readyCompleter != null && !readyCompleter.isCompleted) {
readyCompleter.complete(pollResult);
}
} else if (pollResult == UNIFFI_RUST_FUTURE_POLL_MAYBE_READY) {
repoll();
} else {
final errorCompleter = _uniffiRustFutureContinuationHandles.maybeRemove(
data,
);
if (errorCompleter != null && !errorCompleter.isCompleted) {
errorCompleter.completeError(
UniffiInternalError.panicked(
"Unexpected poll result from Rust future: \$pollResult",
),
);
}
}
}
callback = NativeCallable<UniffiRustFutureContinuationCallback>.listener(
onResponse,
);
try {
repoll();
await completer.future;
final status = calloc<RustCallStatus>();
try {
final result = completeFunc(rustFuture, status);
checkCallStatus(errorHandler ?? NullRustCallStatusErrorHandler(), status);
return liftFunc(result);
} finally {
calloc.free(status);
}
} finally {
callback.close();
_uniffiRustFutureContinuationHandles.maybeRemove(handle);
freeFunc(rustFuture);
}
}