blob: 0d83a99b900d56a7c1d9befaa9c30894cd6d7e5c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
{#
// For each enum declared in the UDL, we assume the caller has provided a corresponding
// rust `enum`. We provide the traits for sending it across the FFI, which will fail to
// compile if the provided struct has a different shape to the one declared in the UDL.
//
// We define a unit-struct to implement the trait to sidestep Rust's orphan rule (ADR-0006). It's
// public so other crates can refer to it via an `[External='crate'] typedef`
#}
#[doc(hidden)]
pub struct {{ e.type_().borrow()|ffi_converter_name }};
#[doc(hidden)]
impl uniffi::RustBufferFfiConverter for {{ e.type_().borrow()|ffi_converter_name }} {
type RustType = r#{{ e.name() }};
fn write(obj: Self::RustType, buf: &mut std::vec::Vec<u8>) {
use uniffi::deps::bytes::BufMut;
match obj {
{%- for variant in e.variants() %}
r#{{ e.name() }}::r#{{ variant.name() }} { {% for field in variant.fields() %}r#{{ field.name() }}, {%- endfor %} } => {
buf.put_i32({{ loop.index }});
{% for field in variant.fields() -%}
{{ field.type_().borrow()|ffi_converter }}::write(r#{{ field.name() }}, buf);
{%- endfor %}
},
{%- endfor %}
};
}
fn try_read(buf: &mut &[u8]) -> uniffi::deps::anyhow::Result<r#{{ e.name() }}> {
use uniffi::deps::bytes::Buf;
uniffi::check_remaining(buf, 4)?;
Ok(match buf.get_i32() {
{%- for variant in e.variants() %}
{{ loop.index }} => r#{{ e.name() }}::r#{{ variant.name() }}{% if variant.has_fields() %} {
{% for field in variant.fields() %}
r#{{ field.name() }}: {{ field.type_().borrow()|ffi_converter }}::try_read(buf)?,
{%- endfor %}
}{% endif %},
{%- endfor %}
v => uniffi::deps::anyhow::bail!("Invalid {{ e.name() }} enum value: {}", v),
})
}
}
|