Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated functions should use interfaces when possible (arg, return types) #1352

Closed
jmartinesp opened this issue Sep 29, 2022 · 2 comments
Closed

Comments

@jmartinesp
Copy link
Contributor

jmartinesp commented Sep 29, 2022

Hi all, first thanks for the amazing work on the bindings generator, we're using it as a base for our shared library across Android, iOS and web and it's been incredibly helpful so far.

Today I was trying to create a fake of one of our Rust classes in Kotlin and I saw that for every exported type there is a "TypeNameInterface" interface or protocol created. I started creating the fake for this interface but I had to stop as soon as I got to a function that returned a struct with some impl from our project since the function returned the concrete generated type that extends from FFIObject instead of the interface.

interface Bar {
   void bar();
}

interface Foo {
    Bar foo();
}

This would create:

class Bar(pointer: Pointer) : FFIObject(pointer), BarInterface {
    override fun `bar`() { ... }
}

class Foo(pointer: Pointer) : FFIObject(pointer), FooInterface {
    override fun `foo`(): Bar { ... }
}
  • If I tried to create a fake of that class, I couldn't do it because the Foo class is not open, so you can't extend it.
  • There is not way to return a fake that extends from the interface since the return type is the concrete class, not the interface.

The problem seems to be the same on iOS, but with protocols instead of interfaces. Maybe I'm doing something wrong?

It's probably worth noting that while this might work for unit testing using some mocking library, which I haven't really tested yet, my current use case is that one of our Android views indirectly calls our Rust code to retrieve some values and this causes some tools such as the UI layout preview to crash when they try to load the generated native library that the concrete generated classes use underneath. So my goal is to have those fakes not only for testing, but for other use cases where I can't load the native library.

┆Issue is synchronized with this Jira Task
┆friendlyId: UNIFFI-202

@mhammond
Copy link
Member

mhammond commented Oct 1, 2022

It does make sense that the interface should be described in terms of other interfaces where possible, but it just hasn't bitten us yet. IOW, I think a PR for this is likely to be accepted.

@jmartinesp
Copy link
Contributor Author

Fixed by #1815.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants