| 1 |
{%- let obj = ci.get_object_definition(name).unwrap() %} |
| 2 |
{%- let methods = obj.methods() %} |
| 3 |
{%- let protocol_name = format!("{type_name}Protocol") %} |
| 4 |
{%- let protocol_docstring = obj.docstring() %} |
| 5 |
{%- let protocol_base_class = "typing.Protocol" %} |
| 6 |
{%- include "Protocol.py" %} |
| 7 |
|
| 8 |
{%- let impl_name %} |
| 9 |
{%- if obj.has_callback_interface() %} |
| 10 |
# {{ type_name }} is a foreign trait so treated like a callback interface, where the |
| 11 |
# primary use-case is the trait being implemented locally. |
| 12 |
# It is a base-class local implementations might subclass. |
| 13 |
{# We reuse "Protocol.py" for this, even though here we are not generating a protocol #} |
| 14 |
{%- let protocol_name = format!("{type_name}") %} |
| 15 |
{%- let protocol_base_class = "" %} |
| 16 |
{% include "Protocol.py" %} |
| 17 |
|
| 18 |
{%- let impl_name = format!("{type_name}Impl") %} |
| 19 |
# `{{ impl_name }}` is the implementation for a Rust implemented version. |
| 20 |
{%- else %} |
| 21 |
# {{ type_name }} is a Rust-only trait - it's a wrapper around a Rust implementation. |
| 22 |
{%- let impl_name = type_name.clone() %} |
| 23 |
{%- endif %} |
| 24 |
|
| 25 |
{%- if ci.is_name_used_as_error(name) %} |
| 26 |
class {{ impl_name }}(Exception): |
| 27 |
{%- else %} |
| 28 |
class {{ impl_name }}({% for t in obj.trait_impls() %}{{ t.trait_name }},{% endfor %}): |
| 29 |
{%- endif %} |
| 30 |
{%- call py::docstring(obj, 4) %} |
| 31 |
_pointer: ctypes.c_void_p |
| 32 |
|
| 33 |
{%- match obj.primary_constructor() %} |
| 34 |
{%- when Some(cons) %} |
| 35 |
{%- if cons.is_async() %} |
| 36 |
def __init__(self, *args, **kw): |
| 37 |
raise ValueError("async constructors not supported.") |
| 38 |
{%- else %} |
| 39 |
def __init__(self, {% call py::arg_list_decl(cons) -%}): |
| 40 |
{%- call py::docstring(cons, 8) %} |
| 41 |
{%- call py::setup_args_extra_indent(cons) %} |
| 42 |
self._pointer = {% call py::to_ffi_call(cons) %} |
| 43 |
{%- endif %} |
| 44 |
{%- when None %} |
| 45 |
{# no __init__ means simple construction without a pointer works, which can confuse #} |
| 46 |
def __init__(self, *args, **kwargs): |
| 47 |
raise ValueError("This class has no default constructor") |
| 48 |
{%- endmatch %} |
| 49 |
|
| 50 |
def __del__(self): |
| 51 |
# In case of partial initialization of instances. |
| 52 |
pointer = getattr(self, "_pointer", None) |
| 53 |
if pointer is not None: |
| 54 |
_uniffi_rust_call(_UniffiLib.{{ obj.ffi_object_free().name() }}, pointer) |
| 55 |
|
| 56 |
def _uniffi_clone_pointer(self): |
| 57 |
return _uniffi_rust_call(_UniffiLib.{{ obj.ffi_object_clone().name() }}, self._pointer) |
| 58 |
|
| 59 |
# Used by alternative constructors or any methods which return this type. |
| 60 |
@classmethod |
| 61 |
def _make_instance_(cls, pointer): |
| 62 |
# Lightly yucky way to bypass the usual __init__ logic |
| 63 |
# and just create a new instance with the required pointer. |
| 64 |
inst = cls.__new__(cls) |
| 65 |
inst._pointer = pointer |
| 66 |
return inst |
| 67 |
|
| 68 |
{%- for cons in obj.alternate_constructors() %} |
| 69 |
@classmethod |
| 70 |
{%- if cons.is_async() %} |
| 71 |
async def {{ cons.name() }}(cls, {% call py::arg_list_decl(cons) %}): |
| 72 |
{%- call py::docstring(cons, 8) %} |
| 73 |
{%- call py::setup_args_extra_indent(cons) %} |
| 74 |
|
| 75 |
return await _uniffi_rust_call_async( |
| 76 |
_UniffiLib.{{ cons.ffi_func().name() }}({% call py::arg_list_lowered(cons) %}), |
| 77 |
_UniffiLib.{{ cons.ffi_rust_future_poll(ci) }}, |
| 78 |
_UniffiLib.{{ cons.ffi_rust_future_complete(ci) }}, |
| 79 |
_UniffiLib.{{ cons.ffi_rust_future_free(ci) }}, |
| 80 |
{{ ffi_converter_name }}.lift, |
| 81 |
{% call py::error_ffi_converter(cons) %} |
| 82 |
) |
| 83 |
{%- else %} |
| 84 |
def {{ cons.name() }}(cls, {% call py::arg_list_decl(cons) %}): |
| 85 |
{%- call py::docstring(cons, 8) %} |
| 86 |
{%- call py::setup_args_extra_indent(cons) %} |
| 87 |
# Call the (fallible) function before creating any half-baked object instances. |
| 88 |
pointer = {% call py::to_ffi_call(cons) %} |
| 89 |
return cls._make_instance_(pointer) |
| 90 |
{%- endif %} |
| 91 |
{% endfor %} |
| 92 |
|
| 93 |
{%- for meth in obj.methods() -%} |
| 94 |
{%- call py::method_decl(meth.name(), meth) %} |
| 95 |
{%- endfor %} |
| 96 |
{%- for tm in obj.uniffi_traits() -%} |
| 97 |
{%- match tm %} |
| 98 |
{%- when UniffiTrait::Debug { fmt } %} |
| 99 |
{%- call py::method_decl("__repr__", fmt) %} |
| 100 |
{%- when UniffiTrait::Display { fmt } %} |
| 101 |
{%- call py::method_decl("__str__", fmt) %} |
| 102 |
{%- when UniffiTrait::Eq { eq, ne } %} |
| 103 |
def __eq__(self, other: object) -> {{ eq.return_type().unwrap()|type_name }}: |
| 104 |
if not isinstance(other, {{ type_name }}): |
| 105 |
return NotImplemented |
| 106 |
|
| 107 |
return {{ eq.return_type().unwrap()|lift_fn }}({% call py::to_ffi_call_with_prefix("self._uniffi_clone_pointer()", eq) %}) |
| 108 |
|
| 109 |
def __ne__(self, other: object) -> {{ ne.return_type().unwrap()|type_name }}: |
| 110 |
if not isinstance(other, {{ type_name }}): |
| 111 |
return NotImplemented |
| 112 |
|
| 113 |
return {{ ne.return_type().unwrap()|lift_fn }}({% call py::to_ffi_call_with_prefix("self._uniffi_clone_pointer()", ne) %}) |
| 114 |
{%- when UniffiTrait::Hash { hash } %} |
| 115 |
{%- call py::method_decl("__hash__", hash) %} |
| 116 |
{%- endmatch %} |
| 117 |
{%- endfor %} |
| 118 |
|
| 119 |
{%- if obj.has_callback_interface() %} |
| 120 |
{%- let ffi_init_callback = obj.ffi_init_callback() %} |
| 121 |
{%- let vtable = obj.vtable().expect("trait interface should have a vtable") %} |
| 122 |
{%- let vtable_methods = obj.vtable_methods() %} |
| 123 |
{% include "CallbackInterfaceImpl.py" %} |
| 124 |
{%- endif %} |
| 125 |
|
| 126 |
{# Objects as error #} |
| 127 |
{%- if ci.is_name_used_as_error(name) %} |
| 128 |
{# Due to some mismatches in the ffi converter mechanisms, errors are forced to be a RustBuffer #} |
| 129 |
class {{ ffi_converter_name }}__as_error(_UniffiConverterRustBuffer): |
| 130 |
@classmethod |
| 131 |
def read(cls, buf): |
| 132 |
raise NotImplementedError() |
| 133 |
|
| 134 |
@classmethod |
| 135 |
def write(cls, value, buf): |
| 136 |
raise NotImplementedError() |
| 137 |
|
| 138 |
@staticmethod |
| 139 |
def lift(value): |
| 140 |
# Errors are always a rust buffer holding a pointer - which is a "read" |
| 141 |
with value.consume_with_stream() as stream: |
| 142 |
return {{ ffi_converter_name }}.read(stream) |
| 143 |
|
| 144 |
@staticmethod |
| 145 |
def lower(value): |
| 146 |
raise NotImplementedError() |
| 147 |
|
| 148 |
{%- endif %} |
| 149 |
|
| 150 |
class {{ ffi_converter_name }}: |
| 151 |
{%- if obj.has_callback_interface() %} |
| 152 |
_handle_map = _UniffiHandleMap() |
| 153 |
{%- endif %} |
| 154 |
|
| 155 |
@staticmethod |
| 156 |
def lift(value: int): |
| 157 |
return {{ impl_name }}._make_instance_(value) |
| 158 |
|
| 159 |
@staticmethod |
| 160 |
def check_lower(value: {{ type_name }}): |
| 161 |
{%- if obj.has_callback_interface() %} |
| 162 |
pass |
| 163 |
{%- else %} |
| 164 |
if not isinstance(value, {{ impl_name }}): |
| 165 |
raise TypeError("Expected {{ impl_name }} instance, {} found".format(type(value).__name__)) |
| 166 |
{%- endif %} |
| 167 |
|
| 168 |
@staticmethod |
| 169 |
def lower(value: {{ protocol_name }}): |
| 170 |
{%- if obj.has_callback_interface() %} |
| 171 |
return {{ ffi_converter_name }}._handle_map.insert(value) |
| 172 |
{%- else %} |
| 173 |
if not isinstance(value, {{ impl_name }}): |
| 174 |
raise TypeError("Expected {{ impl_name }} instance, {} found".format(type(value).__name__)) |
| 175 |
return value._uniffi_clone_pointer() |
| 176 |
{%- endif %} |
| 177 |
|
| 178 |
@classmethod |
| 179 |
def read(cls, buf: _UniffiRustBuffer): |
| 180 |
ptr = buf.read_u64() |
| 181 |
if ptr == 0: |
| 182 |
raise InternalError("Raw pointer value was null") |
| 183 |
return cls.lift(ptr) |
| 184 |
|
| 185 |
@classmethod |
| 186 |
def write(cls, value: {{ protocol_name }}, buf: _UniffiRustBuffer): |
| 187 |
buf.write_u64(cls.lower(value)) |