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

Generate code for Default trait's functions #2331

Open
patmuk opened this issue Sep 30, 2024 · 4 comments
Open

Generate code for Default trait's functions #2331

patmuk opened this issue Sep 30, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@patmuk
Copy link
Contributor

patmuk commented Sep 30, 2024

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

@patmuk patmuk added the enhancement New feature or request label Sep 30, 2024
@fzyzcjy
Copy link
Owner

fzyzcjy commented Sep 30, 2024

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 fzyzcjy changed the title generate code for trait functions (like Default) Generate code for Default trait's functions Sep 30, 2024
@fzyzcjy
Copy link
Owner

fzyzcjy commented Sep 30, 2024

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
Copy link
Contributor Author

patmuk commented Sep 30, 2024

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
Copy link
Owner

fzyzcjy commented Sep 30, 2024

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants