fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.

Home Page:https://fzyzcjy.github.io/flutter_rust_bridge/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Public Structs with Private Fields

mike-lloyd03 opened this issue · comments

I've searched all over the docs and the issues page here and haven't found an answer to a question which I think would have been asked by now. Please forgive me if I've missed it.

How can I generate bindings for a struct with private fields?

pub struct Calc {
    db: SqlitePool,
}

I do not want to make db public because there is no need for it on the Dart side and generating mirror bindings for SqlitePool was creating all sorts of difficulties (all of it's fields are private).

Whenever I try to generate bindings for this struct, I get many of these errors in frb_generated.rs:

// Codec=Dco (DartCObject based), see doc to use other codecs
impl flutter_rust_bridge::IntoDart for crate::api::calc::Calc {
    fn into_dart(self) -> flutter_rust_bridge::for_generated::DartAbi {
        [self.db.into_into_dart().into_dart()].into_dart()
    }
}
field `db` of `Calc` is private [E0616]

And furthermore, I'm getting errors that SqlitePool isn't in scope even though there should be no need for frb to generate code for it:

error[E0412]: cannot find type `SqlitePool` in this scope
  --> src/frb_generated.io.rs:20:69
   |
20 | ...enerated::rust_async::RwLock<SqlitePool>>::increment_strong_count(ptr as _);
   |                                 ^^^^^^^^^^ not found in this scope
   |
help: consider importing this type alias
   |
6  + use sqlx::SqlitePool;

I feel like I'm missing something obvious but I can't figure it out. Any help would be greatly appreciated.

Hi! Thanks for opening your first issue here! 😄

Hi, what about adding #[frb(opaque)] to your struct definition - then everything inside it will be opaque.

Thank you! That did the trick.

Question, where is this found in the documentation? I looked for this but even in the opaque section, I don't see anything about the frb(opaque) derive macro.

docs.rs doesn't have anything about this either.

You are welcome!

Doc: https://cjycode.com/flutter_rust_bridge/guides/types/arbitrary/rust-auto-opaque#force-a-non-opaque-type-to-be-opaque I agree it is not trivial to find it, since frb has so many features. But no worries, if you (or anyone else) cannot find something, just create an issue, and I usually reply quickly ;)

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.