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.26k stars 300 forks source link

[Bug] mirroring not working when struct has another optional struct #897

Closed codercengiz closed 1 year ago

codercengiz commented 1 year ago

Describe the bug

When your struct has another optional struct, it gives cannot be dereferenced error.

pub struct Parent {
       pub child: Option<Child>
}
pub struct Child {
       pub name: String
}

image

I changed only pure_dart example https://github.com/fzyzcjy/flutter_rust_bridge/blob/master/frb_example/pure_dart/rust/external/src/lib.rs

diff --git a/frb_example/pure_dart/rust/external/src/lib.rs b/frb_example/pure_dart/rust/external/src/lib.rs
index c44feb76..b97c4adf 100644
--- a/frb_example/pure_dart/rust/external/src/lib.rs
+++ b/frb_example/pure_dart/rust/external/src/lib.rs
@@ -6,6 +6,12 @@ pub struct ApplicationSettings {
     pub version: String,
     pub mode: ApplicationMode,
     pub env: Box<ApplicationEnv>,
+    pub child: Option<ChildApplicationSetting>,
+}
+
+#[derive(Debug, Clone)]
+pub struct ChildApplicationSetting {
+    pub name: String,
 }

 #[derive(Debug, Clone)]
@@ -52,6 +58,7 @@ impl ApplicationSettings {
                     .map(|(k, v)| ApplicationEnvVar(k.into(), v))
                     .collect(),
             }),
+            child: None,
         }
     }
 }

https://github.com/fzyzcjy/flutter_rust_bridge/blob/master/frb_example/pure_dart/rust/src/api.rs

 diff --git a/frb_example/pure_dart/rust/src/api.rs b/frb_example/pure_dart/rust/src/api.rs
