uniffi-rs icon indicating copy to clipboard operation
uniffi-rs copied to clipboard

Using errors as function/callback method parameters

Open arg0d opened this issue 1 year ago • 5 comments

I'm not entirely sure whether this is meant to be supported or not. If this is indeed a valid use case, then a similliar issue in uniffi-bindgen-go will also have to be fixed.

Using an error as function parameter causes compilation errors.

error[E0277]: the trait bound `Enumeration: Lift<UniFfiTag>` is not satisfied
   --> /mounted_workdir/target/debug/build/uniffi-example-rondpoint-7ba6758a67cd664b/out/rondpoint.uniffi.rs:181:1
    |
181 | #[::uniffi::export_for_udl]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Lift<UniFfiTag>` is not implemented for `Enumeration`
    |
    = help: the following other types implement trait `Lift<UT>`:
              <bool as Lift<UT>>
              <i8 as Lift<UT>>
              <i16 as Lift<UT>>
              <i32 as Lift<UT>>
              <i64 as Lift<UT>>
              <u8 as Lift<UT>>
              <u16 as Lift<UT>>
              <u32 as Lift<UT>>
            and 18 others
    = note: this error originates in the attribute macro `::uniffi::export_for_udl` (in Nightly builds, run with -Z macro-backtrace for more info)

Sample:

namespace rondpoint {
  Enumeration copie_enumeration(Enumeration e);
}

[Error]
enum Enumeration {
    "Un",
    "Deux",
    "Trois",
};

Repro:

iff --git a/examples/rondpoint/src/rondpoint.udl b/examples/rondpoint/src/rondpoint.udl
index 7c8261d74..40e27aee3 100644
--- a/examples/rondpoint/src/rondpoint.udl
+++ b/examples/rondpoint/src/rondpoint.udl
@@ -14,6 +14,7 @@ enum minusculeMAJUSCULEEnum {
   "minusculeMAJUSCULEVariant",
 };
 
+[Error]
 enum Enumeration {
     "Un",
     "Deux",

arg0d avatar Nov 29 '23 12:11 arg0d

welp, I was hoping this was fixed on main, but there I see something slightly different.

error[E0277]: the trait bound `Enumeration: LowerReturn<UniFfiTag>` is not satisfied
   --> /Users/skip/src/moz/uniffi-rs/target/debug/build/uniffi-example-rondpoint-3da52427f3b90e4f/out/rondpoint.uniffi.rs:181:1
    |
181 | #[::uniffi::export_for_udl]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `LowerReturn<UniFfiTag>` is not implemented for `Enumeration`
    |

@bendk any ideas?

mhammond avatar Nov 29 '23 14:11 mhammond

Oh, I see. I was testing with v0.25.0. It seems to be working on main.

arg0d avatar Nov 29 '23 15:11 arg0d

No, its not working on main afterall :D I reset my changes after switching branches.

arg0d avatar Nov 29 '23 15:11 arg0d

We implement Lift, Lower, and ConvertError for error types: https://github.com/mozilla/uniffi-rs/blob/42362c8fdd7eadb4f94da7945f22923f7dc5059a/uniffi_macros/src/error.rs#L89-L91

I think this means that using Enumeration as an argument should work, but returning it doesn't. It should be easy to extend this by adding LiftReturn and/or LowerReturn to the derive_ffi_traits list. That seems like a good idea to me, although I haven't really thought though all the consequences of returning an exception type.

bendk avatar Nov 29 '23 15:11 bendk

Yes you are right, if Rust enum derives thiserror:Error, then using it as parameter is fine. Returning error as a value is what actually causes these errors.

arg0d avatar Nov 29 '23 15:11 arg0d