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.12k stars 283 forks source link

Question: What is the recommended approach of passing a data type with private field/type? #829

Closed momotaro1105 closed 1 year ago

momotaro1105 commented 1 year ago

Have been working on the 'curve25519-dalek' crate in Rust to generate a private and public keypair in flutter. However, the 'PublicKey' struct, defined externally within the aforementioned crate contains nested fields that are defined in private/internal modules, and hence cannot be re-exported to 'api.rs'.

What would be the best practice to handle this? Here is an example;

pub use curve25519_dalek::{
    edwards::{EdwardsPoint},
    ristretto::{CompressedRistretto, RistrettoPoint},
    scalar::{Scalar},
    field::{FieldElement, FieldElement51},          // module 'field' is private and throws an error on compile
};
pub use schnorrkel::{
    keys::{
        Keypair, 
        SecretKey, 
        PublicKey
    },
    points::{RistrettoBoth},
    sign::{Signature},
};

// keypair
#[frb(mirror(Keypair))]
pub struct _Keypair {
    pub secret: SecretKey,
    pub public: PublicKey,
}

// public key
#[frb(mirror(PublicKey))]
pub struct _PublicKey(pub (crate) RistrettoBoth);

...........

#[frb(mirror(EdwardsPoint))]
pub struct _EdwardsPoint {
    pub(crate) X: FieldElement,
    pub(crate) Y: FieldElement,
    pub(crate) Z: FieldElement,
    pub(crate) T: FieldElement,
}

#[frb(mirror(FieldElement))] 
pub type _FieldElement = backend::serial::u64::field::FieldElement51;

#[frb(mirror(FieldElement51))] 
pub struct _FieldElement51(pub (crate) [u64; 5]);
welcome[bot] commented 1 year ago

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

fzyzcjy commented 1 year ago

does this help? -> https://github.com/fzyzcjy/flutter_rust_bridge/pull/795

momotaro1105 commented 1 year ago

does this help? -> #795

Thank you. Having a look, might take some time for me to get a good understanding.

momotaro1105 commented 1 year ago

Sorry for late, I do think the implementation of opaque types is what I am looking for, but not confident enough to say for sure until I actually use - it given my lack of programming experience.

In the meantime, I decided to power through it. While I know it is NOT best practice, I made changes to the external dependencies to turn some private data types/modules to public and then re-exporting the same to api.rs. In doing so, I noticed that cargo update or ctrl + s doesn't properly reflect the changes to the larger flutter project. Is there an alternative command that I can run? I'm sorry if I missed it in the official docs.

fzyzcjy commented 1 year ago

that sounds like a hack. a way is to create your own fork of source code and modify it.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

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