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

BUG: Kotlin Bug (swift works) #2

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

Sajjon
Copy link
Owner

@Sajjon Sajjon commented May 21, 2024

This is a bug, do not merge. I opened it as a DRAFT PR just to be able to show this bug to @bendk. I'm using Bens fork for UniFFI:

uniffi = { git = "https://github.com/bendk/uniffi-rs/", rev = "4b65c3796bc3743404e1ee2a7b4ab51e24589aa6" }

N.B. Kotlin bug only, works in Swift! Bug due to in Kotlin there are many different RustBuffers, one per crate

error: type mismatch: inferred type is com.sajjon.farm.RustBuffer.ByValue but com.sajjon.common.RustBuffer.ByValue was expected uniffiRustCall() { _status ->
Screenshot 2024-05-21 at 10 15 53

Workspace setup

This is the setup to reproduce the bug:

  • Workspace with third party crate dependency: Uuid
  • Workspace with three crates:
    • common crate which does makes Uuid UniFFI compatible using custom_type
    • farm crate which declares struct BatchID(Uuid); remote_type!(Uuid, common); custom_type!(BatchID, Uuid, {
    • pie crate which declares struct PieBatchID(BatchID); remote_type!(BatchID, farm); custom_type!(PieBatchID, BatchID, {

common

In common crate:

uniffi::custom_type!(Uuid, String, {
    remote,
    from_custom: |id| id.to_string(),
    try_into_custom: |s| Ok(Uuid::from_str(&s)?)
});

farm

In farm crate:

#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct BatchID(Uuid);

uniffi::remote_type!(Uuid, common);

uniffi::custom_type!(BatchID, Uuid, {
    from_custom: |batch_id| batch_id.0,
    try_into_custom: |uuid| Ok(BatchID(uuid))
});

#[uniffi::export]
pub fn new_batch_id_random() -> BatchID {
    BatchID(Uuid::new_v4())
}

pie

In pie crate:

#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct PieBatchID(BatchID);

uniffi::custom_type!(PieBatchID, BatchID, {
    from_custom: |batch_id| batch_id.0,
    try_into_custom: |uuid| Ok(PieBatchID(uuid))
});

#[uniffi::export]
pub fn new_pie_batch_id_random() -> PieBatchID {
    PieBatchID(new_batch_id_random())
}

This setup works for Swift bindgen tests.

But when running a Kotlin bindgen tests it does not compile. here is the generated non compiling Kotlin code in question:

 fun `newPieBatchIdRandom`(): PieBatchId {
            return FfiConverterTypePieBatchID.lift(
    uniffiRustCall() { _status ->
    UniffiLib.INSTANCE.uniffi_lemon_meringue_pie_fn_func_new_pie_batch_id_random(
        _status)
}
    )
    }

I guess what is missing is some kind of conversion between the RustBuffers of different packages? Or hmm I'm actually not in need of the different crates of my Workspace, the plan is not to vendor each crate individually, the only purpose for me of the Workspace is to decrease compilation time.

…sajjon.farm.RustBuffer.ByValue but com.sajjon.common.RustBuffer.ByValue was expected uniffiRustCall() { _status ->' when nesting Uuid newtypes in accross different crates
@bendk
Copy link

bendk commented May 21, 2024

You're getting into some real corner cases with your remote/custom/external types, which I love. I just filed an issue that I think captures the problem.

In the meantime, I think you could get it to work by having PieBatchID wrap Uuid directly, rather than the double-wrapping you currently have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants