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

Generate code for `Default` trait's functions #2331

Closed patmuk closed 2 weeks ago

patmuk commented 1 month ago

Is your feature request related to a problem? Please describe. In the following struct I implemented Default:

#[derive(Debug)]
pub struct TodoListModel {
    pub items: RustAutoOpaque<Vec<String>>,
}

impl Default for TodoListModel {
    fn default() -> Self {
        TodoListModel {
            items: RustAutoOpaque::new(Vec::default()),
        }
    }
}

While there is a constructor

  const TodoListModel({
    required this.items,
  });

There is no default() method available. As items is RustAutoOpaque I can't initialize it on the Dart side (VecString).

Describe the solution you'd like Translating the default() function from the Trait to a Dart call as well. If possible doing so for all trait functions.

Describe alternatives you've considered I am currently figuring out how I can do this without a default() call :)

Additional context

fzyzcjy commented 1 month ago

IIRC we disable by default when the traits satisfies some condition (third party?) for sanity reasons. Try to put an empty #[frb] on it, i.e. #[frb] impl Default ... or try #[frb] fn default() and see whether it works.

(We should have better doc for that!)

fzyzcjy commented 1 month ago

Oh there seems already have doc: https://cjycode.com/flutter_rust_bridge/guides/traits/implementations

Some trait implementations are ignored by default to avoid generating meaningless things to Dart side such as clone and deref. However, if a function is ignored while you want it, you can put arbitrary attributes on it to tell flutter_rust_bridge you want it. For example, #[frb] fn f() { .. } suffices. Attributes with real contents like #[frb(sync)] fn f() { .. } also works.

patmuk commented 1 month ago

Ah, super :)

But it doesn't work for me ... maybe because of the RustAutoOpaque? However, function is function ... I tried it with #[frb] and #[frb(sync)], note before impl MyTrait and fn f().

In the doc, did you mean

impl MyTrait for MyStruct {
    #[frb] // marker to have this translated to dart
    fn f(&self, a: String) -> i32 { ... }
}

instead of

impl MyTrait for MyStruct {
    fn f(&self, a: String) -> i32 { ... }
}

?

fzyzcjy commented 1 month ago

Hmm...

Firstly, the workaround is to make another function (that calls this trait method).

Secondly, feel free to debug and fix it! I am happy to provide hints if you like.

In the doc, did you mean

If it is a custom trait (unlike Default etc) then it already works, without any extra #[frb]

fzyzcjy commented 2 weeks ago

I checked frb_example and default seems to work. For example, this one in frb_example/pure_dart:

image

is auto generated from:

image

fzyzcjy commented 2 weeks ago

Thus close this issue, but feel free to reopen if you have any questions!

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.