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.61k stars 254 forks source link

Error when trying to use: https://github.com/serialport/serialport-rs #1905

Open mpep2 opened 2 weeks ago

mpep2 commented 2 weeks ago

Describe the bug

I got the following error when trying to generate for the above mentioned package:

Error: function=Ident { sym: new, span: bytes(48557..48560) }

Caused by:
    0: when trying to parse DartFn
    1: Unknown ident: Into

Steps to reproduce

  1. Download the mentioned repository code into rust/src/api
  2. rename lib.rs to serialport.rs and add it to mod.rs file
  3. run flutter_rust_bridge_codegen generate

Logs

$ flutter_rust_bridge_codegen generate
[7.3s] Parse  
  └── [7.1s] Run cargo expand  
  └── [0.2s] Parse source graph                                                                                                                                                                            Error: function=Ident { sym: new, span: bytes(48557..48560) }

Caused by:
    0: when trying to parse DartFn
    1: Unknown ident: Into

Expected behavior

No response

Generated binding code

No response

OS

No response

Version of flutter_rust_bridge_codegen

2.0.0-dev.32

Flutter info

[✓] Flutter (Channel beta, 3.22.0-0.1.pre, on macOS 14.4.1 23E224 darwin-arm64, locale en-US)

Version of clang++

No response

Additional context

No response

welcome[bot] commented 2 weeks ago

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

fzyzcjy commented 2 weeks ago

Hi, IMHO generate directly on the whole serialport codebase without modification may not work. This is because, though flutter_rust_bridge understands a lot of Rust syntax, Rust is so flexible that there are still scenarios that is not supported. As for your specific case, I guess it may be something like Into<Sth> together with some fn that makes flutter_rust_bridge confused.

To debug this, one way is to add --verbose and see which one gives error. Another way is to bisect the code (comment out some of them and see results). In addition, you can also keep the original crate untouched, and create a thin wrapper in your crate to call the original crate.

mpep2 commented 2 weeks ago

I remove Into from the code and now I get this error:

[2024-04-29T12:56:06.215Z ERROR flutter_rust_bridge_codegen-2.0.0-dev.32/src/library/utils/logs.rs:52] panicked at .cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.0.0-dev.32/src/library/codegen/polisher/auto_upgrade.rs:58:41:
no entry found for key
thread 'main' panicked at .cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.0.0-dev.32/src/library/codegen/polisher/auto_upgrade.rs:58:41:
no entry found for key
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
mpep2 commented 2 weeks ago

I succeeded in generating the code but now the problem is that the types are no translated e.g. u8 is translated as:

class U8 extends RustOpaque {
  U8.dcoDecode(List<dynamic> wire) : super.dcoDecode(wire, _kStaticData);

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

  static final _kStaticData = RustArcStaticData(
    rustArcIncrementStrongCount:
        RustLib.instance.api.rust_arc_increment_strong_count_U8,
    rustArcDecrementStrongCount:
        RustLib.instance.api.rust_arc_decrement_strong_count_U8,
    rustArcDecrementStrongCountPtr:
        RustLib.instance.api.rust_arc_decrement_strong_count_U8Ptr,
  );
}
fzyzcjy commented 2 weeks ago

Hi, it would be great to provide a minimal reproducible sample to see what is going on.

mpep2 commented 2 weeks ago

I am using simple template with 2 files (https://github.com/berkowski/tokio-serial/tree/master/src). with lib.rs renamed to serialport.rs Cargo file:

[lib]
crate-type = ["cdylib", "staticlib"]

[dependencies]
flutter_rust_bridge = "=2.0.0-dev.32"

[features]
default = []
libudev = ["mio-serial/libudev"]
rt = ["tokio/rt-multi-thread"]
codec = ["tokio-util/codec", "bytes"]

[dependencies.futures]
version = "^0.3"

[dependencies.tokio]
version = "^1.8"
default-features = false
features = ["net"]

[dependencies.tokio-util]
version = "^0.7"
default-features = false
optional = true

[dependencies.mio-serial]
version = "^5.0.3"
default-features = false

[dependencies.bytes]
version = "^1"
default-features = false
optional = true

[dependencies.log]
version = "^0.4"

[dependencies.cfg-if]
version = "^1"

[dev-dependencies.tokio]
version = "^1.8"
features = ["macros", "rt", "process", "time", "fs", "io-util"]
default-features = false

[dev-dependencies.env_logger]
version = "^0.10.0"

mod.rs:

pub mod serialport;
pub mod frame;
fzyzcjy commented 2 weeks ago

Briefly checked it, and I suspect it is &[u8] that confuses flutter_rust_bridge. Maybe we can mimic how to deal with &str (by treating it as a String) to work on &[u8] (by treating it as Vec<u8>).

As a workaround, you can try to modify it manually to Vec<u8> (and change a few lines of code to adapt to it) before this feature is implemented

patmuk commented 3 days ago

@mpep2 how did you solve this error?

[2024-04-29T12:56:06.215Z ERROR flutter_rust_bridge_codegen-2.0.0-dev.32/src/library/utils/logs.rs:52] panicked at .cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.0.0-dev.32/src/library/codegen/polisher/auto_upgrade.rs:58:41:
no entry found for key
thread 'main' panicked at .cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.0.0-dev.32/src/library/codegen/polisher/auto_upgrade.rs:58:41:
no entry found for key
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I get this as well, having changed from v2.0.0-dev.32 to v2.0.0-dev.33.

patmuk commented 3 days ago

Solved it. I accidentally removed

[dependencies]
flutter_rust_bridge = "=2.0.0-dev.33"

from my cargo.toml