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

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

Closed
bryanmehall opened this issue Jul 15, 2024 · 8 comments · Fixed by #2294
Closed

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

bryanmehall opened this issue Jul 15, 2024 · 8 comments · Fixed by #2294
Labels
enhancement New feature or request

Comments

@bryanmehall
Copy link

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.

@bryanmehall bryanmehall added the enhancement New feature or request label Jul 15, 2024
Copy link

welcome bot commented Jul 15, 2024

Hi! Thanks for opening your first issue here! 😄

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jul 16, 2024

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,

  • provide custom encode/decode for MyStruct
  • or, provide custom encode/decode for ExternalStruct type

but I'm happy to help with changes.

Looking forward to your PR!

@fzyzcjy fzyzcjy added the awaiting Waiting for responses, PR, further discussions, upstream release, etc label Jul 16, 2024
@bryanmehall
Copy link
Author

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

fzyzcjy commented Jul 16, 2024

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

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

fzyzcjy commented Jul 27, 2024

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

fzyzcjy commented Sep 12, 2024

See #2212 (comment) for updates. Feel free to reopen if you have any questions!

@fzyzcjy fzyzcjy closed this as completed Sep 12, 2024
@fzyzcjy fzyzcjy removed the awaiting Waiting for responses, PR, further discussions, upstream release, etc label Sep 12, 2024
Copy link
Contributor

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants