in uniffi_macros/src/error.rs [58:178]
fn flat_error_ffi_converter_impl(item: &EnumItem, options: &DeriveOptions) -> TokenStream {
let name = item.name();
let ident = item.ident();
let lower_impl_spec = options.ffi_impl_header("Lower", ident);
let lift_impl_spec = options.ffi_impl_header("Lift", ident);
let type_id_impl_spec = options.ffi_impl_header("TypeId", ident);
let derive_ffi_traits = options.derive_ffi_traits(ident, &["LowerError", "ConvertError"]);
let mod_path = match mod_path() {
Ok(p) => p,
Err(e) => return e.into_compile_error(),
};
let lower_impl = {
let mut match_arms: Vec<_> = item
.enum_()
.variants
.iter()
.enumerate()
.map(|(i, v)| {
let v_ident = &v.ident;
let idx = Index::from(i + 1);
let write_string = ffiops::write(quote! { ::std::string::String });
quote! {
Self::#v_ident { .. } => {
::uniffi::deps::bytes::BufMut::put_i32(buf, #idx);
#write_string(error_msg, buf);
}
}
})
.collect();
if item.is_non_exhaustive() {
match_arms.push(quote! {
_ => ::std::panic!("Unexpected variant in non-exhaustive enum"),
})
}
let lower = ffiops::lower_into_rust_buffer(quote! { Self });
quote! {
#[automatically_derived]
unsafe #lower_impl_spec {
type FfiType = ::uniffi::RustBuffer;
fn write(obj: Self, buf: &mut ::std::vec::Vec<u8>) {
let error_msg = ::std::string::ToString::to_string(&obj);
match obj { #(#match_arms)* }
}
fn lower(obj: Self) -> ::uniffi::RustBuffer {
#lower(obj)
}
}
}
};
let lift_impl = if item.generate_error_try_read() {
let match_arms = item.enum_().variants.iter().enumerate().map(|(i, v)| {
let v_ident = &v.ident;
let idx = Index::from(i + 1);
quote! {
#idx => Self::#v_ident,
}
});
let try_lift = ffiops::try_lift_from_rust_buffer(quote! { Self });
quote! {
#[automatically_derived]
unsafe #lift_impl_spec {
type FfiType = ::uniffi::RustBuffer;
fn try_read(buf: &mut &[::std::primitive::u8]) -> ::uniffi::deps::anyhow::Result<Self> {
::std::result::Result::Ok(match ::uniffi::deps::bytes::Buf::get_i32(buf) {
#(#match_arms)*
v => ::uniffi::deps::anyhow::bail!("Invalid #ident enum value: {}", v),
})
}
fn try_lift(v: ::uniffi::RustBuffer) -> ::uniffi::deps::anyhow::Result<Self> {
#try_lift(v)
}
}
}
} else {
quote! {
// Lifting flat errors is not currently supported, but we still define the trait so
// that dicts containing flat errors don't cause compile errors (see ReturnOnlyDict in
// coverall.rs).
//
// Note: it would be better to not derive `Lift` for dictionaries containing flat
// errors, but getting the trait bounds and derived impls there would be much harder.
// For now, we just fail at runtime.
#[automatically_derived]
unsafe #lift_impl_spec {
type FfiType = ::uniffi::RustBuffer;
fn try_read(buf: &mut &[::std::primitive::u8]) -> ::uniffi::deps::anyhow::Result<Self> {
::std::panic!("Can't lift flat errors")
}
fn try_lift(v: ::uniffi::RustBuffer) -> ::uniffi::deps::anyhow::Result<Self> {
::std::panic!("Can't lift flat errors")
}
}
}
};
quote! {
#lower_impl
#lift_impl
#[automatically_derived]
#type_id_impl_spec {
const TYPE_ID_META: ::uniffi::MetadataBuffer = ::uniffi::MetadataBuffer::from_code(::uniffi::metadata::codes::TYPE_ENUM)
.concat_str(#mod_path)
.concat_str(#name);
}
#derive_ffi_traits
}
}