fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
4.12k stars 283 forks source link

[Bug] type u8 array support? #834

Closed thaodt closed 1 year ago

thaodt commented 1 year ago

Describe the bug

My native/src/api.rs:

use anyhow::{anyhow, Result};
use once_cell::sync::Lazy;
use std::sync::{Arc, RwLock};

use common::{
    types::Sha256Hash,
    my_data::MyData,
};

pub static GLOB_DATA: Lazy<Arc<RwLock<Option<MyData>>>> =
    Lazy::new(|| Arc::new(RwLock::new(None)));

pub struct MyDataWrapper {}
impl MyDataWrapper {
pub fn get_latest_block_hash() -> Result<Sha256Hash, String> { // <----- PROBLEM HERE!!! the fn output signature cannot be parsed
        match GLOB_DATA.try_read() {
            Ok(rw_lock) => match rw_lock.as_ref() {
                Some(wallet_db) => {
                    if let Some(block_hash) = wallet_db.get_latest_block_hash() {
                        Ok(*block_hash)
                    } else {
                        Err(anyhow!("data not synchronized").to_string())
                    }
                }
                None => Err(anyhow!("data was not set").to_string()),
            },
            Err(err) => Err(format!("The mutex poisoned. {}", err)),
        }
    }
}

and the codegen seems didn't work. The codegen error in the next section.

The Sha256Hash type is defined: pub type Sha256Hash = [u8; 32]; Its just a fixed-size array in Rust.

Codegen logs with RUST_LOG=debug environment variable

RUST_LOG=debug flutter_rust_bridge_codegen  --rust-input native/src/api.rs --dart-output lib/bridge_generated.dart --c-output ios/Runner/bridge_generated.h --rust-output native/src/bridge_generated.rs

The output:

[2022-11-09T12:23:43Z DEBUG flutter_rust_bridge_codegen] configs=[Opts { rust_input_path: "/home/thaodt/projects/MyWallet/native/src/api.rs", dart_output_path: "/home/thaodt/projects/MyWallet/lib/bridge_generated.dart", dart_decl_output_path: None, c_output_path: ["/home/thaodt/projects/MyWallet/ios/Runner/bridge_generated.h"], rust_crate_dir: "/home/thaodt/projects/MyWallet/native", rust_output_path: "/home/thaodt/projects/MyWallet/native/src/bridge_generated.rs", class_name: "Native", dart_format_line_length: 80, skip_add_mod_to_lib: false, llvm_path: ["/opt/homebrew/opt/llvm", "/usr/local/opt/llvm", "/usr/lib/llvm-9", "/usr/lib/llvm-10", "/usr/lib/llvm-11", "/usr/lib/llvm-12", "/usr/lib/llvm-13", "/usr/lib/llvm-14", "/usr/lib/", "/usr/lib64/", "C:/Program Files/llvm", "C:/msys64/mingw64"], llvm_compiler_opts: "", manifest_path: "/home/thaodt/projects/MyWallet/native/Cargo.toml", dart_root: Some("/home/thaodt/projects/MyWallet"), build_runner: true, block_index: BlockIndex(0), skip_deps_check: false, wasm_enabled: false, inline_rust: false }]
[2022-11-09T12:23:44Z DEBUG lib_flutter_rust_bridge_codegen::source_graph] Trying to parse "/home/thaodt/projects/MyWallet/native/src/api.rs"
[2022-11-09T12:23:44Z DEBUG lib_flutter_rust_bridge_codegen::source_graph] Trying to parse "/home/thaodt/projects/MyWallet/native/src/bridge_generated.rs"
[2022-11-09T12:23:44Z DEBUG lib_flutter_rust_bridge_codegen::parser] parse_function function name: Ident(get_balance__static_method__MyDataWrapper)
[2022-11-09T12:23:44Z DEBUG lib_flutter_rust_bridge_codegen::parser] parse_function function name: Ident(get_latest_block_id__static_method__MyDataWrapper)
[2022-11-09T12:23:44Z DEBUG lib_flutter_rust_bridge_codegen::parser] parse_function function name: Ident(get_latest_block_hash__static_method__MyDataWrapper)
thread 'main' panicked at 'Failed to parse function output type `Result<Sha256Hash,String>`', /home/thaodt/.cargo/registry/src/github.com-1ecc6299db9ec823/flutter_rust_bridge_codegen-1.49.1/src/parser/mod.rs:161:25
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: Recipe `gen` failed on line 16 with exit code 101

