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

Unable to automatically generate include 3rd party sdk (matrix-sdk) #2486

Open
Prathiv7 opened this issue Jan 8, 2025 · 9 comments
Open
Labels
experimental feature Issues related to experimental instead of stable features

Comments

@Prathiv7
Copy link

Prathiv7 commented Jan 8, 2025

Describe the bug

After running

Steps to reproduce

  1. flutter_rust_bridge_codegen integrate then manually including the 3rd Party library and updated rust_root in flutter_rust_bridge.yaml
  2. Then added below into newly added rust sdk create
    [lib] crate-type = ["cdylib", "staticlib"]
    [dependencies] flutter_rust_bridge = "=2.7.0"
  3. RUST_LOG=debug flutter_rust_bridge_codegen -v generate --stop-on-error

issue occurs

Logs

[65.5s] Parse  
  └── [62.8s] Cargo expand & syn parse  
  └── [2.8s] Parse HIR
 Error: parse_function halt since stop_on_error=true and see error (function={"namespace":"matrix_sdk::encryption::backups","owner":{"StructOrEnum":{"impl_ty":"Backups","trait_def_name":null}},
 "sources":["Normal"],"item_fn":"GeneralizedItemFn(name=room_keys_for_room_stream, vis=Some(Visibility::Public(Pub)), attrs=[# [doc = \" Subscribe to a stream that notifies when a room key for the specified\"], 
 # [doc = \" room is downloaded from the key backup.\"]])"})

Caused by:
    0: when trying to parse DartFn
    1: Unknown ident: Stream

Expected behavior

No response

Generated binding code

No response

OS

MacOS

Version of flutter_rust_bridge_codegen

2.7.0

Flutter info

[✓] Flutter (Channel stable, 3.24.4, on macOS 14.6.1 23G93 darwin-x64, locale en-GB)
    • Flutter version 3.24.4 on channel stable at /Users/grootan/fvm/versions/3.24.4
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 603104015d (3 months ago), 2024-10-24 08:01:25 -0700
    • Engine revision db49896cf2
    • Dart version 3.5.4
    • DevTools version 2.37.3

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/grootan/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • ANDROID_HOME = /Users/grootan/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/grootan/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • 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 17.0.11+0-17.0.11b1207.24-11852314)

[✓] IntelliJ IDEA Community Edition (version 2024.3.1.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin version 243.23177

[✓] VS Code (version 1.96.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.102.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-x64     • macOS 14.6.1 23G93 darwin-x64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 131.0.6778.264
    ! Error: Browsing on the local area network for Grootan’s iPhone XR (2). Ensure the device is unlocked and attached with a cable or associated
      with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Version of clang++

No response

Additional context

Also a side note , no steps in flutter_rust_bridge doc says to added below on rust project , maybe this is basic but if it's in doc then it will be useful .

Also if we #[frb(ignore)] on issue method or struct it would leads to add on many other struct & methods other depends on it

Can you please give suggestions or solution to this

[lib] crate-type = ["cdylib", "staticlib"]

[dependencies] flutter_rust_bridge = "=2.7.0"

@Prathiv7 Prathiv7 added the bug Something isn't working label Jan 8, 2025
Copy link

welcome bot commented Jan 8, 2025

Hi! Thanks for opening your first issue here! 😄

@Prathiv7 Prathiv7 changed the title Unable to automatically generate include 3rd part(matrix-sdk) Unable to automatically generate include 3rd party sdk (matrix-sdk) Jan 8, 2025
@fzyzcjy
Copy link
Owner

fzyzcjy commented Jan 8, 2025

Hmm, briefly checked the matrix-sdk crate, looks like it contains a lot of advanced syntax such as closure with async types. Thus I guess frb will not be able to fully automatically translate such complex packages yet. At the same time, you can always manually do the glueing https://cjycode.com/flutter_rust_bridge/guides/third-party/manual.

@fzyzcjy fzyzcjy added experimental feature Issues related to experimental instead of stable features and removed bug Something isn't working labels Jan 8, 2025
@Prathiv7
Copy link
Author

Prathiv7 commented Jan 8, 2025

So we need to manually mark all the methods in our main crates with #[frb(external)]
to make the flutter_rust_bridge_codegen generate to build dart function ?

like
cargo install flutter_rust_bridge_codegen && flutter_rust_bridge_codegen create my_app do we have any live examples ?

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jan 9, 2025

Example: https://github.com/fzyzcjy/flutter_rust_bridge/tree/master/frb_example/integrate_third_party/rust

@Prathiv7
Copy link
Author

Prathiv7 commented Jan 13, 2025

Just playing around with frb to know how it works and found after marking a struct as non_opaque also getting issue while generating code and if the struct is marked as opaque no issue
the name of struct & traits may not be relevant , it's just to try some combination

use flutter_rust_bridge::frb;
use rand::Rng;
#[frb(non_opaque)]
pub struct Server {
    pub home_server_url: String,
    pub user_name: String,
    pub password: String,
}

impl Server {
    pub fn login(&self) -> Result<String, bool> {
        let mut rng = rand::thread_rng();
        let random_bool = rng.gen_bool(0.5);

        if random_bool {
            Ok("login Token".to_string())
        } else {
            Err(false)
        }
    }

    pub fn default_server(&self) -> Server {
        Server {
            home_server_url: "".to_string(),
            password: "".to_string(),
            user_name: "".to_string(),
        }
    }

    pub fn update_url<'a, T>(server: &'a mut T) -> &'a String
    where
        T: ServerCore,
    {
        let url = server.return_url();
        url.insert_str(0, "updated url");

        url
    }
}

impl ServerCore for Server {
    fn return_url<'a>(&'a mut self) -> &'a mut String {
        &mut self.home_server_url
    }
}

pub trait ServerCore {
    fn return_url<'a>(&'a mut self) -> &'a mut String;
}

Error: Will generate duplicated class names (["Server"]). This is often because the type is auto inferred as both opaque and non-opaque. Try to add #[frb(opaque)] or #[frb(non_opaque)] to the struct, or change code that uses it.Another way to debug is to temporarily set environment variable FRB_DEBUG_SKIP_SANITY_CHECK_CLASS_NAME_DUPLICATES=1 and check the generated code.

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jan 13, 2025

For this error - hmm is it possible you have a struct named Server somewhere else in your repo?

@Prathiv7
Copy link
Author

To make sure the name is unique , changed it to Server54325 but still same error

Error: Will generate duplicated class names (["Server54325"]). This is often because the type is auto inferred as both opaque and non-opaque. Try to add #[frb(opaque)] or #[frb(non_opaque)] to the struct, or change code that uses it.Another way to debug is to temporarily set environment variable FRB_DEBUG_SKIP_SANITY_CHECK_CLASS_NAME_DUPLICATES=1 and check the generated code.

@Prathiv7
Copy link
Author

Prathiv7 commented Jan 13, 2025

@fzyzcjy When the trait impl of ServerCore is removed it's generating properly.

is there any chance if a struct has both impl and trait impl . Then it would case issue ?

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jan 13, 2025

Hmm I guess that may be a bug. Could you please try to make a more minimal reproducible sample? For example,

use flutter_rust_bridge::frb;

#[frb(non_opaque)]
pub struct MyStruct {
    pub a: String,
}

impl MyStruct {
    pub fn f(&self) {}
}

impl MyTrait for MyStruct {
    fn g(&self) {}
}

pub trait MyTrait {
    fn g(&self);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
experimental feature Issues related to experimental instead of stable features
Projects
None yet
Development

No branches or pull requests

2 participants