@@ -50,55 +50,7 @@ struct RustCallStatus {
50
50
51
51
typedef int ForeignCallback (uint64_t handle, uint32_t method, uint8_t *args_data, int32_t args_len, RustBuffer *buf_ptr);
52
52
53
- struct RustStreamBuffer : std::basic_streambuf<uint8_t > {
54
- RustStreamBuffer (RustBuffer *buf) {
55
- this ->setg (buf->data , buf->data , buf->data + buf->len );
56
- this ->setp (buf->data , buf->data + buf->capacity );
57
- }
58
- ~RustStreamBuffer () = default ;
59
-
60
- private:
61
- RustStreamBuffer () = delete ;
62
- RustStreamBuffer (const RustStreamBuffer &) = delete ;
63
- RustStreamBuffer (RustStreamBuffer &&) = delete ;
64
-
65
- RustStreamBuffer &operator =(const RustStreamBuffer &) = delete ;
66
- RustStreamBuffer &operator =(RustStreamBuffer &&) = delete ;
67
- };
68
-
69
- struct RustStream : std::basic_iostream<uint8_t > {
70
- RustStream (RustBuffer *buf):
71
- streambuf (RustStreamBuffer(buf)), std::basic_iostream<uint8_t >(&streambuf) { }
72
-
73
- template <typename T, typename = std::enable_if_t <std::is_arithmetic_v<T>>>
74
- RustStream &operator >>(T &val) {
75
- read (reinterpret_cast <uint8_t *>(&val), sizeof (T));
76
-
77
- if (std::endian::native != std::endian::big) {
78
- auto bytes = reinterpret_cast <char *>(&val);
79
-
80
- std::reverse (bytes, bytes + sizeof (T));
81
- }
82
-
83
- return *this ;
84
- }
85
-
86
- template <typename T, typename = std::enable_if_t <std::is_arithmetic_v<T>>>
87
- RustStream &operator <<(T val) {
88
- if (std::endian::native != std::endian::big) {
89
- auto bytes = reinterpret_cast <char *>(&val);
90
-
91
- std::reverse (bytes, bytes + sizeof (T));
92
- }
93
-
94
- write (reinterpret_cast <uint8_t *>(&val), sizeof (T));
95
-
96
- return *this ;
97
- }
98
- private:
99
- RustStreamBuffer streambuf;
100
- };
101
-
53
+ {%- include " rust_buf_stream.cpp" %}
102
54
{%- include " scaffolding/object_map.cpp" %}
103
55
104
56
{%- for typ in ci.iter_types () %}
@@ -209,28 +161,9 @@ UNIFFI_EXPORT RustBuffer {{ ci.ffi_rustbuffer_reserve().name() }}(RustBuffer buf
209
161
210
162
{% for func in ci.function_definitions () %}
211
163
{% let ffi_func = func.ffi_func () %}
212
- UNIFFI_EXPORT
213
- {% match ffi_func.return_type () -%}
214
- {% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ ffi_func.name () }}(
215
- {%- for arg in ffi_func.arguments () %}
216
- {{- arg.type_ ().borrow ()|ffi_type_name }} {{ arg.name () }}{% if !loop.last || ffi_func.has_rust_call_status_arg () %}, {% endif -%}
217
- {% endfor %}
218
- {%- if ffi_func.has_rust_call_status_arg () %}RustCallStatus *out_status{% endif -%}
219
- ) {
164
+ UNIFFI_EXPORT {%- call macros::fn_definition (ffi_func) %} {
220
165
{%- call macros::fn_prologue (ci, func, ffi_func) %}
221
- {%- match func.return_type () %}
222
- {%- when Some with (return_type) %}
223
- auto ret = {{ namespace }}::{{ func.name () }}(
224
- {%- for arg in func.arguments () %}
225
- {{- arg|lift_fn }}({{ arg.name ()|var_name }}){% if !loop.last %}, {% endif -%}
226
- {% endfor %});
227
- return {{ return_type|lower_fn }}(ret);
228
- {% when None %}
229
- {{ namespace }}::{{ func.name () }}(
230
- {%- for arg in func.arguments () %}
231
- {{- arg|lift_fn }}({{ arg.name ()|var_name }}){% if !loop.last %}, {% endif -%}
232
- {%- endfor %});
233
- {%- endmatch %}
166
+ {%- call macros::invoke_native_fn (func, namespace ) %}
234
167
{%- call macros::fn_epilogue (ci, func, ffi_func) %}
235
168
}
236
169
{% endfor %}
@@ -248,14 +181,7 @@ UNIFFI_EXPORT void {{ ffi_func.name() }}(ForeignCallback callback_stub, RustCall
248
181
249
182
{% for ctor in obj.constructors () %}
250
183
{% let ffi_ctor = ctor.ffi_func () %}
251
- UNIFFI_EXPORT
252
- {% match ffi_ctor.return_type () -%}
253
- {% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ ffi_ctor.name () }}(
254
- {%- for arg in ffi_ctor.arguments () %}
255
- {{- arg.type_ ().borrow ()|ffi_type_name }} {{ arg.name () }}{% if !loop.last || ffi_ctor.has_rust_call_status_arg () %}, {% endif -%}
256
- {% endfor %}
257
- {%- if ffi_ctor.has_rust_call_status_arg () %}RustCallStatus *out_status{% endif -%}
258
- ) {
184
+ UNIFFI_EXPORT {%- call macros::fn_definition (ffi_ctor) %} {
259
185
{%- if ffi_ctor.has_rust_call_status_arg () %}
260
186
out_status->code = UNIFFI_CALL_STATUS_OK;
261
187
{% endif -%}
@@ -269,14 +195,7 @@ UNIFFI_EXPORT
269
195
{% endfor %}
270
196
271
197
{% let ffi_dtor = obj.ffi_object_free () %}
272
- UNIFFI_EXPORT
273
- {% match ffi_dtor.return_type () -%}
274
- {% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ ffi_dtor.name () }}(
275
- {%- for arg in ffi_dtor.arguments () %}
276
- {{- arg.type_ ().borrow ()|ffi_type_name }} {{ arg.name () }}{% if !loop.last || ffi_dtor.has_rust_call_status_arg () %}, {% endif -%}
277
- {% endfor %}
278
- {%- if ffi_dtor.has_rust_call_status_arg () %}RustCallStatus *out_status{% endif -%}
279
- ) {
198
+ UNIFFI_EXPORT {%- call macros::fn_definition (ffi_dtor) %} {
280
199
{%- if ffi_dtor.has_rust_call_status_arg () %}
281
200
out_status->code = UNIFFI_CALL_STATUS_OK;
282
201
{% endif -%}
@@ -286,48 +205,17 @@ UNIFFI_EXPORT
286
205
287
206
{% for func in obj.methods () %}
288
207
{% let ffi_func = func.ffi_func () %}
289
- UNIFFI_EXPORT
290
- {% match ffi_func.return_type () -%}
291
- {% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ ffi_func.name () }}(
292
- {%- for arg in ffi_func.arguments () %}
293
- {{- arg.type_ ().borrow ()|ffi_type_name }} {{ arg.name () }}{% if !loop.last || ffi_func.has_rust_call_status_arg () %}, {% endif -%}
294
- {% endfor %}
295
- {%- if ffi_func.has_rust_call_status_arg () %}RustCallStatus *out_status{% endif -%}
296
- ) {
297
- {%- if ffi_func.has_rust_call_status_arg () %}
298
- out_status->code = UNIFFI_CALL_STATUS_OK;
299
- {% endif -%}
300
-
301
- auto obj = {{ obj.name () }}_map.at ((uint64_t )ptr);
302
-
208
+ UNIFFI_EXPORT {%- call macros::fn_definition (ffi_func) %} {
303
209
{%- call macros::fn_prologue (ci, func, ffi_func) %}
304
- {% match func.return_type () %}
305
- {% when Some with (return_type) %}
306
- auto ret = obj->{{ func.name () }}({% if func.takes_self_by_arc () %}obj{% if !func.arguments ().is_empty () %},{% endif %}{% endif %}
307
- {%- for arg in func.arguments () %}
308
- {{- arg|lift_fn }}({{ arg.name ()|var_name }}){% if !loop.last %}, {% endif -%}
309
- {% endfor %});
310
- return {{ return_type|lower_fn }}(ret);
311
- {% when None %}
312
- obj->{{ func.name () }}(
313
- {%- for arg in func.arguments () %}
314
- {{- arg|lift_fn }}({{ arg.name ()|var_name }}){% if !loop.last %}, {% endif -%}
315
- {% endfor %});
316
- {% endmatch %}
210
+ auto obj = {{ obj.name () }}_map.at ((uint64_t )ptr);
211
+ {%- call macros::invoke_native_fn_obj (func) %}
317
212
{%- call macros::fn_epilogue (ci, func, ffi_func) %}
318
213
}
319
214
{% endfor %}
320
215
{% endfor %}
321
216
322
217
{% for func in ci.iter_futures_ffi_function_definitons () %}
323
- UNIFFI_EXPORT
324
- {% match func.return_type () -%}
325
- {% when Some with (return_type) %}{{ return_type|ffi_type_name }} {% when None %}void {% endmatch %}{{ func.name () }}(
326
- {%- for arg in func.arguments () %}
327
- {{- arg.type_ ().borrow ()|ffi_type_name }} {{ arg.name () }}{% if !loop.last || func.has_rust_call_status_arg () %}, {% endif -%}
328
- {% endfor %}
329
- {%- if func.has_rust_call_status_arg () %}RustCallStatus *out_status{% endif -%}
330
- ) {
218
+ UNIFFI_EXPORT {%- call macros::fn_definition (func) %} {
331
219
{%- match func.return_type () %}{% when Some with (return_type) %}
332
220
{% match return_type %}
333
221
{% when FfiType::RustArcPtr (_) %}
0 commit comments