1 {%- let obj
= ci|
get_object_definition(name
) %}
4 _pointer
: ctypes
.c_void_p
6 {%- match obj
.primary_constructor() %}
7 {%- when Some
with (cons
) %}
8 def __init__(self
, {% call py
::arg_list_decl(cons
) -%}):
9 {%- call py
::setup_args_extra_indent(cons
) %}
10 self
._pointer
= {% call py
::to_ffi_call(cons
) %}
15 # In case of partial initialization of instances.
16 pointer
= getattr(self
, "_pointer", None)
17 if pointer
is not None:
18 _rust_call(_UniffiLib
.{{ obj
.ffi_object_free().name() }}, pointer
)
20 # Used by alternative constructors or any methods which return this type.
22 def _make_instance_(cls
, pointer
):
23 # Lightly yucky way to bypass the usual __init__ logic
24 # and just create a new instance with the required pointer.
25 inst
= cls
.__new
__(cls
)
26 inst
._pointer
= pointer
29 {%- for cons
in obj
.alternate_constructors() %}
32 def {{ cons
.name()|fn_name
}}(cls
, {% call py
::arg_list_decl(cons
) %}):
33 {%- call py
::setup_args_extra_indent(cons
) %}
34 # Call the (fallible) function before creating any half-baked object instances.
35 pointer
= {% call py
::to_ffi_call(cons
) %}
36 return cls
._make
_instance
_(pointer
)
39 {%- for meth
in obj
.methods() -%}
40 {%- call py
::method_decl(meth
.name()|fn_name
, meth
) %}
43 {%- for tm
in obj
.uniffi_traits() -%}
45 {%- when UniffiTrait
::Debug
{ fmt
} %}
46 {%- call py
::method_decl("__repr__", fmt
) %}
47 {%- when UniffiTrait
::Display
{ fmt
} %}
48 {%- call py
::method_decl("__str__", fmt
) %}
49 {%- when UniffiTrait
::Eq
{ eq
, ne
} %}
50 def __eq__(self
, other
: object) -> {{ eq
.return_type().unwrap()|type_name
}}:
51 if not isinstance(other
, {{ type_name
}}):
54 return {{ eq
.return_type().unwrap()|lift_fn
}}({% call py
::to_ffi_call_with_prefix("self._pointer", eq
) %})
56 def __ne__(self
, other
: object) -> {{ ne
.return_type().unwrap()|type_name
}}:
57 if not isinstance(other
, {{ type_name
}}):
60 return {{ ne
.return_type().unwrap()|lift_fn
}}({% call py
::to_ffi_call_with_prefix("self._pointer", ne
) %})
61 {%- when UniffiTrait
::Hash
{ hash } %}
62 {%- call py
::method_decl("__hash__", hash) %}
67 class {{ ffi_converter_name
}}:
72 raise InternalError("Raw pointer value was null")
76 def write(cls
, value
, buf
):
77 if not isinstance(value
, {{ type_name
}}):
78 raise TypeError("Expected {{ type_name }} instance, {} found".format(type(value
).__name
__))
79 buf
.write_u64(cls
.lower(value
))
83 return {{ type_name
}}._make
_instance
_(value
)