index d5b617a6..ba7e4c0c 100644
--- a/frb_example/pure_dart/rust/src/api.rs
+++ b/frb_example/pure_dart/rust/src/api.rs
@@ -526,7 +526,7 @@ pub fn use_imported_enum(my_enum: MyEnum) -> bool {
 // To use an external type with mirroring, it MUST be imported publicly (aka. re-export)
 pub use external_lib::{
     ApplicationEnv, ApplicationEnvVar, ApplicationMessage, ApplicationMode, ApplicationSettings,
-    Numbers, Sequences,
+    ChildApplicationSetting, Numbers, Sequences,
 };

 // To mirror an external struct, you need to define a placeholder type with the same definition
@@ -536,6 +536,12 @@ pub struct _ApplicationSettings {
     pub version: String,
     pub mode: ApplicationMode,
     pub env: Box<ApplicationEnv>,
+    pub child: Option<ChildApplicationSetting>,
+}
+
+#[frb(mirror(ChildApplicationSetting))]
+pub struct _ChildApplicationSetting {
+    pub name: String,
 }

 #[frb(mirror(ApplicationMode))]

Codegen logs with RUST_LOG=debug environment variable

cd frb_codegen && cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.05s
(cd frb_example/with_flutter && flutter pub get)
Running "flutter pub get" in with_flutter...                     1,282ms
cargo run --manifest-path frb_codegen/Cargo.toml -- -r frb_example/with_flutter/rust/src/api.rs -d frb_example/with_flutter/lib/bridge_generated.dart --dart-decl-output frb_example/with_flutter/lib/bridge_definitions.dart -c frb_example/with_flutter/ios/Runner/bridge_generated.h -c frb_example/with_flutter/macos/Runner/bridge_generated.h --dart-format-line-length 120 --wasm
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/flutter_rust_bridge_codegen -r frb_example/with_flutter/rust/src/api.rs -d frb_example/with_flutter/lib/bridge_generated.dart --dart-decl-output frb_example/with_flutter/lib/bridge_definitions.dart -c frb_example/with_flutter/ios/Runner/bridge_generated.h -c frb_example/with_flutter/macos/Runner/bridge_generated.h --dart-format-line-length 120 --wasm`
[2022-12-15T11:43:46Z WARN  lib_flutter_rust_bridge_codegen::source_graph] Skipping unresolvable module web (tried /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/web.rs, /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/web/mod.rs, /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/bridge_generated/web.rs, /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/bridge_generated/web/mod.rs, )
[2022-12-15T11:43:46Z WARN  lib_flutter_rust_bridge_codegen::source_graph] Skipping unresolvable module io (tried /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/io.rs, /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/io/mod.rs, /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/bridge_generated/io.rs, /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/bridge_generated/io/mod.rs, )
Flutter 3.3.9 • channel stable • https://github.com/flutter/flutter.git
Framework • revision b8f7f1f986 (3 weeks ago) • 2022-11-23 06:43:51 +0900
Engine • revision 8f2221fbef
Tools • Dart 2.18.5 • DevTools 2.15.0
[2022-12-15T11:43:47Z INFO  lib_flutter_rust_bridge_codegen] Picked config: Opts { rust_input_path: "/home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/api.rs", dart_output_path: "/home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/lib/bridge_generated.dart", dart_decl_output_path: Some("/home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/lib/bridge_definitions.dart"), c_output_path: ["/home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/ios/Runner/bridge_generated.h", "/home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/macos/Runner/bridge_generated.h"], rust_crate_dir: "/home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust", rust_output_path: "/home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/bridge_generated.rs", class_name: "FlutterRustBridgeExample", dart_format_line_length: 120, skip_add_mod_to_lib: false, llvm_path: ["/opt/homebrew/opt/llvm", "/usr/local/opt/llvm", "/usr/lib/llvm-9", "/usr/lib/llvm-10", "/usr/lib/llvm-11", "/usr/lib/llvm-12", "/usr/lib/llvm-13", "/usr/lib/llvm-14", "/usr/lib/", "/usr/lib64/", "C:/Program Files/llvm", "C:/msys64/mingw64"], llvm_compiler_opts: "", manifest_path: "/home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/Cargo.toml", dart_root: Some("/home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter"), build_runner: true, block_index: BlockIndex(0), skip_deps_check: false, wasm_enabled: true, inline_rust: false }
[2022-12-15T11:43:47Z INFO  lib_flutter_rust_bridge_codegen] Phase: Parse source code to AST, then to IR
[2022-12-15T11:43:47Z WARN  lib_flutter_rust_bridge_codegen::source_graph] Skipping unresolvable module web (tried /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/web.rs, /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/web/mod.rs, /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/bridge_generated/web.rs, /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/bridge_generated/web/mod.rs, )
[2022-12-15T11:43:47Z WARN  lib_flutter_rust_bridge_codegen::source_graph] Skipping unresolvable module io (tried /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/io.rs, /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/io/mod.rs, /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/bridge_generated/io.rs, /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/rust/src/bridge_generated/io/mod.rs, )
[2022-12-15T11:43:47Z INFO  lib_flutter_rust_bridge_codegen] Phase: Transform IR
[2022-12-15T11:43:47Z INFO  lib_flutter_rust_bridge_codegen] Phase: Generate Rust code
[2022-12-15T11:43:47Z INFO  lib_flutter_rust_bridge_codegen] Phase: Generate Dart code
[2022-12-15T11:43:47Z INFO  lib_flutter_rust_bridge_codegen] Phase: Generating Dart bindings for Rust
Running in Directory: '/home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter'
Input Headers: [/tmp/.tmpub9YFD.h]
[WARNING]: No definition found for declaration - (Cursor) spelling: _Dart_Handle, kind: 2, kindSpelling: StructDecl, type: 105, typeSpelling: struct _Dart_Handle, usr: c:@S@_Dart_Handle
[WARNING]: No definition found for declaration - (Cursor) spelling: _Dart_Handle, kind: 2, kindSpelling: StructDecl, type: 105, typeSpelling: struct _Dart_Handle, usr: c:@S@_Dart_Handle
[WARNING]: No definition found for declaration - (Cursor) spelling: _Dart_Handle, kind: 2, kindSpelling: StructDecl, type: 105, typeSpelling: struct _Dart_Handle, usr: c:@S@_Dart_Handle
[WARNING]: No definition found for declaration - (Cursor) spelling: _Dart_Handle, kind: 2, kindSpelling: StructDecl, type: 105, typeSpelling: struct _Dart_Handle, usr: c:@S@_Dart_Handle
[WARNING]: No definition found for declaration - (Cursor) spelling: _Dart_Handle, kind: 2, kindSpelling: StructDecl, type: 105, typeSpelling: struct _Dart_Handle, usr: c:@S@_Dart_Handle
[WARNING]: No definition found for declaration - (Cursor) spelling: _Dart_Handle, kind: 2, kindSpelling: StructDecl, type: 105, typeSpelling: struct _Dart_Handle, usr: c:@S@_Dart_Handle
[WARNING]: Generated declaration '_Dart_Handle' start's with '_' and therefore will be private.
Finished, Bindings generated in /tmp/.tmpidyEe3
[2022-12-15T11:43:51Z INFO  lib_flutter_rust_bridge_codegen] Phase: Running build_runner
[2022-12-15T11:43:51Z INFO  lib_flutter_rust_bridge_codegen] Phase: Formatting Dart code
Formatted /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/lib/bridge_generated.dart
Formatted /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/lib/bridge_definitions.dart
Formatted /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/lib/bridge_generated.web.dart
Formatted /home/cengiz/StudioProjects/flutter_rust_bridge/frb_example/with_flutter/lib/bridge_generated.io.dart
Formatted 4 files (4 changed) in 0.30 seconds.
[2022-12-15T11:43:51Z INFO  lib_flutter_rust_bridge_codegen] Success!
[2022-12-15T11:43:51Z INFO  flutter_rust_bridge_codegen] Now go and use it :)

To Reproduce

No response

Expected behavior

No response

Generated binding code

No response

OS

Archlinux

Version of flutter_rust_bridge_codegen

1.54

Flutter info

No response

Version of clang++

No response

Version of ffigen

No response

Additional context

No response

fzyzcjy commented 1 year ago

Looks similar to https://github.com/fzyzcjy/flutter_rust_bridge/issues/895 , the mirroring logic needs to be fixed

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.