Closed canxin121 closed 3 weeks ago
That complex type does not seem to be supported yet. However, frb does support normal Rust async fn
s. What about using https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html thus to write
pub trait TraitA {
async fn do_something(&self) {
//
}
}
That complex type does not seem to be supported yet. However, frb does support normal Rust
async fn
s. What about using https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html thus to writepub trait TraitA { async fn do_something(&self) { // } }
error[E0038]: the trait `TraitA` cannot be made into an object
--> src\api\mod.rs:17:10
|
17 | struct B(TraitAObject);
| ^^^^^^^^^^^^ `TraitA` cannot be made into an object
|
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> src\api\mod.rs:11:14
|
10 | pub trait TraitA {
| ------ this trait cannot be made into an object...
11 | async fn do_something(&self) -> () {}
| ^^^^^^^^^^^^ ...because method `do_something` is `async`
= help: consider moving `do_something` to another trait
= help: only type `api::A` is seen to implement the trait in this crate, consider using it directly instead
= note: `TraitA` can be implemented in other crates; if you want to support your users passing their own types here, you can't refer to a specific type
There is a crate about it async-trait
This crate's macro translate async fn
to fn -> Pin<Box<dyn Future<Output = _>>>
to make the trait "object safe"
Hmm I see. Then maybe we can fix it like: Whenever we see Pin<Box<dyn Future<Output = WhateverType>>>
, we internally treat it as async fn f() -> WhateverType
. What do you think?
And btw, is there a full list what types needs to be treated like this pined boxed future?
Hmm I see. Then maybe we can fix it like: Whenever we see
Pin<Box<dyn Future<Output = WhateverType>>>
, we internally treat it asasync fn f() -> WhateverType
. What do you think?And btw, is there a full list what types needs to be treated like this pined boxed future?
I'm not sure about other types.
And I currently use a wrapper like below. And it works well.
use std::{future::Future, pin::Pin};
use flutter_rust_bridge::frb;
pub mod simple;
#[frb(ignore)]
struct A();
impl TraitA for A {}
#[frb(ignore)]
pub trait TraitA {
#[frb(ignore)]
fn do_something(&self) -> Pin<Box<dyn Future<Output = ()> + 'static>> {
Box::pin(async {})
}
}
#[frb(ignore)]
type TraitAObject = Box<dyn TraitA + Send + Sync>;
#[frb]
pub struct TraitAObjectWrapper(TraitAObject);
impl TraitAObjectWrapper {
async fn do_something(&self) {
self.0.do_something().await;
}
}
That looks great!
Feel free to PR for the type translation, alternatively I will work on it in the next batch.
I'm afraid I've been busy lately and haven't had time to read the flutter_rust_bridge
code to figure out how to implement it.
No worries
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.
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.
Describe the bug
This minium code translation got 2 errors:
got error:
flutter_rust_bridge::frb_generated_moi_arc_impl_value!( flutter_rust_bridge::for_generated::RustAutoOpaqueInner< Pin<Box<dyn Future
Steps to reproduce
Hint: A simple way to reproduce is to clone and modify the https://github.com/fzyzcjy/flutter_rust_bridge/tree/master/frb_example/dart_minimal example package according to your needs.
Logs
Expected behavior
get the same translation as below async fn code
Generated binding code
OS
Windows 10
Version of
flutter_rust_bridge_codegen
=2.0.0
Flutter info
Version of
clang++
not related
Additional context
no more