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.29k stars 301 forks source link

Issue with async_trait #2305

Closed anidotnet closed 2 weeks ago

anidotnet commented 2 months ago

Describe the bug

I am facing code generation issue with simple rust code involving async_trait. I may be wrong, but could not fix it. The frb_generated.rs file has lots of error in it. I have added enable_lifetime: true in flutter_rust_bridge.yaml file.

Steps to reproduce

  1. Modify the simple.rs file from the code generated by the template as follows:
// simple.rs

use std::fmt::Display;
use std::io::Error;
use async_trait::async_trait;
use flutter_rust_bridge::frb;

#[frb] // Synchronous mode for simplicity of the demo
pub async fn greet(name: String) -> String {
    //sleep
    tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
    format!("Hello, {name}!")
}

#[frb(init)]
pub fn init_app() {
    // Default utilities - feel free to customize
    flutter_rust_bridge::setup_default_user_utils();
}

#[frb]
pub struct Email {}

impl Display for Email {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Email")
    }
}

#[frb]
#[async_trait]
pub trait EmailService {
    async fn send_email(&self, email: Email) -> Result<(), Error>;
}

#[frb]
pub struct EmailServiceImpl {
    pub dummy: i32,
}

#[frb]
#[async_trait]
impl EmailService for EmailServiceImpl {
    async fn send_email(&self, email: Email) -> Result<(), Error> {
        tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;

        println!("Email sent {}", email);

        Ok(())
    }
}
  1. Add below dependenciies in Cargo.toml file
tokio = { version = "1.34.0", features = ["time"] }
async-trait = "0.1.82"
  1. Run below command
flutter_rust_bridge_codegen generate

At this point the frb_generated.rs file has lots of error.

  1. Try running the flutter app. It will display error logs and fails

Logs

Due to max character length restriction in github issues, log is pasted here - https://gist.github.com/anidotnet/bec9c144c81e7270fc458e053397419d

Expected behavior

No response

Generated binding code


// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.4.0.

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';

// These function are ignored because they are on traits that is not defined in current crate (put an empty `#[frb]` on it to unignore): `fmt`

Future<String> greet({required String name}) =>
    RustLib.instance.api.crateApiSimpleGreet(name: name);

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<:: Pin < Box < Future < Output = Result < () , Error > > + Send + 'async_trait > >>>
abstract class PinBoxFutureOutputResultErrorAsyncTrait
    implements RustOpaqueInterface {}

abstract class EmailService {
  Future<PinBoxFutureOutputResultErrorAsyncTrait> sendEmail(
      {required Email email});
}

class Email {
  const Email();

  @override
  int get hashCode => 0;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Email && runtimeType == other.runtimeType;
}

class EmailServiceImpl {
  final int dummy;

  const EmailServiceImpl({
    required this.dummy,
  });

  Future<PinBoxFutureOutputResultErrorAsyncTrait> sendEmail(
          {required Email email}) =>
      RustLib.instance.api
          .crateApiSimpleEmailServiceImplSendEmail(that: this, email: email);

  @override
  int get hashCode => dummy.hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is EmailServiceImpl &&
          runtimeType == other.runtimeType &&
          dummy == other.dummy;
}

OS

MacOS

Version of flutter_rust_bridge_codegen

2.4.0

Flutter info

[✓] Flutter (Channel stable, 3.24.1, on macOS 15.0 24A335 darwin-arm64, locale en-IN)
    • Flutter version 3.24.1 on channel stable at /Users/anindya/development/sdk/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5874a72aa4 (4 weeks ago), 2024-08-20 16:46:00 -0500
    • Engine revision c9b9d5780d
    • Dart version 3.5.1
    • DevTools version 2.37.2

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/anindya/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/anindya/Library/Android/sdk
    • Java binary at: /Users/anindya/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 16.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16A242d
    • 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 /Users/anindya/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 Ultimate Edition (version 2024.2.2)
    • IntelliJ at /Users/anindya/Applications/IntelliJ IDEA Ultimate.app
    • Flutter plugin version 81.1.3
    • Dart plugin version 242.22855.32

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

[✓] Connected device (3 available)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 15.0 24A335 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 15.0 24A335 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 128.0.6613.139

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

• No issues found!

Version of clang++

16

Additional context

No response

welcome[bot] commented 2 months ago

Hi! Thanks for opening your first issue here! :smile:

fzyzcjy commented 2 months ago

Hmm, is this related -> https://github.com/fzyzcjy/flutter_rust_bridge/pull/1848

fzyzcjy commented 2 weeks ago

Close since inactivity and possibly has duplication. But feel free to reopen if needed!

github-actions[bot] commented 3 days 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.