Backed out 5 changesets (bug 1890092, bug 1888683) for causing build bustages & crash...
[gecko.git] / third_party / rust / uniffi_bindgen / src / bindings / python / templates / ObjectTemplate.py
blob7e98f7c46f650bcb944ac68f8acd7c0ff090c34d
1 {%- let obj = ci|get_object_definition(name) %}
3 class {{ type_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) %}
11 {%- when None %}
12 {%- endmatch %}
14 def __del__(self):
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.
21 @classmethod
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
27 return inst
29 {%- for cons in obj.alternate_constructors() %}
31 @classmethod
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)
37 {% endfor %}
39 {%- for meth in obj.methods() -%}
40 {%- call py::method_decl(meth.name()|fn_name, meth) %}
41 {% endfor %}
43 {%- for tm in obj.uniffi_traits() -%}
44 {%- match tm %}
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 }}):
52 return NotImplemented
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 }}):
58 return NotImplemented
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) %}
63 {% endmatch %}
64 {% endfor %}
67 class {{ ffi_converter_name }}:
68 @classmethod
69 def read(cls, buf):
70 ptr = buf.read_u64()
71 if ptr == 0:
72 raise InternalError("Raw pointer value was null")
73 return cls.lift(ptr)
75 @classmethod
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))
81 @staticmethod
82 def lift(value):
83 return {{ type_name }}._make_instance_(value)
85 @staticmethod
86 def lower(value):
87 return value._pointer