summaryrefslogtreecommitdiffstats
path: root/third_party/rust/uniffi_bindgen/src/bindings/python/templates/macros.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--third_party/rust/uniffi_bindgen/src/bindings/python/templates/macros.py95
1 files changed, 66 insertions, 29 deletions
diff --git a/third_party/rust/uniffi_bindgen/src/bindings/python/templates/macros.py b/third_party/rust/uniffi_bindgen/src/bindings/python/templates/macros.py
index ef3b1bb94d..6818a8c107 100644
--- a/third_party/rust/uniffi_bindgen/src/bindings/python/templates/macros.py
+++ b/third_party/rust/uniffi_bindgen/src/bindings/python/templates/macros.py
@@ -5,27 +5,29 @@
#}
{%- macro to_ffi_call(func) -%}
- {%- match func.throws_type() -%}
- {%- when Some with (e) -%}
-_rust_call_with_error({{ e|ffi_converter_name }},
- {%- else -%}
-_rust_call(
- {%- endmatch -%}
- _UniffiLib.{{ func.ffi_func().name() }},
- {%- call arg_list_lowered(func) -%}
-)
+{%- call _to_ffi_call_with_prefix_arg("", func) %}
{%- endmacro -%}
{%- macro to_ffi_call_with_prefix(prefix, func) -%}
- {%- match func.throws_type() -%}
- {%- when Some with (e) -%}
-_rust_call_with_error(
- {{ e|ffi_converter_name }},
- {%- else -%}
+{%- call _to_ffi_call_with_prefix_arg(format!("{},", prefix), func) %}
+{%- endmacro -%}
+
+{%- macro _to_ffi_call_with_prefix_arg(prefix, func) -%}
+{%- match func.throws_type() -%}
+{%- when Some with (e) -%}
+{%- match e -%}
+{%- when Type::Enum { name, module_path } -%}
+_rust_call_with_error({{ e|ffi_converter_name }},
+{%- when Type::Object { name, module_path, imp } -%}
+_rust_call_with_error({{ e|ffi_converter_name }}__as_error,
+{%- else %}
+# unsupported error type!
+{%- endmatch %}
+{%- else -%}
_rust_call(
- {%- endmatch -%}
+{%- endmatch -%}
_UniffiLib.{{ func.ffi_func().name() }},
- {{- prefix }},
+ {{- prefix }}
{%- call arg_list_lowered(func) -%}
)
{%- endmacro -%}
@@ -37,6 +39,19 @@ _rust_call(
{%- endfor %}
{%- endmacro -%}
+{%- macro docstring_value(maybe_docstring, indent_spaces) %}
+{%- match maybe_docstring %}
+{%- when Some(docstring) %}
+{{ docstring|docstring(indent_spaces) }}
+{{ "" }}
+{%- else %}
+{%- endmatch %}
+{%- endmacro %}
+
+{%- macro docstring(defn, indent_spaces) %}
+{%- call docstring_value(defn.docstring(), indent_spaces) %}
+{%- endmacro %}
+
{#-
// Arglist as used in Python declarations of methods, functions and constructors.
// Note the var_name and type_name filters.
@@ -76,6 +91,7 @@ _rust_call(
if {{ arg.name()|var_name }} is _DEFAULT:
{{ arg.name()|var_name }} = {{ literal|literal_py(arg.as_type().borrow()) }}
{%- endmatch %}
+ {{ arg|check_lower_fn }}({{ arg.name()|var_name }})
{% endfor -%}
{%- endmacro -%}
@@ -91,6 +107,7 @@ _rust_call(
if {{ arg.name()|var_name }} is _DEFAULT:
{{ arg.name()|var_name }} = {{ literal|literal_py(arg.as_type().borrow()) }}
{%- endmatch %}
+ {{ arg|check_lower_fn }}({{ arg.name()|var_name }})
{% endfor -%}
{%- endmacro -%}
@@ -100,11 +117,18 @@ _rust_call(
{%- macro method_decl(py_method_name, meth) %}
{% if meth.is_async() %}
- def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}):
+{%- match meth.return_type() %}
+{%- when Some with (return_type) %}
+ async def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> "{{ return_type|type_name }}":
+{%- when None %}
+ async def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> None:
+{% endmatch %}
+
+ {%- call docstring(meth, 8) %}
{%- call setup_args_extra_indent(meth) %}
- return _uniffi_rust_call_async(
+ return await _uniffi_rust_call_async(
_UniffiLib.{{ meth.ffi_func().name() }}(
- self._pointer, {% call arg_list_lowered(meth) %}
+ self._uniffi_clone_pointer(), {% call arg_list_lowered(meth) %}
),
_UniffiLib.{{ meth.ffi_rust_future_poll(ci) }},
_UniffiLib.{{ meth.ffi_rust_future_complete(ci) }},
@@ -116,13 +140,7 @@ _rust_call(
{%- when None %}
lambda val: None,
{% endmatch %}
- # Error FFI converter
- {%- match meth.throws_type() %}
- {%- when Some(e) %}
- {{ e|ffi_converter_name }},
- {%- when None %}
- None,
- {%- endmatch %}
+ {% call error_ffi_converter(meth) %}
)
{%- else -%}
@@ -131,17 +149,36 @@ _rust_call(
{%- when Some with (return_type) %}
def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> "{{ return_type|type_name }}":
+ {%- call docstring(meth, 8) %}
{%- call setup_args_extra_indent(meth) %}
return {{ return_type|lift_fn }}(
- {% call to_ffi_call_with_prefix("self._pointer", meth) %}
+ {% call to_ffi_call_with_prefix("self._uniffi_clone_pointer()", meth) %}
)
{%- when None %}
- def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}):
+ def {{ py_method_name }}(self, {% call arg_list_decl(meth) %}) -> None:
+ {%- call docstring(meth, 8) %}
{%- call setup_args_extra_indent(meth) %}
- {% call to_ffi_call_with_prefix("self._pointer", meth) %}
+ {% call to_ffi_call_with_prefix("self._uniffi_clone_pointer()", meth) %}
{% endmatch %}
{% endif %}
{% endmacro %}
+
+{%- macro error_ffi_converter(func) %}
+ # Error FFI converter
+{% match func.throws_type() %}
+{%- when Some(e) %}
+{%- match e -%}
+{%- when Type::Enum { name, module_path } -%}
+ {{ e|ffi_converter_name }},
+{%- when Type::Object { name, module_path, imp } -%}
+ {{ e|ffi_converter_name }}__as_error,
+{%- else %}
+ # unsupported error type!
+{%- endmatch %}
+{%- when None %}
+ None,
+{%- endmatch %}
+{% endmacro %}