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

Implement operator overloading methods (for external opaque types)

vhdirk opened this issue · comments

I am implementing a derivation of a Semver in rust. Since there's a fair bit of logic behind it, I want to implement it as an opaque type.
And since the type is implemented in an external rust crate (from: https://cjycode.com/flutter_rust_bridge/guides/miscellaneous/methods), it looks like this:

#[frb(external)]
impl MyVersion {
    #[frb(sync)]
    pub fn new(
        _major: u8,
        _minor: u8,
        _patch: u8,
        _extrabit: u8,
        _modifier: u8,
    ) -> Self {
    }

Now, I'd like to be able to use the dart comparison overloads (==,>,=>, etc) to make the api nicer to work with (Strict reference/pointer equality is not important). In the docs, you describe that this can be implemented using arbitrary dart_code (https://cjycode.com/flutter_rust_bridge/guides/miscellaneous/dart-code). However, that annotation seems only usable on struct definitions, not on impl blocks.
I think that, ideally, operators should be first-class citizens, much like pyo3 handles magic functions. Probably even something simple like function renaming #[frb(name="operator==")] would work, although then you'd still need to somehow declare the need for @overload.

What do you think?

Hi! Thanks for opening your first issue here! 😄

Looks pretty reasonable! One way may be the rename you mentioned, and another way may be utilize the PartialEq / Eq / Ord / ....

Btw I am not very sure, where do you want to add @overload? IMHO the solutions can be done without it.

I meant @override in Dart. Since that is what things like operator== require

I see. Indeed IIRC that is optional - the linter will be unhappy without it, but it is just linter instead of compiler, and you can // ignore_for_file for it