| 1 |
{%- let trait_impl=format!("_UniffiTraitImpl{}", name) %} |
| 2 |
|
| 3 |
# Put all the bits inside a class to keep the top-level namespace clean |
| 4 |
class {{ trait_impl }}: |
| 5 |
# For each method, generate a callback function to pass to Rust |
| 6 |
{%- for (ffi_callback, meth) in vtable_methods.iter() %} |
| 7 |
|
| 8 |
@{{ ffi_callback.name()|ffi_callback_name }} |
| 9 |
def {{ meth.name() }}( |
| 10 |
{%- for arg in ffi_callback.arguments() %} |
| 11 |
{{ arg.name() }}, |
| 12 |
{%- endfor -%} |
| 13 |
{%- if ffi_callback.has_rust_call_status_arg() %} |
| 14 |
uniffi_call_status_ptr, |
| 15 |
{%- endif %} |
| 16 |
): |
| 17 |
uniffi_obj = {{ ffi_converter_name }}._handle_map.get(uniffi_handle) |
| 18 |
def make_call(): |
| 19 |
args = ({% for arg in meth.arguments() %}{{ arg|lift_fn }}({{ arg.name() }}), {% endfor %}) |
| 20 |
method = uniffi_obj.{{ meth.name() }} |
| 21 |
return method(*args) |
| 22 |
|
| 23 |
{% if !meth.is_async() %} |
| 24 |
{%- match meth.return_type() %} |
| 25 |
{%- when Some(return_type) %} |
| 26 |
def write_return_value(v): |
| 27 |
uniffi_out_return[0] = {{ return_type|lower_fn }}(v) |
| 28 |
{%- when None %} |
| 29 |
write_return_value = lambda v: None |
| 30 |
{%- endmatch %} |
| 31 |
|
| 32 |
{%- match meth.throws_type() %} |
| 33 |
{%- when None %} |
| 34 |
_uniffi_trait_interface_call( |
| 35 |
uniffi_call_status_ptr.contents, |
| 36 |
make_call, |
| 37 |
write_return_value, |
| 38 |
) |
| 39 |
{%- when Some(error) %} |
| 40 |
_uniffi_trait_interface_call_with_error( |
| 41 |
uniffi_call_status_ptr.contents, |
| 42 |
make_call, |
| 43 |
write_return_value, |
| 44 |
{{ error|type_name }}, |
| 45 |
{{ error|lower_fn }}, |
| 46 |
) |
| 47 |
{%- endmatch %} |
| 48 |
{%- else %} |
| 49 |
def handle_success(return_value): |
| 50 |
uniffi_future_callback( |
| 51 |
uniffi_callback_data, |
| 52 |
{{ meth.foreign_future_ffi_result_struct().name()|ffi_struct_name }}( |
| 53 |
{%- if let Some(return_type) = meth.return_type() %} |
| 54 |
{{ return_type|lower_fn }}(return_value), |
| 55 |
{%- endif %} |
| 56 |
_UniffiRustCallStatus.default() |
| 57 |
) |
| 58 |
) |
| 59 |
|
| 60 |
def handle_error(status_code, rust_buffer): |
| 61 |
uniffi_future_callback( |
| 62 |
uniffi_callback_data, |
| 63 |
{{ meth.foreign_future_ffi_result_struct().name()|ffi_struct_name }}( |
| 64 |
{%- match meth.return_type() %} |
| 65 |
{%- when Some(return_type) %} |
| 66 |
{{ meth.return_type().map(FfiType::from)|ffi_default_value(ci) }}, |
| 67 |
{%- when None %} |
| 68 |
{%- endmatch %} |
| 69 |
_UniffiRustCallStatus(status_code, rust_buffer), |
| 70 |
) |
| 71 |
) |
| 72 |
|
| 73 |
{%- match meth.throws_type() %} |
| 74 |
{%- when None %} |
| 75 |
uniffi_out_return[0] = _uniffi_trait_interface_call_async(make_call, handle_success, handle_error) |
| 76 |
{%- when Some(error) %} |
| 77 |
uniffi_out_return[0] = _uniffi_trait_interface_call_async_with_error(make_call, handle_success, handle_error, {{ error|type_name }}, {{ error|lower_fn }}) |
| 78 |
{%- endmatch %} |
| 79 |
{%- endif %} |
| 80 |
{%- endfor %} |
| 81 |
|
| 82 |
@{{ "CallbackInterfaceFree"|ffi_callback_name }} |
| 83 |
def _uniffi_free(uniffi_handle): |
| 84 |
{{ ffi_converter_name }}._handle_map.remove(uniffi_handle) |
| 85 |
|
| 86 |
# Generate the FFI VTable. This has a field for each callback interface method. |
| 87 |
_uniffi_vtable = {{ vtable|ffi_type_name(ci) }}( |
| 88 |
{%- for (_, meth) in vtable_methods.iter() %} |
| 89 |
{{ meth.name() }}, |
| 90 |
{%- endfor %} |
| 91 |
_uniffi_free |
| 92 |
) |
| 93 |
# Send Rust a pointer to the VTable. Note: this means we need to keep the struct alive forever, |
| 94 |
# or else bad things will happen when Rust tries to access it. |
| 95 |
_UniffiLib.{{ ffi_init_callback.name() }}(ctypes.byref(_uniffi_vtable)) |