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

UniFFI fails to generate Swift bindings in my project #2324

Open
bes opened this issue Nov 19, 2024 · 4 comments
Open

UniFFI fails to generate Swift bindings in my project #2324

bes opened this issue Nov 19, 2024 · 4 comments

Comments

@bes
Copy link

bes commented Nov 19, 2024

Hello, I am using a staticlib-only iOS project with UniFFI, and I am having some trouble generating Swift bindings.

I have a small proof-of-concept here https://github.com/bes/uniffi-2024-11-poc

When I run the build.sh script, the output is:

    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s
+ cargo run --features=uniffi/cli --bin uniffi-bindgen generate --library ./target/aarch64-apple-ios-sim/debug/libuniffi_2024_11_poc.a --language swift --out-dir generated/uniffi
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s
     Running `target/debug/uniffi-bindgen generate --library ./target/aarch64-apple-ios-sim/debug/libuniffi_2024_11_poc.a --language swift --out-dir generated/uniffi`
Failed to extract data from archive member `uniffi_2024_11_poc-e43440458ed975e7.988kinrgws14mi3ykd8qm9h0t.rcgu.o`

Rust:

rustc --version
rustc 1.82.0 (f6e511eec 2024-10-15)
@bes
Copy link
Author

bes commented Nov 20, 2024

Downgrading to 0.27.3 and compiling reveals that it doesn't like that the constructor returns Option<Arc<Self>>. Changing the constructor to return Arc<Self> instead fixes the problem.

Does NOT work on 0.27.3 or 0.28.3

    #[uniffi::constructor]
    pub fn new(
        id: Option<String>,
        id2: String,
        id3: Option<String>,
        loaded: bool,
    ) -> Option<Arc<Self>> {
        todo!();
    }

Works on both

    #[uniffi::constructor]
    pub fn new(
        id: Option<String>,
        id2: String,
        id3: Option<String>,
        loaded: bool,
    ) -> Arc<Self> {
        todo!();
    }

But at least 0.27.3 complains, which makes it possible to temp fix.

I would really like the constructor to be able to return Option :)

@bes
Copy link
Author

bes commented Nov 20, 2024

Workaround

It's possible to work around by adding another layer of indirection, by storing the optional result in another object and then using a regular self-method to fetch it.

@bendk
Copy link
Contributor

bendk commented Nov 20, 2024

We've had a bunch of philosophical discussions over the years on what exactly a "constructor" is. IMO, a constructor should not be able to return None, since that means it didn't construct anything. On a practical level, I think what you want is:

  • A better UniFFI error message (let's use this issue for that)
  • Switching your code to a failible constructor: one that returns a Result where the error values represent failure to construct. See the discussion in Support constructors returning Option<>? #1882.
  • Static method support (static method support #1074). In the meantime, you can define a top-level function that returns Option<YourClass>

@bes
Copy link
Author

bes commented Nov 20, 2024

As long as the problem is solvable (easily solvable as soon as I understood the problem) I'm fine with whatever you propose.

I must say, I converted my code from swift-bridge to uniffi, and the experience was smooth, I converted a lot of code and it mostly just worked, with better interfaces (and safer to boot!). Well done!

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