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.26k stars 297 forks source link

Skiped async_trait for different crates #2222

Open normalllll opened 2 months ago

normalllll commented 2 months ago

The frb will skip traits with #[async_trait]. The specific manifestation is that a non-existent file is imported into dart (the file where the trait is located) or there are no methods in the struct.

When they are all in the /api crate

#[async_trait]
pub trait MyTrait {
    async fn f1() -> anyhow::Result<String>;
}

pub struct MyStruct1 {}

#[async_trait]
impl MyTrait for MyStruct1 {
    async fn f1() -> anyhow::Result<String> {
        Ok(String::from("hello"))
    }
}

pub fn my_struct1_new() -> MyStruct1 {
   MyStruct1 {}
}

pub fn f2()->String{
    String::from("world")
}
import '../frb_generated.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';

Future<MyStruct1> myStruct1New() =>
    RustLib.instance.api.crateApiBooruClientMyStruct1New();

Future<String> f2() => RustLib.instance.api.crateApiBooruClientF2();

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<:: Pin < Box < Future < Output = Result < String > > + Send + 'async_trait > >>>
abstract class PinBoxFutureOutputResultStringAsyncTrait
    implements RustOpaqueInterface {}

abstract class MyTrait {}

class MyStruct1 {
  const MyStruct1();

  static Future<PinBoxFutureOutputResultStringAsyncTrait> f1() =>
      RustLib.instance.api.crateApiBooruClientMyStruct1F1();

  static Future<PinBoxFutureOutputResultStringAsyncTrait> f1() =>
      RustLib.instance.api.crateApiBooruClientMyStruct1F1();

  @override
  int get hashCode => 0;

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

They can be produced normally.


When they are in different crates.

/api/some.rs

use async_trait::async_trait; use create::...;

pub fn my_struct1_new() -> MyStruct1 {
   MyStruct1 {}
}

pub fn f2()->String{
    String::from("world")
}

/other/my_struct1.rs

pub struct MyStruct1 {}

#[async_trait]
impl MyTrait for MyStruct1 {
    async fn f1() -> anyhow::Result<String> {
        Ok(String::from("hello"))
    }
}

/other/my_trait.rs

#[async_trait]
pub trait MyTrait {
    async fn f1() -> anyhow::Result<String>;
}

/api/some.dart

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

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

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

Future<MyStruct1> myStruct1New() =>
    RustLib.instance.api.crateApiBooruClientMyStruct1New();

Future<String> f2() => RustLib.instance.api.crateApiBooruClientF2();

/some/my_struct1.dart

// This file is automatically generated, so please do not edit it.
// Generated by `flutter_rust_bridge`@ 2.1.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';

class MyStruct1 {
  const MyStruct1();

  @override
  int get hashCode => 0;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is MyStruct1 && runtimeType == other.runtimeType;
}
fzyzcjy commented 2 months ago

Hmm, is it solved by https://github.com/fzyzcjy/flutter_rust_bridge/pull/2189?

normalllll commented 2 months ago

Try:

/other/my_trait.rs

use std::future::Future;

pub trait MyTrait {
    fn f1() -> impl Future<Output=anyhow::Result<String>>;
}

/other/subcrate/my_struct1.rs

pub struct MyStruct1 {}

impl MyTrait for MyStruct1 {
    fn f1() -> impl Future<Output=anyhow::Result<String>> {
        async {
            Ok(String::from("hello"))
        }
    }
}

Generated: /other/subcrate/my_struct1.dart

// This file is automatically generated, so please do not edit it.
// Generated by `flutter_rust_bridge`@ 2.1.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';

class MyStruct1 {
  const MyStruct1();

  @override
  int get hashCode => 0;

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

Traits not in /api/* are still skipped.

fzyzcjy commented 2 months ago

That PR is ongoing, thus I mean does it solve your problem after it is implemented and merged?

stale[bot] commented 3 weeks ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.