| 1 |
{%- let trait_impl=format!("uniffiCallbackInterface{}", name) %} |
| 2 |
|
| 3 |
// Put the implementation in an object so we don't pollute the top-level namespace |
| 4 |
internal object {{ trait_impl }} { |
| 5 |
{%- for (ffi_callback, meth) in vtable_methods.iter() %} |
| 6 |
internal object {{ meth.name()|var_name }}: {{ ffi_callback.name()|ffi_callback_name }} { |
| 7 |
override fun callback( |
| 8 |
{%- for arg in ffi_callback.arguments() -%} |
| 9 |
{{ arg.name().borrow()|var_name }}: {{ arg.type_().borrow()|ffi_type_name_by_value(ci) }}, |
| 10 |
{%- endfor -%} |
| 11 |
{%- if ffi_callback.has_rust_call_status_arg() -%} |
| 12 |
uniffiCallStatus: UniffiRustCallStatus, |
| 13 |
{%- endif -%} |
| 14 |
) |
| 15 |
{%- if let Some(return_type) = ffi_callback.return_type() %} |
| 16 |
: {{ return_type|ffi_type_name_by_value(ci) }}, |
| 17 |
{%- endif %} { |
| 18 |
val uniffiObj = {{ ffi_converter_name }}.handleMap.get(uniffiHandle) |
| 19 |
val makeCall = {% if meth.is_async() %}suspend {% endif %}{ -> |
| 20 |
uniffiObj.{{ meth.name()|fn_name() }}( |
| 21 |
{%- for arg in meth.arguments() %} |
| 22 |
{{ arg|lift_fn }}({{ arg.name()|var_name }}), |
| 23 |
{%- endfor %} |
| 24 |
) |
| 25 |
} |
| 26 |
{%- if !meth.is_async() %} |
| 27 |
|
| 28 |
{%- match meth.return_type() %} |
| 29 |
{%- when Some(return_type) %} |
| 30 |
val writeReturn = { value: {{ return_type|type_name(ci) }} -> uniffiOutReturn.setValue({{ return_type|lower_fn }}(value)) } |
| 31 |
{%- when None %} |
| 32 |
val writeReturn = { _: Unit -> Unit } |
| 33 |
{%- endmatch %} |
| 34 |
|
| 35 |
{%- match meth.throws_type() %} |
| 36 |
{%- when None %} |
| 37 |
uniffiTraitInterfaceCall(uniffiCallStatus, makeCall, writeReturn) |
| 38 |
{%- when Some(error_type) %} |
| 39 |
uniffiTraitInterfaceCallWithError( |
| 40 |
uniffiCallStatus, |
| 41 |
makeCall, |
| 42 |
writeReturn, |
| 43 |
{ e: {{error_type|type_name(ci) }} -> {{ error_type|lower_fn }}(e) } |
| 44 |
) |
| 45 |
{%- endmatch %} |
| 46 |
|
| 47 |
{%- else %} |
| 48 |
val uniffiHandleSuccess = { {% if meth.return_type().is_some() %}returnValue{% else %}_{% endif %}: {% match meth.return_type() %}{%- when Some(return_type) %}{{ return_type|type_name(ci) }}{%- when None %}Unit{% endmatch %} -> |
| 49 |
val uniffiResult = {{ meth.foreign_future_ffi_result_struct().name()|ffi_struct_name }}.UniffiByValue( |
| 50 |
{%- if let Some(return_type) = meth.return_type() %} |
| 51 |
{{ return_type|lower_fn }}(returnValue), |
| 52 |
{%- endif %} |
| 53 |
UniffiRustCallStatus.ByValue() |
| 54 |
) |
| 55 |
uniffiResult.write() |
| 56 |
uniffiFutureCallback.callback(uniffiCallbackData, uniffiResult) |
| 57 |
} |
| 58 |
val uniffiHandleError = { callStatus: UniffiRustCallStatus.ByValue -> |
| 59 |
uniffiFutureCallback.callback( |
| 60 |
uniffiCallbackData, |
| 61 |
{{ meth.foreign_future_ffi_result_struct().name()|ffi_struct_name }}.UniffiByValue( |
| 62 |
{%- if let Some(return_type) = meth.return_type() %} |
| 63 |
{{ return_type.into()|ffi_default_value }}, |
| 64 |
{%- endif %} |
| 65 |
callStatus, |
| 66 |
), |
| 67 |
) |
| 68 |
} |
| 69 |
|
| 70 |
uniffiOutReturn.uniffiSetValue( |
| 71 |
{%- match meth.throws_type() %} |
| 72 |
{%- when None %} |
| 73 |
uniffiTraitInterfaceCallAsync( |
| 74 |
makeCall, |
| 75 |
uniffiHandleSuccess, |
| 76 |
uniffiHandleError |
| 77 |
) |
| 78 |
{%- when Some(error_type) %} |
| 79 |
uniffiTraitInterfaceCallAsyncWithError( |
| 80 |
makeCall, |
| 81 |
uniffiHandleSuccess, |
| 82 |
uniffiHandleError, |
| 83 |
{ e: {{error_type|type_name(ci) }} -> {{ error_type|lower_fn }}(e) } |
| 84 |
) |
| 85 |
{%- endmatch %} |
| 86 |
) |
| 87 |
{%- endif %} |
| 88 |
} |
| 89 |
} |
| 90 |
{%- endfor %} |
| 91 |
|
| 92 |
internal object uniffiFree: {{ "CallbackInterfaceFree"|ffi_callback_name }} { |
| 93 |
override fun callback(handle: Long) { |
| 94 |
{{ ffi_converter_name }}.handleMap.remove(handle) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
internal var vtable = {{ vtable|ffi_type_name_by_value(ci) }}( |
| 99 |
{%- for (ffi_callback, meth) in vtable_methods.iter() %} |
| 100 |
{{ meth.name()|var_name() }}, |
| 101 |
{%- endfor %} |
| 102 |
uniffiFree, |
| 103 |
) |
| 104 |
|
| 105 |
// Registers the foreign callback with the Rust side. |
| 106 |
// This method is generated for each callback interface. |
| 107 |
internal fun register(lib: UniffiLib) { |
| 108 |
lib.{{ ffi_init_callback.name() }}(vtable) |
| 109 |
} |
| 110 |
} |