Closed NightBlaze closed 1 month ago
Firstly, does pub fn enum_foo(p: MyEnum) -> String {
instead of pub fn enum_foo(p: &MyEnum) -> String {
work? I suspect it is because the &MyEnum
automatically generates an opaque object.
Firstly, does
pub fn enum_foo(p: MyEnum) -> String {
instead ofpub fn enum_foo(p: &MyEnum) -> String {
work? I suspect it is because the&MyEnum
automatically generates an opaque object.
Yep, it works. But there is another issue. If MyStruct
will have a func with self
(not &self
, &mut self
) then it'll be an error on generation phase because MyEnum
became non opaque.
-> % flutter_rust_bridge_codegen generate
[12.5s] Parse
└── [12.2s] Run cargo expand
└── [0.2s] Parse source graph
Error: function=Ident { sym: consume, span: bytes(1513..1520) }
Caused by:
If you want to use `self`/`&mut self`, please make the struct opaque (by adding `#[frb(opaque)]` on the struct).
It will force to make MyStruct
opaque too and it can break architecture.
The snippet that gives the error on generate phase
#[derive(Debug)]
pub enum MyEnum {
Abc
}
#[flutter_rust_bridge::frb(sync)]
pub fn enum_foo(p: MyEnum) -> String {
format!("{:?}", p)
}
#[derive(Debug)]
pub struct MyStruct {
pub field: MyEnum
}
impl MyStruct {
#[flutter_rust_bridge::frb(sync)]
pub fn foo(&self) -> String {
format!("{:?}", self)
}
#[flutter_rust_bridge::frb(sync)]
pub fn consume(self) {
}
}
If MyStruct will have a func with self (not &self, &mut self) then it'll be an error on generation phase because MyEnum became non opaque.
That's true, only &self
is supported on non-opaque currently (though I do agree self
may make sense!).
So what is the desired code that you want to write but fails? (For that snippet, I guess it may work if changing consume(self)
into &self
)
So what is the desired code that you want to write but fails? (For that snippet, I guess it may work if changing
consume(self)
into&self
)
After changing &MyEnum
to MyEnum
all my code works. But I can image a situation when a developer will need a struct with MyEnum
field and a function with self
on the struct.
So it's nice to have the issue to be fixed but it's not high priority because there are workarounds (using MyEnum
, don't use self
or explicitly set a struct as opaque) even if these workarounds will make a code not such clear and beautiful as it can be.
But I can image a situation when a developer will need a struct with MyEnum field and a function with self on the struct.
Totally agree. I am a bit hesitate about the design choice that, when should a struct be automatically judged as opaque, and when for non-opaque?
The drawbacks may include:
arg: &MyType
, users may wrongly think that, the object is passed by reference - but indeed they are copied (when non-opaque).self
, users may wrongly think that, the object will really be consumed, and there is runtime check to ensure nobody can use it after this call.However, I guess the proposed change should be good, since we already have owned-type (the MyEnum
) and ref-type (the &self
) scenarios.
Firstly, https://github.com/fzyzcjy/flutter_rust_bridge/pull/1876 for better error hint
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.
Describe the bug
This issue is related to https://github.com/fzyzcjy/flutter_rust_bridge/issues/1870.
Because I need functions for enum and
impl
section is ignored by frb generator I decided to create a public function that accepts enum:Also the enum uses in a struct
This combination leads to incorrect Dart code
The error is
The name 'MyEnum' is already defined.
because there areenum MyEnum
andclass MyEnum
.There are also errors in
frb_generated.dart
. All the errors in the file are related to encoding/decoding likeThe method 'dcoDecode' isn't defined for the type 'MyEnum'.
Steps to reproduce
flutter_rust_bridge_codegen create frb_enum
simple.rs
[flutter_rust_bridge::frb(sync)]
pub fn enum_foo(p: &MyEnum) -> String { format!("{:?}", p) }
[derive(Debug)]
pub struct MyStruct { pub field: MyEnum }
impl MyStruct {
[flutter_rust_bridge::frb(sync)]
}
Expected behavior
No response
Generated binding code
No response
OS
No response
Version of
flutter_rust_bridge_codegen
2.0.0-dev.30
Flutter info
Version of
clang++
No response
Additional context
No response