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

Not clear how to use `spawn_*` functions or they don't work

NightBlaze opened this issue · comments

Describe the bug

There is a short docs about spawn_* funcs here https://cjycode.com/flutter_rust_bridge/guides/cross-platform/async
But it's unclear how to call them.

I tried to call then directly but have an error. At the same time I can see tokio threads (is it means that tokio runtime setted up?).
frb_tokio

Steps to reproduce

  1. Add these three functions to default project
pub fn spawn_test() {
    spawn(async {});
}

pub fn spawn_blocking_with_test() {
    spawn_blocking_with( || {}, FLUTTER_RUST_BRIDGE_HANDLER.thread_pool());
}

pub fn spawn_local_test() {
    spawn_local(async {});
}
  1. Call them (uncomment lines)
Future<void> main() async {
  await RustLib.init();
  // await spawnTest();
  // await spawnBlockingWithTest();
  // await spawnLocalTest();
  runApp(const MyApp());
}

For spawn and spawn_blocking_with I have an error:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PanicException(there is no reactor running, must be called from the context of a Tokio 1.x runtimeBacktrace [{ fn: "std::backtrace::Backtrace::create" }, { fn: "std::backtrace::Backtrace::capture" }, { fn: "flutter_rust_bridge::misc::panic_backtrace::PanicBacktrace::setup::{{closure}}" }, { fn: "std::panicking::rust_panic_with_hook" }, { fn: "std::panicking::begin_panic_handler::{{closure}}" }, { fn: "std::sys_common::backtrace::__rust_end_short_backtrace" }, { fn: "_rust_begin_unwind" }, { fn: "core::panicking::panic_fmt" }, { fn: "core::panicking::panic_display" }, { fn: "tokio::task::spawn::spawn_inner::panic_cold_display" }, { fn: "tokio::task::spawn::spawn_inner" }, { fn: "tokio::task::spawn::spawn" }, { fn: "rust_lib::api::simple::spawn_test" }, { fn: "rust_lib::frb_generated::wire_spawn_test_impl::{{closure}}::{{closure}}::{{closure}}" }, { fn: "rust_lib::frb_generated::wire_spawn_test_impl::{{closure}}::{{closure}}" }, { <…>

For spawn_local I have an error:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PanicException(spawn_localcalled from outside of atask::LocalSetBacktrace [{ fn: "std::backtrace::Backtrace::create" }, { fn: "std::backtrace::Backtrace::capture" }, { fn: "flutter_rust_bridge::misc::panic_backtrace::PanicBacktrace::setup::{{closure}}" }, { fn: "std::panicking::rust_panic_with_hook" }, { fn: "std::panicking::begin_panic_handler::{{closure}}" }, { fn: "std::sys_common::backtrace::__rust_end_short_backtrace" }, { fn: "_rust_begin_unwind" }, { fn: "core::panicking::panic_fmt" }, { fn: "tokio::task::local::spawn_local_inner" }, { fn: "tokio::task::local::spawn_local" }, { fn: "rust_lib::api::simple::spawn_local_test" }, { fn: "rust_lib::frb_generated::wire_spawn_local_test_impl::{{closure}}::{{closure}}::{{closure}}" }, { fn: "rust_lib::frb_generated::wire_spawn_local_test_impl::{{closure}}::{{closure}}" }, { fn: "<flutter_rust_bridge::handler::implementation::executor::SimpleExecutor<EL,TP,AR> as flutter_rust_<…>

Logs

-> % RUST_LOG=debug flutter_rust_bridge_codegen generate
[2024-01-31T14:32:49.642Z DEBUG /Users/username/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.0.0-dev.22/src/main.rs:24] cli=Cli { verbose: false, command: Generate(GenerateCommandArgs { watch: false, primary: GenerateCommandArgsPrimary { config_file: None, rust_input: None, dart_output: None, c_output: None, duplicated_c_output: None, rust_root: None, rust_output: None, dart_entrypoint_class_name: None, dart_format_line_length: None, no_dart_enums_style: false, no_add_mod_to_lib: false, llvm_path: None, llvm_compiler_opts: None, dart_root: None, no_build_runner: false, extra_headers: None, no_web: false, no_deps_check: false, default_external_library_loader_web_prefix: None, no_dart3: false, full_dep: false, local: false, dump: None, dump_all: false } }) }
[2024-01-31T14:32:49.644Z DEBUG /Users/username/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.0.0-dev.22/src/binary/commands_parser.rs:8] compute_codegen_config: mode=from_files_auto
[2024-01-31T14:32:49.645Z DEBUG <path>/codegen/config/config_parser.rs:43] Found config file flutter_rust_bridge.yaml
[2024-01-31T14:32:49.647Z DEBUG <path>/codegen/mod.rs:25] config=Config { base_dir: Some(""), rust_input: "rust/src/api/**/*.rs", dart_output: "lib/src/rust", c_output: None, duplicated_c_output: None, rust_root: None, rust_output: None, dart_entrypoint_class_name: None, dart_format_line_length: None, dart_enums_style: None, add_mod_to_lib: None, llvm_path: None, llvm_compiler_opts: None, dart_root: None, build_runner: None, extra_headers: None, web: None, deps_check: None, dart3: None, full_dep: None, local: None, default_external_library_loader_web_prefix: None, dump: None, dump_all: None } meta_config=MetaConfig { watch: false }
[2024-01-31T14:32:49.647Z DEBUG <path>/codegen/config/internal_config_parser.rs:42] InternalConfig.parse base_dir="/Users/username/Work/frb_tokio"
[2024-01-31T14:32:50.057Z DEBUG <path>/codegen/mod.rs:28] internal_config=InternalConfig { controller: ControllerInternalConfig { watch: false, watching_paths: ["/Users/username/Work/frb_tokio/rust/src"], exclude_paths: ["/Users/username/Work/frb_tokio/rust/src/frb_generated.rs", "/Users/username/Work/frb_tokio/rust/src/frb_generated.io.rs", "/Users/username/Work/frb_tokio/rust/src/frb_generated.web.rs"], max_count: None }, preparer: PreparerInternalConfig { dart_root: "/Users/username/Work/frb_tokio", deps_check: true, needs_ffigen: false }, parser: ParserInternalConfig { rust_input_path_pack: RustInputPathPack { rust_input_paths: ["/Users/username/Work/frb_tokio/rust/src/api/simple.rs"] }, rust_crate_dir: "/Users/username/Work/frb_tokio/rust", force_codec_mode_pack: Some(CodecModePack { dart2rust: Pde, rust2dart: Pde }), default_rust_opaque_codec: Moi }, generator: GeneratorInternalConfig { api_dart: GeneratorApiDartInternalConfig { dart_enums_style: true, dart3: true, dart_decl_base_output_path: "/Users/username/Work/frb_tokio/lib/src/rust", dart_entrypoint_class_name: "RustLib" }, wire: GeneratorWireInternalConfig { dart: GeneratorWireDartInternalConfig { has_ffigen: false, web_enabled: true, 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: "", dart_root: "/Users/username/Work/frb_tokio", extra_headers: "", dart_impl_output_path: TargetOrCommonMap { common: "/Users/username/Work/frb_tokio/lib/src/rust/frb_generated.dart", io: "/Users/username/Work/frb_tokio/lib/src/rust/frb_generated.io.dart", web: "/Users/username/Work/frb_tokio/lib/src/rust/frb_generated.web.dart" }, dart_output_class_name_pack: DartOutputClassNamePack { entrypoint_class_name: "RustLib", api_class_name: "RustLibApi", api_impl_class_name: "RustLibApiImpl", api_impl_platform_class_name: "RustLibApiImplPlatform", wire_class_name: "RustLibWire", wasm_module_name: "RustLibWasmModule" }, default_external_library_loader: GeneratorWireDartDefaultExternalLibraryLoaderInternalConfig { stem: "rust_lib", io_directory: "rust/target/release/", web_prefix: "pkg/" }, c_symbol_prefix: "frbgen_frb_tokio_" }, rust: GeneratorWireRustInternalConfig { rust_input_path_pack: RustInputPathPack { rust_input_paths: ["/Users/username/Work/frb_tokio/rust/src/api/simple.rs"] }, rust_crate_dir: "/Users/username/Work/frb_tokio/rust", web_enabled: true, rust_output_path: TargetOrCommonMap { common: "/Users/username/Work/frb_tokio/rust/src/frb_generated.rs", io: "/Users/username/Work/frb_tokio/rust/src/frb_generated.io.rs", web: "/Users/username/Work/frb_tokio/rust/src/frb_generated.web.rs" }, c_symbol_prefix: "frbgen_frb_tokio_", has_ffigen: false, default_stream_sink_codec: Sse, default_rust_opaque_codec: Moi }, c: GeneratorWireCInternalConfig { enable: false, rust_crate_dir: "/Users/username/Work/frb_tokio/rust", rust_output_path: TargetOrCommonMap { common: "/Users/username/Work/frb_tokio/rust/src/frb_generated.rs", io: "/Users/username/Work/frb_tokio/rust/src/frb_generated.io.rs", web: "/Users/username/Work/frb_tokio/rust/src/frb_generated.web.rs" }, c_output_path: None, c_symbol_prefix: "frbgen_frb_tokio_" } } }, polisher: PolisherInternalConfig { duplicated_c_output_path: [], dart_format_line_length: 80, add_mod_to_lib: true, build_runner: true, web_enabled: true, dart_root: "/Users/username/Work/frb_tokio", rust_crate_dir: "/Users/username/Work/frb_tokio/rust", rust_output_path: TargetOrCommonMap { common: "/Users/username/Work/frb_tokio/rust/src/frb_generated.rs", io: "/Users/username/Work/frb_tokio/rust/src/frb_generated.io.rs", web: "/Users/username/Work/frb_tokio/rust/src/frb_generated.web.rs" }, c_output_path: None, enable_auto_upgrade: true }, dumper: DumperInternalConfig { dump_contents: [], dump_directory: "/Users/username/Work/frb_tokio/rust/target/frb_dump" } }
[2024-01-31T14:32:50.060Z DEBUG <path>/utils/dart_repository/dart_repo.rs:22] Guessing toolchain the runner is run into
[2024-01-31T14:32:50.063Z DEBUG <path>/commands/command_runner.rs:109] execute command: bin=sh args="-c \"flutter\" \"--version\"" current_dir=None cmd="sh" "-c" "\"flutter\" \"--version\""
[2024-01-31T14:32:50.537Z DEBUG <path>/commands/command_runner.rs:120] command="sh" "-c" "\"flutter\" \"--version\"" stdout=Flutter 3.16.7 • channel stable • https://github.com/flutter/flutter.git
Framework • revision ef1af02aea (3 weeks ago) • 2024-01-11 15:19:26 -0600
Engine • revision 4a585b7929
Tools • Dart 3.2.4 • DevTools 2.28.5
 stderr=
[2024-01-31T14:32:50.539Z DEBUG <path>/reader.rs:22] read_rust_file rust_file_path="/Users/username/Work/frb_tokio/rust/src/api/simple.rs" module=Some("api::simple")
[2024-01-31T14:32:50.539Z DEBUG <path>/commands/cargo_expand.rs:27] CachedCargoExpand execute manifest_dir= rust_crate_dir="/Users/username/Work/frb_tokio/rust"
[2024-01-31T14:32:50.539Z DEBUG <path>/commands/cargo_expand.rs:90] Running cargo expand in '"/Users/username/Work/frb_tokio/rust"'
[2024-01-31T14:32:50.539Z DEBUG <path>/commands/command_runner.rs:109] execute command: bin=cargo args="expand --lib --theme=none --ugly" current_dir=Some("/Users/username/Work/frb_tokio/rust") cmd=cd "/Users/username/Work/frb_tokio/rust" && "cargo" "expand" "--lib" "--theme=none" "--ugly"
[2024-01-31T14:32:51.644Z DEBUG <path>/commands/command_runner.rs:120] command=cd "/Users/username/Work/frb_tokio/rust" && "cargo" "expand" "--lib" "--theme=none" "--ugly" stdout=#![feature(prelude_import)]
<redacted>
 stderr=    Checking rust_lib v0.1.0 (/Users/username/Work/frb_tokio/rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.63s


[2024-01-31T14:32:51.662Z DEBUG <path>/source_graph/crates.rs:26] parse manifest_path="/Users/username/Work/frb_tokio/rust/Cargo.toml"
[2024-01-31T14:32:51.857Z DEBUG <path>/source_graph/module_parser.rs:31] parse START info=ModuleInfo { visibility: Public, file_path: "/Users/username/Work/frb_tokio/rust/src/lib.rs", module_path: ["crate"] }
[2024-01-31T14:32:51.858Z DEBUG <path>/source_graph/module_parser.rs:150] parse_syn_item_mod module_path=["crate", "api"]
[2024-01-31T14:32:51.858Z DEBUG <path>/source_graph/module_parser.rs:202] parse_syn_item_mod_contentless module_path=["crate", "api"]
[2024-01-31T14:32:51.858Z DEBUG <path>/source_graph/module_parser.rs:205] file_path_candidates "api" "/Users/username/Work/frb_tokio/rust/src/lib.rs" ["/Users/username/Work/frb_tokio/rust/src/api.rs", "/Users/username/Work/frb_tokio/rust/src/api/mod.rs", "/Users/username/Work/frb_tokio/rust/src/lib/api.rs", "/Users/username/Work/frb_tokio/rust/src/lib/api/mod.rs"]
[2024-01-31T14:32:51.858Z DEBUG <path>/reader.rs:22] read_rust_file rust_file_path="/Users/username/Work/frb_tokio/rust/src/api/mod.rs" module=Some("api")
[2024-01-31T14:32:51.858Z DEBUG <path>/commands/cargo_expand.rs:27] CachedCargoExpand execute manifest_dir= rust_crate_dir="/Users/username/Work/frb_tokio/rust"
[2024-01-31T14:32:51.858Z DEBUG <path>/source_graph/module_parser.rs:216] Trying to parse "/Users/username/Work/frb_tokio/rust/src/api/mod.rs"
[2024-01-31T14:32:51.859Z DEBUG <path>/source_graph/module_parser.rs:31] parse START info=ModuleInfo { visibility: Public, file_path: "/Users/username/Work/frb_tokio/rust/src/api/mod.rs", module_path: ["crate", "api"] }
[2024-01-31T14:32:51.859Z DEBUG <path>/source_graph/module_parser.rs:150] parse_syn_item_mod module_path=["crate", "api", "simple"]
[2024-01-31T14:32:51.859Z DEBUG <path>/source_graph/module_parser.rs:180] parse_syn_item_mod_contentful module_path=["crate", "api", "simple"]
[2024-01-31T14:32:51.860Z DEBUG <path>/source_graph/module_parser.rs:31] parse START info=ModuleInfo { visibility: Public, file_path: "/Users/username/Work/frb_tokio/rust/src/api/mod.rs", module_path: ["crate", "api", "simple"] }
[2024-01-31T14:32:51.860Z DEBUG <path>/source_graph/module_parser.rs:77] parse END info=ModuleInfo { visibility: Public, file_path: "/Users/username/Work/frb_tokio/rust/src/api/mod.rs", module_path: ["crate", "api", "simple"] }
[2024-01-31T14:32:51.861Z DEBUG <path>/source_graph/module_parser.rs:77] parse END info=ModuleInfo { visibility: Public, file_path: "/Users/username/Work/frb_tokio/rust/src/api/mod.rs", module_path: ["crate", "api"] }
[2024-01-31T14:32:51.861Z DEBUG <path>/source_graph/module_parser.rs:150] parse_syn_item_mod module_path=["crate", "frb_generated"]
[2024-01-31T14:32:51.862Z DEBUG <path>/source_graph/module_parser.rs:202] parse_syn_item_mod_contentless module_path=["crate", "frb_generated"]
[2024-01-31T14:32:51.862Z DEBUG <path>/source_graph/module_parser.rs:205] file_path_candidates "frb_generated" "/Users/username/Work/frb_tokio/rust/src/lib.rs" ["/Users/username/Work/frb_tokio/rust/src/frb_generated.rs", "/Users/username/Work/frb_tokio/rust/src/frb_generated/mod.rs", "/Users/username/Work/frb_tokio/rust/src/lib/frb_generated.rs", "/Users/username/Work/frb_tokio/rust/src/lib/frb_generated/mod.rs"]
[2024-01-31T14:32:51.863Z DEBUG <path>/reader.rs:22] read_rust_file rust_file_path="/Users/username/Work/frb_tokio/rust/src/frb_generated.rs" module=Some("frb_generated")
[2024-01-31T14:32:51.863Z DEBUG <path>/commands/cargo_expand.rs:27] CachedCargoExpand execute manifest_dir= rust_crate_dir="/Users/username/Work/frb_tokio/rust"
[2024-01-31T14:32:51.864Z DEBUG <path>/source_graph/module_parser.rs:216] Trying to parse "/Users/username/Work/frb_tokio/rust/src/frb_generated.rs"
[2024-01-31T14:32:51.872Z DEBUG <path>/source_graph/module_parser.rs:31] parse START info=ModuleInfo { visibility: Inherited, file_path: "/Users/username/Work/frb_tokio/rust/src/frb_generated.rs", module_path: ["crate", "frb_generated"] }
[2024-01-31T14:32:51.872Z DEBUG <path>/source_graph/module_parser.rs:101] parse_syn_item_struct_or_enum item_ident=Ident { sym: MoiArc, span: bytes(2466..2472) }
[2024-01-31T14:32:51.873Z DEBUG <path>/source_graph/module_parser.rs:124] parse_syn_item_struct_or_enum item_type=ItemType { attrs: [], vis: Visibility::Inherited, type_token: Type, ident: Ident { sym: ObjectId, span: bytes(6292..6300) }, generics: Generics { lt_token: None, params: [], gt_token: None, where_clause: None }, eq_token: Eq, ty: Type::Path { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: usize, span: bytes(6303..6308) }, arguments: PathArguments::None }] } }, semi_token: Semi }
[2024-01-31T14:32:51.873Z DEBUG <path>/source_graph/module_parser.rs:124] parse_syn_item_struct_or_enum item_type=ItemType { attrs: [], vis: Visibility::Public(Pub), type_token: Type, ident: Ident { sym: MoiArcPool, span: bytes(6323..6333) }, generics: Generics { lt_token: Some(Lt), params: [GenericParam::Type(TypeParam { attrs: [], ident: Ident { sym: T, span: bytes(6334..6335) }, colon_token: None, bounds: [], eq_token: None, default: None })], gt_token: Some(Gt), where_clause: None }, eq_token: Eq, ty: Type::Path { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: std, span: bytes(6339..6342) }, arguments: PathArguments::None }, PathSep, PathSegment { ident: Ident { sym: sync, span: bytes(6344..6348) }, arguments: PathArguments::None }, PathSep, PathSegment { ident: Ident { sym: RwLock, span: bytes(6350..6356) }, arguments: PathArguments::AngleBracketed { colon2_token: None, lt_token: Lt, args: [GenericArgument::Type(Type::Path { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: MoiArcPoolInner, span: bytes(6357..6372) }, arguments: PathArguments::AngleBracketed { colon2_token: None, lt_token: Lt, args: [GenericArgument::Type(Type::Path { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: T, span: bytes(6373..6374) }, arguments: PathArguments::None }] } })], gt_token: Gt } }] } })], gt_token: Gt } }] } }, semi_token: Semi }
[2024-01-31T14:32:51.873Z DEBUG <path>/source_graph/module_parser.rs:101] parse_syn_item_struct_or_enum item_ident=Ident { sym: MoiArcPoolInner, span: bytes(6393..6408) }
[2024-01-31T14:32:51.873Z DEBUG <path>/source_graph/module_parser.rs:101] parse_syn_item_struct_or_enum item_ident=Ident { sym: IdGenerator, span: bytes(6701..6712) }
[2024-01-31T14:32:51.873Z DEBUG <path>/source_graph/module_parser.rs:101] parse_syn_item_struct_or_enum item_ident=Ident { sym: MoiArcPoolValue, span: bytes(7285..7300) }
[2024-01-31T14:32:51.873Z DEBUG <path>/source_graph/module_parser.rs:124] parse_syn_item_struct_or_enum item_type=ItemType { attrs: [Attribute { pound_token: Pound, style: AttrStyle::Outer, bracket_token: Bracket, meta: Meta::NameValue { path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: doc, span: bytes(7709..7750) }, arguments: PathArguments::None }] }, eq_token: Eq, value: Expr::Lit { attrs: [], lit: Lit::Str { token: " Please refer to `RustOpaque` for doc." } } } }], vis: Visibility::Public(Pub), type_token: Type, ident: Ident { sym: RustOpaqueMoi, span: bytes(7764..7777) }, generics: Generics { lt_token: Some(Lt), params: [GenericParam::Type(TypeParam { attrs: [], ident: Ident { sym: T, span: bytes(7778..7779) }, colon_token: None, bounds: [], eq_token: None, default: None })], gt_token: Some(Gt), where_clause: None }, eq_token: Eq, ty: Type::Path { qself: None, path: Path { leading_colon: Some(PathSep), segments: [PathSegment { ident: Ident { sym: flutter_rust_bridge, span: bytes(7793..7812) }, arguments: PathArguments::None }, PathSep, PathSegment { ident: Ident { sym: for_generated, span: bytes(7814..7827) }, arguments: PathArguments::None }, PathSep, PathSegment { ident: Ident { sym: RustOpaqueBase, span: bytes(7829..7843) }, arguments: PathArguments::AngleBracketed { colon2_token: None, lt_token: Lt, args: [GenericArgument::Type(Type::Path { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: T, span: bytes(7844..7845) }, arguments: PathArguments::None }] } }), Comma, GenericArgument::Type(Type::Path { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: MoiArc, span: bytes(7847..7853) }, arguments: PathArguments::AngleBracketed { colon2_token: None, lt_token: Lt, args: [GenericArgument::Type(Type::Path { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: T, span: bytes(7854..7855) }, arguments: PathArguments::None }] } })], gt_token: Gt } }] } })], gt_token: Gt } }] } }, semi_token: Semi }
[2024-01-31T14:32:51.873Z DEBUG <path>/source_graph/module_parser.rs:124] parse_syn_item_struct_or_enum item_type=ItemType { attrs: [Attribute { pound_token: Pound, style: AttrStyle::Outer, bracket_token: Bracket, meta: Meta::NameValue { path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: doc, span: bytes(7863..7975) }, arguments: PathArguments::None }] }, eq_token: Eq, value: Expr::Lit { attrs: [], lit: Lit::Str { token: " A wrapper to support [arbitrary Rust types](https://cjycode.com/flutter_rust_bridge/guides/types/arbitrary)." } } } }], vis: Visibility::Public(Pub), type_token: Type, ident: Ident { sym: RustOpaque, span: bytes(7989..7999) }, generics: Generics { lt_token: Some(Lt), params: [GenericParam::Type(TypeParam { attrs: [], ident: Ident { sym: T, span: bytes(8000..8001) }, colon_token: None, bounds: [], eq_token: None, default: None })], gt_token: Some(Gt), where_clause: None }, eq_token: Eq, ty: Type::Path { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: RustOpaqueMoi, span: bytes(8005..8018) }, arguments: PathArguments::AngleBracketed { colon2_token: None, lt_token: Lt, args: [GenericArgument::Type(Type::Path { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: T, span: bytes(8019..8020) }, arguments: PathArguments::None }] } })], gt_token: Gt } }] } }, semi_token: Semi }
[2024-01-31T14:32:51.874Z DEBUG <path>/source_graph/module_parser.rs:124] parse_syn_item_struct_or_enum item_type=ItemType { attrs: [Attribute { pound_token: Pound, style: AttrStyle::Outer, bracket_token: Bracket, meta: Meta::NameValue { path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: doc, span: bytes(8077..8122) }, arguments: PathArguments::None }] }, eq_token: Eq, value: Expr::Lit { attrs: [], lit: Lit::Str { token: " Please refer to `RustAutoOpaque` for doc." } } } }], vis: Visibility::Public(Pub), type_token: Type, ident: Ident { sym: RustAutoOpaqueMoi, span: bytes(8136..8153) }, generics: Generics { lt_token: Some(Lt), params: [GenericParam::Type(TypeParam { attrs: [], ident: Ident { sym: T, span: bytes(8154..8155) }, colon_token: None, bounds: [], eq_token: None, default: None })], gt_token: Some(Gt), where_clause: None }, eq_token: Eq, ty: Type::Path { qself: None, path: Path { leading_colon: Some(PathSep), segments: [PathSegment { ident: Ident { sym: flutter_rust_bridge, span: bytes(8169..8188) }, arguments: PathArguments::None }, PathSep, PathSegment { ident: Ident { sym: for_generated, span: bytes(8190..8203) }, arguments: PathArguments::None }, PathSep, PathSegment { ident: Ident { sym: RustAutoOpaqueBase, span: bytes(8205..8223) }, arguments: PathArguments::AngleBracketed { colon2_token: None, lt_token: Lt, args: [GenericArgument::Type(Type::Path { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: T, span: bytes(8224..8225) }, arguments: PathArguments::None }] } }), Comma, GenericArgument::Type(Type::Path { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: MoiArc, span: bytes(8235..8241) }, arguments: PathArguments::AngleBracketed { colon2_token: None, lt_token: Lt, args: [GenericArgument::Type(Type::Path { qself: None, path: Path { leading_colon: Some(PathSep), segments: [PathSegment { ident: Ident { sym: flutter_rust_bridge, span: bytes(8244..8263) }, arguments: PathArguments::None }, PathSep, PathSegment { ident: Ident { sym: for_generated, span: bytes(8265..8278) }, arguments: PathArguments::None }, PathSep, PathSegment { ident: Ident { sym: rust_async, span: bytes(8280..8290) }, arguments: PathArguments::None }, PathSep, PathSegment { ident: Ident { sym: RwLock, span: bytes(8292..8298) }, arguments: PathArguments::AngleBracketed { colon2_token: None, lt_token: Lt, args: [GenericArgument::Type(Type::Path { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: T, span: bytes(8299..8300) }, arguments: PathArguments::None }] } })], gt_token: Gt } }] } })], gt_token: Gt } }] } })], gt_token: Gt } }] } }, semi_token: Semi }
[2024-01-31T14:32:51.874Z DEBUG <path>/source_graph/module_parser.rs:124] parse_syn_item_struct_or_enum item_type=ItemType { attrs: [Attribute { pound_token: Pound, style: AttrStyle::Outer, bracket_token: Bracket, meta: Meta::NameValue { path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: doc, span: bytes(8309..8375) }, arguments: PathArguments::None }] }, eq_token: Eq, value: Expr::Lit { attrs: [], lit: Lit::Str { token: " Usually this is unneeded, and just write down arbitrary types." } } } }, Attribute { pound_token: Pound, style: AttrStyle::Outer, bracket_token: Bracket, meta: Meta::NameValue { path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: doc, span: bytes(8380..8460) }, arguments: PathArguments::None }] }, eq_token: Eq, value: Expr::Lit { attrs: [], lit: Lit::Str { token: " However, when you need arbitrary types at places that are not supported yet," } } } }, Attribute { pound_token: Pound, style: AttrStyle::Outer, bracket_token: Bracket, meta: Meta::NameValue { path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: doc, span: bytes(8465..8511) }, arguments: PathArguments::None }] }, eq_token: Eq, value: Expr::Lit { attrs: [], lit: Lit::Str { token: " use `RustOpaqueOpaque<YourArbitraryType>`." } } } }], vis: Visibility::Public(Pub), type_token: Type, ident: Ident { sym: RustAutoOpaque, span: bytes(8525..8539) }, generics: Generics { lt_token: Some(Lt), params: [GenericParam::Type(TypeParam { attrs: [], ident: Ident { sym: T, span: bytes(8540..8541) }, colon_token: None, bounds: [], eq_token: None, default: None })], gt_token: Some(Gt), where_clause: None }, eq_token: Eq, ty: Type::Path { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: RustAutoOpaqueMoi, span: bytes(8545..8562) }, arguments: PathArguments::AngleBracketed { colon2_token: None, lt_token: Lt, args: [GenericArgument::Type(Type::Path { qself: None, path: Path { leading_colon: None, segments: [PathSegment { ident: Ident { sym: T, span: bytes(8563..8564) }, arguments: PathArguments::None }] } })], gt_token: Gt } }] } }, semi_token: Semi }
[2024-01-31T14:32:51.875Z DEBUG <path>/source_graph/module_parser.rs:101] parse_syn_item_struct_or_enum item_ident=Ident { sym: StreamSink, span: bytes(10220..10230) }
[2024-01-31T14:32:51.876Z DEBUG <path>/source_graph/module_parser.rs:101] parse_syn_item_struct_or_enum item_ident=Ident { sym: FLUTTER_RUST_BRIDGE_HANDLER, span: bytes(12281..12308) }
[2024-01-31T14:32:51.876Z DEBUG <path>/source_graph/module_parser.rs:150] parse_syn_item_mod module_path=["crate", "frb_generated", "io"]
[2024-01-31T14:32:51.876Z DEBUG <path>/source_graph/module_parser.rs:180] parse_syn_item_mod_contentful module_path=["crate", "frb_generated", "io"]
[2024-01-31T14:32:51.877Z DEBUG <path>/source_graph/module_parser.rs:31] parse START info=ModuleInfo { visibility: Inherited, file_path: "/Users/username/Work/frb_tokio/rust/src/frb_generated.rs", module_path: ["crate", "frb_generated", "io"] }
[2024-01-31T14:32:51.877Z DEBUG <path>/source_graph/module_parser.rs:77] parse END info=ModuleInfo { visibility: Inherited, file_path: "/Users/username/Work/frb_tokio/rust/src/frb_generated.rs", module_path: ["crate", "frb_generated", "io"] }
[2024-01-31T14:32:51.879Z DEBUG <path>/source_graph/module_parser.rs:77] parse END info=ModuleInfo { visibility: Inherited, file_path: "/Users/username/Work/frb_tokio/rust/src/frb_generated.rs", module_path: ["crate", "frb_generated"] }
[2024-01-31T14:32:51.880Z DEBUG <path>/source_graph/module_parser.rs:77] parse END info=ModuleInfo { visibility: Public, file_path: "/Users/username/Work/frb_tokio/rust/src/lib.rs", module_path: ["crate"] }
[2024-01-31T14:32:51.882Z DEBUG <path>/function_parser/mod.rs:67] parse_function function name: Ident { sym: greet, span: bytes(176..181) }
[2024-01-31T14:32:51.884Z DEBUG <path>/function_parser/mod.rs:67] parse_function function name: Ident { sym: spawn_test, span: bytes(384..394) }
[2024-01-31T14:32:51.885Z DEBUG <path>/function_parser/mod.rs:67] parse_function function name: Ident { sym: spawn_local_test, span: bytes(433..449) }
[2024-01-31T14:32:51.885Z DEBUG <path>/function_parser/mod.rs:67] parse_function function name: Ident { sym: spawn_blocking_with_test, span: bytes(494..518) }
[2024-01-31T14:32:51.885Z DEBUG <path>/function_parser/mod.rs:67] parse_function function name: Ident { sym: init_app, span: bytes(668..676) }
[2024-01-31T14:32:51.892Z DEBUG <path>/commands/format_dart.rs:13] execute format_dart paths=["lib/src/rust/api/simple.dart", "lib/src/rust/frb_generated.dart", "lib/src/rust/frb_generated.io.dart", "lib/src/rust/frb_generated.web.dart"] line_length=80
[2024-01-31T14:32:51.892Z DEBUG <path>/commands/command_runner.rs:109] execute command: bin=sh args="-c \"dart\" \"format\" \"--line-length\" \"80\" \"lib/src/rust/api/simple.dart\" \"lib/src/rust/frb_generated.dart\" \"lib/src/rust/frb_generated.io.dart\" \"lib/src/rust/frb_generated.web.dart\"" current_dir=Some("/Users/username/Work/frb_tokio") cmd=cd "/Users/username/Work/frb_tokio" && "sh" "-c" "\"dart\" \"format\" \"--line-length\" \"80\" \"lib/src/rust/api/simple.dart\" \"lib/src/rust/frb_generated.dart\" \"lib/src/rust/frb_generated.io.dart\" \"lib/src/rust/frb_generated.web.dart\""
[2024-01-31T14:32:52.851Z DEBUG <path>/commands/command_runner.rs:120] command=cd "/Users/username/Work/frb_tokio" && "sh" "-c" "\"dart\" \"format\" \"--line-length\" \"80\" \"lib/src/rust/api/simple.dart\" \"lib/src/rust/frb_generated.dart\" \"lib/src/rust/frb_generated.io.dart\" \"lib/src/rust/frb_generated.web.dart\"" stdout=Formatted lib/src/rust/api/simple.dart
Formatted lib/src/rust/frb_generated.dart
Formatted lib/src/rust/frb_generated.io.dart
Formatted lib/src/rust/frb_generated.web.dart
Formatted 4 files (4 changed) in 0.57 seconds.
 stderr=
[2024-01-31T14:32:52.851Z DEBUG <path>/commands/format_rust.rs:9] execute format_rust paths=["src/frb_generated.rs", "src/frb_generated.io.rs", "src/frb_generated.web.rs"]
[2024-01-31T14:32:52.852Z DEBUG <path>/commands/command_runner.rs:109] execute command: bin=sh args="-c \"rustfmt\" \"--edition\" \"2018\" \"src/frb_generated.rs\" \"src/frb_generated.io.rs\" \"src/frb_generated.web.rs\"" current_dir=Some("/Users/username/Work/frb_tokio/rust") cmd=cd "/Users/username/Work/frb_tokio/rust" && "sh" "-c" "\"rustfmt\" \"--edition\" \"2018\" \"src/frb_generated.rs\" \"src/frb_generated.io.rs\" \"src/frb_generated.web.rs\""
[2024-01-31T14:32:52.995Z DEBUG <path>/commands/command_runner.rs:120] command=cd "/Users/username/Work/frb_tokio/rust" && "sh" "-c" "\"rustfmt\" \"--edition\" \"2018\" \"src/frb_generated.rs\" \"src/frb_generated.io.rs\" \"src/frb_generated.web.rs\"" stdout= stderr=
[2024-01-31T14:32:52.996Z DEBUG <path>/utils/dart_repository/dart_repo.rs:22] Guessing toolchain the runner is run into
[2024-01-31T14:32:52.996Z DEBUG <path>/utils/dart_repository/dart_repo.rs:71] Checking presence of flutter_rust_bridge in dependencies at /Users/username/Work/frb_tokio
[2024-01-31T14:32:52.997Z DEBUG <path>/utils/dart_repository/dart_repo.rs:103] Checking presence of flutter_rust_bridge in dependencies at /Users/username/Work/frb_tokio
[1.3s] Parse  
  └── [1.1s] Run cargo expand  
  └── [0.2s] Parse source graph  
[0.0s] Generate  
[1.1s] Polish  
  └── [1.0s] Run Dart formatter  
  └── [0.1s] Run Rust formatter  
  └── [0.0s] Auto upgrade                                                                                                                                                                                                          Done!

Expected behavior

No response

Generated binding code

No response

OS

macOS

Version of flutter_rust_bridge_codegen

2.0.0-dev.22

Flutter info

-> % flutter doctor -v
[✓] Flutter (Channel stable, 3.16.7, on macOS 14.2.1 23C71 darwin-x64, locale ru-RU)
    • Flutter version 3.16.7 on channel stable at /Users/username/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ef1af02aea (3 weeks ago), 2024-01-11 15:19:26 -0600
    • Engine revision 4a585b7929
    • Dart version 3.2.4
    • DevTools version 2.28.5

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/username/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/username/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.15.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] VS Code (version 1.85.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.80.0

[✓] Connected device (4 available)
    • ios            • iOS 17.2.1 21C66
    • iPhone 15 Pro (mobile)      • F77128CE-40B3-4736-9C98-72982282AE51 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-0 (simulator)
    • macOS (desktop)             • macos                                • darwin-x64     • macOS 14.2.1 23C71 darwin-x64
    • Chrome (web)                • chrome                               • web-javascript • Google Chrome 119.0.6045.105

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Version of clang++

-> % clang++ --version Apple clang version 15.0.0 (clang-1500.1.0.2.5) Target: x86_64-apple-darwin23.2.0 Thread model: posix

Additional context

No response

Hi, you need to be in a async fn to call spawn functions.

pub async fn spawn_test() {
    spawn(async {});
}

etc

I will update doc later to explicitly mention this.

Hi! Thanks for the response. Adding async works for all except spawn_local. Adding async to function which calls spawn_local has no effect and still returns an error. I'm new to Rust and think that I missed some silly thing.

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PanicException(`spawn_local` called from outside of a `task::LocalSet`Backtrace [{ fn: "std::backtrace::Backtrace::create" }, { fn: "std::backtrace::Backtrace::capture" }, { fn: "flutter_rust_bridge::misc::panic_backtrace::PanicBacktrace::setup::{{closure}}" }, { fn: "std::panicking::rust_panic_with_hook" }, { fn: "std::panicking::begin_panic_handler::{{closure}}" }, { fn: "std::sys_common::backtrace::__rust_end_short_backtrace" }, { fn: "_rust_begin_unwind" }, { fn: "core::panicking::panic_fmt" }, { fn: "tokio::task::local::spawn_local_inner" }, { fn: "tokio::task::local::spawn_local" }, { fn: "rust_lib::api::simple::spawn_local_test::{{closure}}" }, { fn: "rust_lib::frb_generated::wire_spawn_local_test_impl::{{closure}}::{{closure}}::{{closure}}::{{closure}}::{{closure}}" }, { fn: "rust_lib::frb_generated::wire_spawn_local_test_impl::{{closure}}::{{closure}}::{{closure}}" }, { fn: "<flutter_rust_bridge::handler::implementation:<…>

Looking at https://docs.rs/tokio/latest/tokio/task/fn.spawn_local.html's "example" section, it seems that Tokio (the runtime flutter_rust_bridge uses by default - but you can customize to use other runtimes) requires some extra code.

Copy of the example in that doc:

use std::rc::Rc;
use tokio::task;

#[tokio::main]
async fn main() {
    let nonsend_data = Rc::new("my nonsend data...");

    let local = task::LocalSet::new();

    // Run the local task set.
    local.run_until(async move {
        let nonsend_data = nonsend_data.clone();
        task::spawn_local(async move {
            println!("{}", nonsend_data);
            // ...
        }).await.unwrap();
    }).await;
}