-
I am using a third-party library / crate - for which I don't have any write / modification access at all.
( all three defined in the 3rd-party crate - for which I have no read / modification rights. use thiserror::Error;
#[derive(Error, Debug)]
pub enum CalcError {
#[error("overflow Error")]
OverflowError,
}
pub trait Calc {
fn add(a: u32, b: u32) -> u32;
fn overflow_add(a: u32, b: u32) -> Result<u32, CalcError>;
}
pub struct MyCalc {
}
impl Calc for MyCalc {
fn add(a: u32, b: u32) -> u32 {
a + b
}
fn overflow_add(a: u32, b: u32) -> Result<u32, CalcError> {
Err(CalcError::OverflowError)
}
} Now - to use that traits in my
pub use external_crate::{Calc, MyCalc};
use flutter_rust_bridge::frb;
#[flutter_rust_bridge::frb]
pub fn new_calc() -> impl Calc {
MyCalc{}
} Running Cmd
Output: /home/myuser/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/flutter_rust_bridge_codegen-2.8.0/src/library/codegen/parser/mir/parser/function/real/mod.rs:115] parse_function see error and skip function: function="new_calc" error=when trying to parse DartFn
Caused by:
Unknown ident Calc So - this trait The question is - how do I tell Is there anything I need to do to redefine the same again ? Can you help point to some docs as I could not find anything explaining as appropriate ? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 9 replies
-
Hmm firstly, maybe try not to use |
Beta Was this translation helpful? Give feedback.
-
rust_input: crate::api,<my_external_crate_name> Get past the given issue - now I am getting a different error though , as per the following
Error received:
I am not sure what the origin of this error might be. Any ideas ? |
Beta Was this translation helpful? Give feedback.
-
It seems there is no knowledge in the generated code for the external trait defined in 3rd-party libraries / crates. Hence manually added the definitions right here ( copying from the trait in the 3rd party library ) #[frb(external)]
impl dyn Calc {
pub fn add(&self, a: u32, b: u32) -> u32 {}
pub fn overflow_add(&self, a: u32, b: u32) -> Result<u32, CalcError> {}
} That change does not work - since it brings a whole lot of compilation errors in So - how do we tell What am I missing ? |
Beta Was this translation helpful? Give feedback.
-
Well - finally got to work around this one - based on what you had suggested. #[frb(ignore)]
type ThirdPartyCalc = Box<dyn Calc + Send + Sync>;
#[frb]
pub struct ThirdPartyWrapper(ThirdPartyCalc);
impl ThirdPartyWrapper {
pub fn add(&self, a: u32, b: u32) -> u32 {
return self.0.add(a, b);
}
pub fn overflow_add(&self, a: u32, b: u32) -> Result<u32, CalcError> {
return self.0.overflow_add(a, b);
}
}
#[flutter_rust_bridge::frb(sync)]
pub fn new_calc() -> ThirdPartyWrapper {
ThirdPartyWrapper(Box::new(MyCalc{}))
} [ Credits : This code is very much inspired by this thread here at - #2172 (comment) . ] The above work certainly compiles in rust and dart , although a bit round about and brittle though. FYI. |
Beta Was this translation helpful? Give feedback.
-
Summary: This works but a little bit convoluted for sure. If there are other better options / suggestions - feel free to add some. |
Beta Was this translation helpful? Give feedback.
-
Submitted a PR https://github.com/fzyzcjy/flutter_rust_bridge/pull/2597 to capture some of the final take-away of this thread for future reference. |
Beta Was this translation helpful? Give feedback.
I will check whether it's a bug later. But a quick workaround is: Write
pub type ThirdPartyCalc(Box<dyn Calc>);
and define methods (which is only one-liners because it does nothing but forward everything to the external type) on the new type.