Debian Codegraph @

third_party/rust/uniffi_bindgen/src/scaffolding/templates/ObjectTemplate.rs

raw
1 {#
2 // Forward work to `uniffi_macros` This keeps macro-based and UDL-based generated code consistent.
3 #}
4
5 {%- if obj.is_trait_interface() %}
6 #[::uniffi::export_for_udl{% if obj.has_callback_interface() %}(with_foreign){% endif %}]
7 pub trait r#{{ obj.name() }} {
8 {%- for meth in obj.methods() %}
9 {% if meth.is_async() %}async {% endif %}fn r#{{ meth.name() }}(
10 {% if meth.takes_self_by_arc()%}self: Arc<Self>{% else %}&self{% endif %},
11 {%- for arg in meth.arguments() %}
12 r#{{ arg.name() }}: {% if arg.by_ref() %}&{% endif %}{{ arg.as_type().borrow()|type_rs }},
13 {%- endfor %}
14 )
15 {%- match (meth.return_type(), meth.throws_type()) %}
16 {%- when (Some(return_type), None) %} -> {{ return_type|type_rs }};
17 {%- when (Some(return_type), Some(error_type)) %} -> ::std::result::Result::<{{ return_type|type_rs }}, {{ error_type|type_rs }}>;
18 {%- when (None, Some(error_type)) %} -> ::std::result::Result::<(), {{ error_type|type_rs }}>;
19 {%- when (None, None) %};
20 {%- endmatch %}
21 {% endfor %}
22 }
23 {%- else %}
24 {%- for tm in obj.uniffi_traits() %}
25 {% match tm %}
26 {% when UniffiTrait::Debug { fmt }%}
27 #[uniffi::export(Debug)]
28 {% when UniffiTrait::Display { fmt }%}
29 #[uniffi::export(Display)]
30 {% when UniffiTrait::Hash { hash }%}
31 #[uniffi::export(Hash)]
32 {% when UniffiTrait::Eq { eq, ne }%}
33 #[uniffi::export(Eq)]
34 {% endmatch %}
35 {% endfor %}
36 {%- if obj.remote() %}
37 #[::uniffi::udl_remote(Object)]
38 {%- else %}
39 #[::uniffi::udl_derive(Object)]
40 {%- endif %}
41 struct {{ obj.rust_name() }} { }
42
43 {%- for cons in obj.constructors() %}
44 #[::uniffi::export_for_udl]
45 impl {{ obj.rust_name() }} {
46 #[uniffi::constructor]
47 pub {% if cons.is_async() %}async {% endif %}fn r#{{ cons.name() }}(
48 {%- for arg in cons.arguments() %}
49 r#{{ arg.name() }}: {% if arg.by_ref() %}&{% endif %}{{ arg.as_type().borrow()|type_rs }},
50 {%- endfor %}
51 )
52 {%- match (cons.return_type(), cons.throws_type()) %}
53 {%- when (Some(return_type), None) %} -> {{ return_type|type_rs }}
54 {%- when (Some(return_type), Some(error_type)) %} -> ::std::result::Result::<{{ return_type|type_rs }}, {{ error_type|type_rs }}>
55 {%- when (None, Some(error_type)) %} -> ::std::result::Result::<(), {{ error_type|type_rs }}>
56 {%- when (None, None) %}
57 {%- endmatch %}
58 {
59 unreachable!()
60 }
61 }
62 {%- endfor %}
63
64 {%- for meth in obj.methods() %}
65 #[::uniffi::export_for_udl]
66 impl {{ obj.rust_name() }} {
67 pub {% if meth.is_async() %}async {% endif %}fn r#{{ meth.name() }}(
68 {% if meth.takes_self_by_arc()%}self: Arc<Self>{% else %}&self{% endif %},
69 {%- for arg in meth.arguments() %}
70 r#{{ arg.name() }}: {% if arg.by_ref() %}&{% endif %}{{ arg.as_type().borrow()|type_rs }},
71 {%- endfor %}
72 )
73 {%- match (meth.return_type(), meth.throws_type()) %}
74 {%- when (Some(return_type), None) %} -> {{ return_type|type_rs }}
75 {%- when (Some(return_type), Some(error_type)) %} -> ::std::result::Result::<{{ return_type|type_rs }}, {{ error_type|type_rs }}>
76 {%- when (None, Some(error_type)) %} -> ::std::result::Result::<(), {{ error_type|type_rs }}>
77 {%- when (None, None) %}
78 {%- endmatch %}
79 {
80 unreachable!()
81 }
82 }
83 {%- endfor %}
84
85 {% endif %}