To Reproduce

Just run the command codegen above.

Expected behavior

function output signature should be parsed.

Generated binding code

No response

OS

Kernel 5.15.76-1-MANJARO

Version of flutter_rust_bridge_codegen

1.49.1

Flutter info

[✓] Flutter (Channel stable, 3.3.7, on Manjaro Linux 5.15.76-1-MANJARO, locale en_US.UTF-8)
    • Flutter version 3.3.7 on channel stable at /home/thaodt/projects/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision e99c9c7cd9 (8 days ago), 2022-11-01 16:59:00 -0700
    • Engine revision 857bd6b74c
    • Dart version 2.18.4
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /home/thaodt/Android/Sdk
    • Platform android-32, build-tools 32.1.0-rc1
    • Java binary at: /opt/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = /usr/bin/google-chrome-stable

[✓] Linux toolchain - develop for Linux desktop
    • clang version 14.0.6
    • cmake version 3.24.2
    • ninja version 1.11.1
    • pkg-config version 1.8.0

[✓] Android Studio (version 2021.2)
    • Android Studio at /opt/android-studio
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Manjaro Linux 5.15.76-1-MANJARO
    • Chrome (web)    • chrome • web-javascript • Google Chrome 106.0.5249.103

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

Version of clang++

14.0.6

Version of ffigen

6.0.1

Additional context

Link to #831.

fzyzcjy commented 1 year ago

I forget whether type A = B syntax works or not. Try to change it like struct Sha256Hash([u8; 32]) to see whether this is the problem

thaodt commented 1 year ago

change into struct Sha256Hash([u8; 32])

it seems working. But Im getting the error below in bridge_generated.rs:

fn wire_get_latest_block_hash__static_method__WalletDBWrapper_impl(port_: MessagePort) {
    FLUTTER_RUST_BRIDGE_HANDLER.wrap(
        WrapInfo {
            debug_name: "get_latest_block_hash__static_method__WalletDBWrapper",
            port: Some(port_),
            mode: FfiCallMode::Normal,
        },
        move || move |task_callback| WalletDBWrapper::get_latest_block_hash(), // <----- expected Result<{unknown}, Error>, found Result<Sha256Hash, String>
    )
}

Actually it should expect Result<Sha256Hash, String>, right?

fzyzcjy commented 1 year ago

is common same or different crate? https://cjycode.com/flutter_rust_bridge/feature/lang_external.html

thaodt commented 1 year ago

is common same or different crate? https://cjycode.com/flutter_rust_bridge/feature/lang_external.html

hmm its weird because all vars, fns which I imported from common worked fine. Lemme check again. I will close this issue because the original problem is resolved.

thaodt commented 1 year ago

@fzyzcjy just curious. Is there any reason we didn't support type A = B? any problems with Dart code conversion or from Rust FFI?

fzyzcjy commented 1 year ago

No any problem indeed. Feel free to PR and I am looking forward to merge it! :)

thaodt commented 1 year ago

No any problem indeed. Feel free to PR and I am looking forward to merge it! :)

Any hints for this? I'd like to give it a try. Also, are you planning setup a discord server for discussion? :smile:

fzyzcjy commented 1 year ago

Any hints for this

parser.rs

Also, are you planning setup a discord server for discussion? 😄

Just ask here - I am on github and reply (more) quickly :)

fzyzcjy commented 1 year ago

And this one: https://cjycode.com/flutter_rust_bridge/contributing/design.html

github-actions[bot] commented 1 year ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.