fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
3.64k stars 256 forks source link

struct .. { } Impl .. { } Unable to access other methods of the struct #1952

Closed Zacchaeus-Oluwole closed 1 week ago

Zacchaeus-Oluwole commented 2 weeks ago

I am not sure if this should be a feature request but I need a clear explanation on how to go about this .rs

#[frb(non_final)]
#[frb(opaque)]
pub struct MathArithmetic {
    value: i32,
}

impl MathArithmetic {

    pub fn new(initial_value: i32) -> MathArithmetic {
        MathArithmetic {
            value: initial_value,
        }
    }

    pub fn set_value(&mut self, new_value: i32) -> i32 {
        self.value = new_value;
        self.value
    }

    pub fn add(&mut self, amount: i32) -> i32 {
        self.value += amount;
        self.value
    }

    pub fn subtract(&mut self, amount: i32) -> i32 {
        self.value -= amount;
        self.value
    }

    pub fn get_value(&self) -> i32 {
        self.value
    }
}

.dart


import '../frb_generated.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<MathArithmetic>>
@sealed
class MathArithmetic extends RustOpaque {
  MathArithmetic.dcoDecode(List<dynamic> wire)
      : super.dcoDecode(wire, _kStaticData);

  MathArithmetic.sseDecode(int ptr, int externalSizeOnNative)
      : super.sseDecode(ptr, externalSizeOnNative, _kStaticData);

  static final _kStaticData = RustArcStaticData(
    rustArcIncrementStrongCount:
        RustLib.instance.api.rust_arc_increment_strong_count_MathArithmetic,
    rustArcDecrementStrongCount:
        RustLib.instance.api.rust_arc_decrement_strong_count_MathArithmetic,
    rustArcDecrementStrongCountPtr:
        RustLib.instance.api.rust_arc_decrement_strong_count_MathArithmeticPtr,
  );

  Future<int> add({required int amount, dynamic hint}) => RustLib.instance.api
      .mathArithmeticAdd(that: this, amount: amount, hint: hint);

  Future<int> getValue({dynamic hint}) =>
      RustLib.instance.api.mathArithmeticGetValue(that: this, hint: hint);

  // HINT: Make it `#[frb(sync)]` to let it become the default constructor of Dart class.
  static Future<MathArithmetic> newInstance(int i, 
          {required int initialValue, dynamic hint}) =>
      RustLib.instance.api
          .mathArithmeticNew(initialValue: initialValue, hint: hint);

  Future<int> setValue({required int newValue, dynamic hint}) =>
      RustLib.instance.api
          .mathArithmeticSetValue(that: this, newValue: newValue, hint: hint);

  Future<int> subtract({required int amount, dynamic hint}) =>
      RustLib.instance.api
          .mathArithmeticSubtract(that: this, amount: amount, hint: hint);
}

In the above code, i could only call the newInstance and I couldn't call other methods of the MathArithmetic's struct.

var a = MathArithmetic.newInstance(2,initialValue: 2);

The only way could access the other methods is like this which wasn't working

var c = MathArithmetic.sseDecode(ptr, externalSizeOnNative).getValue();
var e = MathArithmetic.dcoDecode(wire).getValue();

All the methods that take self in the above .rs code were affected

welcome[bot] commented 2 weeks ago

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

fzyzcjy commented 2 weeks ago

Hi, it seems that in order to use a Future, Dart language requires await. For example,

var a = await MathArithmetic.newInstance(2,initialValue: 2); // <-- note the `await`
a.getValue();
Zacchaeus-Oluwole commented 1 week ago

Thank you, it works. This issue will be closed down shortly but before that i have questions to ask.

  1. When flutter_rust_bridge is used to build flutter package, does any user that wants to use the package needs to have rust and the necessary rust crate(s) installed?
  2. On which social media platform can someone have a direct conversation with you? Twitter, Linkedin or any?
fzyzcjy commented 1 week ago

When flutter_rust_bridge is used to build flutter package, does any user that wants to use the package needs to have rust and the necessary rust crate(s) installed?

It depends on how you integrate flutter+rust. For example, if using cargokit (the default one), there is no need for package users to do anything.

On which social media platform can someone have a direct conversation with you? Twitter, Linkedin or any?

Usually just here ;) Feel free to ask me anything on GitHub issues! I check GitHub inboxes quite often indeed.

Zacchaeus-Oluwole commented 1 week ago

Alright, thank you🥰. Can i close the issue now?

fzyzcjy commented 1 week ago

Sure and you are welcome!