Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/swift/templates/macros.swift

raw
1 {#
2 // Template to call into rust. Used in several places.
3 // Variable names in `arg_list_decl` should match up with arg lists
4 // passed to rust via `arg_list_lowered`
5 #}
6
7 {%- macro to_ffi_call(func) -%}
8 {%- call is_try(func) -%}
9 {%- if let(Some(e)) = func.throws_type() -%}
10 rustCallWithError({{ e|ffi_error_converter_name }}_lift) {
11 {%- else -%}
12 rustCall() {
13 {%- endif %}
14 {{ func.ffi_func().name() }}(
15 {%- if func.takes_self() %}self.uniffiClonePointer(),{% endif %}
16 {%- call arg_list_lowered(func) -%} $0
17 )
18 }
19 {%- endmacro -%}
20
21 // eg, `public func foo_bar() { body }`
22 {%- macro func_decl(func_decl, callable, indent) %}
23 {%- call docstring(callable, indent) %}
24 {{ func_decl }} {{ callable.name()|fn_name }}(
25 {%- call arg_list_decl(callable) -%})
26 {%- call is_async(callable) %}
27 {%- call throws(callable) %}
28 {%- if let Some(return_type) = callable.return_type() %} -> {{ return_type|type_name }} {%- endif %} {
29 {%- call call_body(callable) %}
30 }
31 {%- endmacro %}
32
33 // primary ctor - no name, no return-type.
34 {%- macro ctor_decl(callable, indent) %}
35 {%- call docstring(callable, indent) %}
36 public convenience init(
37 {%- call arg_list_decl(callable) -%}) {%- call is_async(callable) %} {%- call throws(callable) %} {
38 {%- if callable.is_async() %}
39 let pointer =
40 {%- call call_async(callable) %}
41 {# The async mechanism returns an already constructed self.
42 We work around that by cloning the pointer from that object, then
43 assune the old object dies as there are no other references possible.
44 #}
45 .uniffiClonePointer()
46 {%- else %}
47 let pointer =
48 {% call to_ffi_call(callable) %}
49 {%- endif %}
50 self.init(unsafeFromRawPointer: pointer)
51 }
52 {%- endmacro %}
53
54 {%- macro call_body(callable) %}
55 {%- if callable.is_async() %}
56 return {%- call call_async(callable) %}
57 {%- else %}
58 {%- match callable.return_type() -%}
59 {%- when Some(return_type) %}
60 return {% call is_try(callable) %} {{ return_type|lift_fn }}({% call to_ffi_call(callable) %})
61 {%- when None %}
62 {%- call to_ffi_call(callable) %}
63 {%- endmatch %}
64 {%- endif %}
65
66 {%- endmacro %}
67
68 {%- macro call_async(callable) %}
69 {% call is_try(callable) %} await uniffiRustCallAsync(
70 rustFutureFunc: {
71 {{ callable.ffi_func().name() }}(
72 {%- if callable.takes_self() %}
73 self.uniffiClonePointer(){% if !callable.arguments().is_empty() %},{% endif %}
74 {% endif %}
75 {%- for arg in callable.arguments() -%}
76 {{ arg|lower_fn }}({{ arg.name()|var_name }}){% if !loop.last %},{% endif %}
77 {%- endfor %}
78 )
79 },
80 pollFunc: {{ callable.ffi_rust_future_poll(ci) }},
81 completeFunc: {{ callable.ffi_rust_future_complete(ci) }},
82 freeFunc: {{ callable.ffi_rust_future_free(ci) }},
83 {%- match callable.return_type() %}
84 {%- when Some(return_type) %}
85 liftFunc: {{ return_type|lift_fn }},
86 {%- when None %}
87 liftFunc: { $0 },
88 {%- endmatch %}
89 {%- match callable.throws_type() %}
90 {%- when Some with (e) %}
91 errorHandler: {{ e|ffi_error_converter_name }}_lift
92 {%- else %}
93 errorHandler: nil
94 {% endmatch %}
95 )
96 {%- endmacro %}
97
98 {%- macro arg_list_lowered(func) %}
99 {%- for arg in func.arguments() %}
100 {{ arg|lower_fn }}({{ arg.name()|var_name }}),
101 {%- endfor %}
102 {%- endmacro -%}
103
104 {#-
105 // Arglist as used in Swift declarations of methods, functions and constructors.
106 // Note the var_name and type_name filters.
107 -#}
108
109 {% macro arg_list_decl(func) %}
110 {%- for arg in func.arguments() -%}
111 {% if config.omit_argument_labels() %}_ {% endif %}{{ arg.name()|var_name }}: {{ arg|type_name -}}
112 {%- match arg.default_value() %}
113 {%- when Some with(literal) %} = {{ literal|literal_swift(arg) }}
114 {%- else %}
115 {%- endmatch %}
116 {%- if !loop.last %}, {% endif -%}
117 {%- endfor %}
118 {%- endmacro %}
119
120 {#-
121 // Field lists as used in Swift declarations of Records and Enums.
122 // Note the var_name and type_name filters.
123 -#}
124 {% macro field_list_decl(item, has_nameless_fields) %}
125 {%- for field in item.fields() -%}
126 {%- call docstring(field, 8) %}
127 {%- if has_nameless_fields %}
128 {{- field|type_name -}}
129 {%- if !loop.last -%}, {%- endif -%}
130 {%- else -%}
131 {{ field.name()|var_name }}: {{ field|type_name -}}
132 {%- match field.default_value() %}
133 {%- when Some with(literal) %} = {{ literal|literal_swift(field) }}
134 {%- else %}
135 {%- endmatch -%}
136 {% if !loop.last %}, {% endif %}
137 {%- endif -%}
138 {%- endfor %}
139 {%- endmacro %}
140
141 {% macro field_name(field, field_num) %}
142 {%- if field.name().is_empty() -%}
143 v{{- field_num -}}
144 {%- else -%}
145 {{ field.name()|var_name }}
146 {%- endif -%}
147 {%- endmacro %}
148
149 {% macro arg_list_protocol(func) %}
150 {%- for arg in func.arguments() -%}
151 {% if config.omit_argument_labels() %}_ {% endif %}{{ arg.name()|var_name }}: {{ arg|type_name -}}
152 {%- if !loop.last %}, {% endif -%}
153 {%- endfor %}
154 {%- endmacro %}
155
156 {%- macro is_async(func) %}
157 {%- if func.is_async() %}async {% endif %}
158 {%- endmacro -%}
159
160 {%- macro throws(func) %}
161 {%- if func.throws() %}throws {% endif %}
162 {%- endmacro -%}
163
164 {%- macro is_try(func) %}
165 {%- if func.throws() %}try {% else %}try! {% endif %}
166 {%- endmacro -%}
167
168 {%- macro docstring_value(maybe_docstring, indent_spaces) %}
169 {%- match maybe_docstring %}
170 {%- when Some(docstring) %}
171 {{ docstring|docstring(indent_spaces) }}
172 {%- else %}
173 {%- endmatch %}
174 {%- endmacro %}
175
176 {%- macro docstring(defn, indent_spaces) %}
177 {%- call docstring_value(defn.docstring(), indent_spaces) %}
178 {%- endmacro %}