Skip to content

Commit

Permalink
Completely remove expect as well as unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
temeddix committed Jun 17, 2024
1 parent 092ee1e commit 06a556f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
23 changes: 17 additions & 6 deletions flutter_ffi_plugin/bin/src/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
''',
atFront: true,
);
Expand All @@ -287,9 +291,13 @@ pub static ${snakeName.toUpperCase()}_CHANNEL: ${messageName}Cell =
Mutex::new(None);
impl ${normalizePascal(messageName)} {
pub fn get_dart_signal_receiver() -> UnboundedReceiver<DartSignal<Self>> {
let mut guard = ${snakeName.toUpperCase()}_CHANNEL.lock()
.expect("Could not access the channel lock.");
pub fn get_dart_signal_receiver()
-> Result<UnboundedReceiver<DartSignal<Self>>>
{
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)));
Expand All @@ -301,17 +309,20 @@ 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)));
}
}
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)
}
}
''',
Expand Down
6 changes: 4 additions & 2 deletions flutter_ffi_plugin/example/native/hub/src/sample_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const IS_DEBUG_MODE: bool = false;
static VECTOR: Mutex<Vec<bool>> = 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.
Expand All @@ -44,6 +44,8 @@ pub async fn tell_numbers() {
}
.send_signal_to_dart();
}

Ok(())
}

// Business logic for the fractal image.
Expand Down
3 changes: 3 additions & 0 deletions flutter_ffi_plugin/template/native/hub/src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use std::error::Error;

pub type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
9 changes: 7 additions & 2 deletions flutter_ffi_plugin/template/native/hub/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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!();

Expand All @@ -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:?}");
Expand Down

0 comments on commit 06a556f

Please sign in to comment.