| 1 |
{%- let trait_impl=format!("UniffiCallbackInterface{}", name) %} |
| 2 |
|
| 3 |
// Put the implementation in a struct so we don't pollute the top-level namespace |
| 4 |
fileprivate struct {{ trait_impl }} { |
| 5 |
|
| 6 |
// Create the VTable using a series of closures. |
| 7 |
// Swift automatically converts these into C callback functions. |
| 8 |
// |
| 9 |
// This creates 1-element array, since this seems to be the only way to construct a const |
| 10 |
// pointer that we can pass to the Rust code. |
| 11 |
static let vtable: [{{ vtable|ffi_type_name }}] = [{{ vtable|ffi_type_name }}( |
| 12 |
{%- for (ffi_callback, meth) in vtable_methods %} |
| 13 |
{{ meth.name()|fn_name }}: { ( |
| 14 |
{%- for arg in ffi_callback.arguments() %} |
| 15 |
{{ arg.name()|var_name }}: {{ arg.type_().borrow()|ffi_type_name }}{% if !loop.last || ffi_callback.has_rust_call_status_arg() %},{% endif %} |
| 16 |
{%- endfor -%} |
| 17 |
{%- if ffi_callback.has_rust_call_status_arg() %} |
| 18 |
uniffiCallStatus: UnsafeMutablePointer<RustCallStatus> |
| 19 |
{%- endif %} |
| 20 |
) in |
| 21 |
let makeCall = { |
| 22 |
() {% if meth.is_async() %}async {% endif %}throws -> {% match meth.return_type() %}{% when Some(t) %}{{ t|type_name }}{% when None %}(){% endmatch %} in |
| 23 |
guard let uniffiObj = try? {{ ffi_converter_name }}.handleMap.get(handle: uniffiHandle) else { |
| 24 |
throw UniffiInternalError.unexpectedStaleHandle |
| 25 |
} |
| 26 |
return {% if meth.throws() %}try {% endif %}{% if meth.is_async() %}await {% endif %}uniffiObj.{{ meth.name()|fn_name }}( |
| 27 |
{%- for arg in meth.arguments() %} |
| 28 |
{% if !config.omit_argument_labels() %} {{ arg.name()|arg_name }}: {% endif %}try {{ arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %},{% endif %} |
| 29 |
{%- endfor %} |
| 30 |
) |
| 31 |
} |
| 32 |
{%- if !meth.is_async() %} |
| 33 |
|
| 34 |
{% match meth.return_type() %} |
| 35 |
{%- when Some(t) %} |
| 36 |
let writeReturn = { uniffiOutReturn.pointee = {{ t|lower_fn }}($0) } |
| 37 |
{%- when None %} |
| 38 |
let writeReturn = { () } |
| 39 |
{%- endmatch %} |
| 40 |
|
| 41 |
{%- match meth.throws_type() %} |
| 42 |
{%- when None %} |
| 43 |
uniffiTraitInterfaceCall( |
| 44 |
callStatus: uniffiCallStatus, |
| 45 |
makeCall: makeCall, |
| 46 |
writeReturn: writeReturn |
| 47 |
) |
| 48 |
{%- when Some(error_type) %} |
| 49 |
uniffiTraitInterfaceCallWithError( |
| 50 |
callStatus: uniffiCallStatus, |
| 51 |
makeCall: makeCall, |
| 52 |
writeReturn: writeReturn, |
| 53 |
lowerError: {{ error_type|lower_fn }} |
| 54 |
) |
| 55 |
{%- endmatch %} |
| 56 |
{%- else %} |
| 57 |
|
| 58 |
let uniffiHandleSuccess = { (returnValue: {{ meth.return_type()|return_type_name }}) in |
| 59 |
uniffiFutureCallback( |
| 60 |
uniffiCallbackData, |
| 61 |
{{ meth.foreign_future_ffi_result_struct().name()|ffi_struct_name }}( |
| 62 |
{%- if let Some(return_type) = meth.return_type() %} |
| 63 |
returnValue: {{ return_type|lower_fn }}(returnValue), |
| 64 |
{%- endif %} |
| 65 |
callStatus: RustCallStatus() |
| 66 |
) |
| 67 |
) |
| 68 |
} |
| 69 |
let uniffiHandleError = { (statusCode, errorBuf) in |
| 70 |
uniffiFutureCallback( |
| 71 |
uniffiCallbackData, |
| 72 |
{{ meth.foreign_future_ffi_result_struct().name()|ffi_struct_name }}( |
| 73 |
{%- if let Some(return_type) = meth.return_type() %} |
| 74 |
returnValue: {{ meth.return_type().map(FfiType::from)|ffi_default_value }}, |
| 75 |
{%- endif %} |
| 76 |
callStatus: RustCallStatus(code: statusCode, errorBuf: errorBuf) |
| 77 |
) |
| 78 |
) |
| 79 |
} |
| 80 |
|
| 81 |
{%- match meth.throws_type() %} |
| 82 |
{%- when None %} |
| 83 |
let uniffiForeignFuture = uniffiTraitInterfaceCallAsync( |
| 84 |
makeCall: makeCall, |
| 85 |
handleSuccess: uniffiHandleSuccess, |
| 86 |
handleError: uniffiHandleError |
| 87 |
) |
| 88 |
{%- when Some(error_type) %} |
| 89 |
let uniffiForeignFuture = uniffiTraitInterfaceCallAsyncWithError( |
| 90 |
makeCall: makeCall, |
| 91 |
handleSuccess: uniffiHandleSuccess, |
| 92 |
handleError: uniffiHandleError, |
| 93 |
lowerError: {{ error_type|lower_fn }} |
| 94 |
) |
| 95 |
{%- endmatch %} |
| 96 |
uniffiOutReturn.pointee = uniffiForeignFuture |
| 97 |
{%- endif %} |
| 98 |
}, |
| 99 |
{%- endfor %} |
| 100 |
uniffiFree: { (uniffiHandle: UInt64) -> () in |
| 101 |
let result = try? {{ ffi_converter_name }}.handleMap.remove(handle: uniffiHandle) |
| 102 |
if result == nil { |
| 103 |
print("Uniffi callback interface {{ name }}: handle missing in uniffiFree") |
| 104 |
} |
| 105 |
} |
| 106 |
)] |
| 107 |
} |
| 108 |
|
| 109 |
private func {{ callback_init }}() { |
| 110 |
{{ ffi_init_callback.name() }}({{ trait_impl }}.vtable) |
| 111 |
} |