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.27k stars 300 forks source link

Use #[frb(ignore)] on struct fields #2201

Closed bryanmehall closed 2 months ago

bryanmehall commented 4 months ago

I'm having issues translating a struct where one of the fields is from an external crate and I don't care about its contents in flutter. Something like:

pub struct MyStruct {
    pub field1: String,
    pub other_field: ExternalStruct,
}

When I run codegen, I get a cannot find type ExternalStruct in this scope compiler error on frb_generated.rs.

The doc page on ignoring functions says: "Currently #[frb(ignore)] doesn't support enums and structs yet - feel free to open an issue if your scenario needs it."

It would be useful to have an ignore macro for struct fields:

pub struct MyStruct {
    pub field1: String,
    #[frb(ignore)]
    pub other_field: ExternalStruct,
}

I've tried making the ExternalStruct opaque but still get errors and would prefer not to use RustAutoOpaque because the MyStruct type is used elsewhere.

Not sure if ignoring fields is possible with the memory model of flutter rust bridge but I'm happy to help with changes.

welcome[bot] commented 4 months ago

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

fzyzcjy commented 4 months ago

That looks reasonable. I guess it is not hard if the struct is only used as a return type, because we can simply treat the field as not existing at all and done. But if it is used as an argument, maybe we need to explicitly ask users to provide a value.

Btw, I wonder whether https://cjycode.com/flutter_rust_bridge/guides/types/translatable/custom will help? For example,

but I'm happy to help with changes.

Looking forward to your PR!

bryanmehall commented 4 months ago

I tried the custom translation and it still had the same Rust compiler error. Maybe I am missing something but it didn't seem to affect the rust bindings at all, just the generated dart code. That's a good point with arguments. I'll dig into it a bit and see how to make it work. Thanks!

fzyzcjy commented 4 months ago

You are welcome!

Btw, you can paste the whole error stack trace and that may be slightly more helpful in debugging when is going on.

bryanmehall commented 3 months ago

Ok, after some thought I think the best way to solve it is with the custom encoder. That prevents the problem you mentioned of translating a type back to rust. I am running into the same issue as here #2212 with the into_into_dart compiler error. I spent the afternoon trying to figure out how to add the IntoIntoDart trait to codegen so I could create a PR but couldn't figure it out. Any ideas on where that should be added in? It looks like the IntoDart and IntoIntoDart traits are only generated for the Dco codec mode. Once that is fixed I'll add to the custom encoder docs explaining how to make it work with third-party types and use it to ignore struct fields.

fzyzcjy commented 3 months ago

Any ideas on where that should be added in?

For normal third party structs, that is created when MirStruct.mirror=true. Maybe mimic that code path and see what is going on.

fzyzcjy commented 2 months ago

See https://github.com/fzyzcjy/flutter_rust_bridge/issues/2212#issuecomment-2345141553 for updates. Feel free to reopen if you have any questions!

github-actions[bot] commented 1 month 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.