Skip to content

Manganis can't pass numeric value to kotlin function #5485

@LLukas22

Description

@LLukas22

Problem

If i want to pass an numeric value (e.g. i64) to a kotlin function im getting this error:

error[E0308]: mismatched types
 --> packages/haptics/src/android.rs:8:5
  |
8 |     #[manganis::ffi("android")]
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `JValueGen<&JObject<'_>>`, found `JValueGen<&&JObject<'_>>`
  |
  = note: expected enum `JValueGen<&JObject<'_>>`
             found enum `JValueGen<&&JObject<'_>>`
  = note: this error originates in the attribute macro `manganis::ffi`

Steps To Reproduce

Steps to reproduce the behavior:

Rust code example:

mod ffi {
    #[cfg(target_os = "android")]
    #[manganis::ffi("android")]
    extern "Kotlin" {
        pub type HapticsPlugin;

        pub fn vibrate(this: &HapticsPlugin, duration: i64);
    }
}

Kotlin:

class HapticsPlugin(activity: Activity) {
    private val appContext = activity.applicationContext

    private val vibrator: Vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        val vibratorManager =
            appContext.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
        vibratorManager.defaultVibrator
    } else {
        @Suppress("DEPRECATION")
        appContext.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
    }

    fun vibrate(duration: Long) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            vibrator.vibrate(VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE))
        } else {
            @Suppress("DEPRECATION")
            vibrator.vibrate(duration)
        }
    }
}

Can be simplified further, but this is where i encountered the bug, if i pass the value as a string and parse it on the kotlin side it works.

Probably related to additionally borrowing a JValue.

Expected behavior

Numeric values should be passable to kotlin functions.

Environment:

  • Dioxus version: v0.7.5
  • Rust version: rustc 1.94.0
  • OS info: Linux 6.19.11-1-cachyos
  • App platform: android

Questionnaire

I would like to fix, but don't have a solution yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmanganisRelated to the manganis crate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions