diff --git a/flutter_ffi_plugin/bin/src/message.dart b/flutter_ffi_plugin/bin/src/message.dart index 1b98a24b..79ddeaa0 100644 --- a/flutter_ffi_plugin/bin/src/message.dart +++ b/flutter_ffi_plugin/bin/src/message.dart @@ -258,12 +258,16 @@ import 'package:rinf/rinf.dart'; rustPath, ''' #![allow(unused_imports)] +#![allow(dead_code)] use crate::tokio; use prost::Message; use rinf::{send_rust_signal, DartSignal}; +use std::error::Error; use std::sync::Mutex; use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}; + +type Result = std::result::Result>; ''', atFront: true, ); @@ -287,9 +291,13 @@ pub static ${snakeName.toUpperCase()}_CHANNEL: ${messageName}Cell = Mutex::new(None); impl ${normalizePascal(messageName)} { - pub fn get_dart_signal_receiver() -> UnboundedReceiver> { - let mut guard = ${snakeName.toUpperCase()}_CHANNEL.lock() - .expect("Could not access the channel lock."); + pub fn get_dart_signal_receiver() + -> Result>> + { + let mut guard = + ${snakeName.toUpperCase()}_CHANNEL.lock().map_err(|_| { + String::from("Could not acquire the channel lock.") + })?; if guard.is_none() { let (sender, receiver) = unbounded_channel(); guard.replace((sender, Some(receiver))); @@ -301,7 +309,7 @@ impl ${normalizePascal(messageName)} { // which is now closed. let pair = guard .as_ref() - .expect("Message channel in Rust not present."); + .ok_or("Message channel in Rust not present.")?; if pair.0.is_closed() { let (sender, receiver) = unbounded_channel(); guard.replace((sender, Some(receiver))); @@ -309,9 +317,12 @@ impl ${normalizePascal(messageName)} { } let pair = guard .take() - .expect("Message channel in Rust not present."); + .ok_or("Message channel in Rust not present.")?; guard.replace((pair.0, None)); - pair.1.expect("Each Dart signal receiver can be taken only once") + let receiver = pair + .1 + .ok_or("Each Dart signal receiver can be taken only once")?; + Ok(receiver) } } ''', diff --git a/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs b/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs index cb683073..1177f83b 100755 --- a/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs +++ b/flutter_ffi_plugin/example/native/hub/src/sample_functions.rs @@ -18,11 +18,11 @@ const IS_DEBUG_MODE: bool = false; static VECTOR: Mutex> = Mutex::const_new(Vec::new()); // Business logic for the counter widget. -pub async fn tell_numbers() { +pub async fn tell_numbers() -> Result<()> { use messages::counter_number::*; // Stream getter is generated from a marked Protobuf message. - let mut receiver = SampleNumberInput::get_dart_signal_receiver(); + let mut receiver = SampleNumberInput::get_dart_signal_receiver()?; while let Some(dart_signal) = receiver.recv().await { // Extract values from the message received from Dart. // This message is a type that's declared in its Protobuf file. @@ -44,6 +44,8 @@ pub async fn tell_numbers() { } .send_signal_to_dart(); } + + Ok(()) } // Business logic for the fractal image. diff --git a/flutter_ffi_plugin/template/native/hub/src/common.rs b/flutter_ffi_plugin/template/native/hub/src/common.rs new file mode 100644 index 00000000..c7a6d7ba --- /dev/null +++ b/flutter_ffi_plugin/template/native/hub/src/common.rs @@ -0,0 +1,3 @@ +use std::error::Error; + +pub type Result = std::result::Result>; diff --git a/flutter_ffi_plugin/template/native/hub/src/lib.rs b/flutter_ffi_plugin/template/native/hub/src/lib.rs index 9c2519f4..80ec36e1 100644 --- a/flutter_ffi_plugin/template/native/hub/src/lib.rs +++ b/flutter_ffi_plugin/template/native/hub/src/lib.rs @@ -1,10 +1,11 @@ //! This `hub` crate is the //! entry point of the Rust logic. +mod common; mod messages; use tokio; // Comment this line to target the web. -// use tokio_with_wasm::alias as tokio; // Uncomment this line to target the web. + // use tokio_with_wasm::alias as tokio; // Uncomment this line to target the web. rinf::write_interface!(); @@ -15,10 +16,14 @@ rinf::write_interface!(); // use `tokio::task::spawn_blocking`. async fn main() { use messages::basic::*; + tokio::spawn(communicate()); +} + +async fn communicate() -> Result<()> { // Send signals to Dart like below. SmallNumber { number: 7 }.send_signal_to_dart(); // Get receivers that listen to Dart signals like below. - let mut receiver = SmallText::get_dart_signal_receiver(); + let mut receiver = SmallText::get_dart_signal_receiver()?; while let Some(dart_signal) = receiver.recv().await { let message: SmallText = dart_signal.message; rinf::debug_print!("{message:?}");