From 529d2ff67dcdbedc02a868d190f887b5e07fce9f Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 16 May 2022 11:24:59 +0200 Subject: [PATCH] error handling fixes --- src/core.rs | 1 + src/core/executable.rs | 27 +++++++++++++ src/main.rs | 86 ++++++++++-------------------------------- 3 files changed, 47 insertions(+), 67 deletions(-) create mode 100644 src/core/executable.rs diff --git a/src/core.rs b/src/core.rs index 61031db..6ee7941 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,2 +1,3 @@ pub mod events; +pub mod executable; pub mod objects; diff --git a/src/core/executable.rs b/src/core/executable.rs new file mode 100644 index 0000000..527e5ee --- /dev/null +++ b/src/core/executable.rs @@ -0,0 +1,27 @@ +use std::time::Duration; + +pub enum OpResult { + Ok, +} + +pub enum ExecutionType { + Infinite, + Cycles(u32), + OneShot, +} + +pub trait Executable { + type Error; + const EXEC_TYPE: ExecutionType; + const TASK_FREQ: Option; + const TASK_NAME: &'static str; + fn periodic_op(&mut self, op_code: u32) -> Result; +} + +trait ExecutableWithAssociatedFuncs { + type Error; + fn exec_type(&self) -> ExecutionType; + fn task_freq(&self) -> Option; + fn task_name(&self) -> &'static str; + fn periodic_op(&mut self) -> Result; +} diff --git a/src/main.rs b/src/main.rs index 0f30f53..d2bb3d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,35 +1,10 @@ +use launchpad::core::executable::{Executable, ExecutionType, OpResult}; use std::error::Error; use std::fmt; use std::thread; use std::thread::JoinHandle; use std::time::Duration; -enum OpResult { - Ok, -} - -enum ExecutionType { - Infinite, - Cycles(u32), - OneShot, -} - -trait ExecutableWithAssocConst { - type Error; - const EXEC_TYPE: ExecutionType; - const TASK_FREQ: Option; - const TASK_NAME: &'static str; - fn periodic_op(&mut self) -> Result; -} - -trait Executable { - type Error; - fn exec_type(&self) -> ExecutionType; - fn task_freq(&self) -> Option; - fn task_name(&self) -> &'static str; - fn periodic_op(&mut self) -> Result; -} - struct ExampleTask {} #[derive(Debug)] @@ -57,48 +32,28 @@ impl Error for ExampleError { } } -impl ExecutableWithAssocConst for ExampleTask { +impl Executable for ExampleTask { type Error = ExampleError; const EXEC_TYPE: ExecutionType = ExecutionType::OneShot; const TASK_FREQ: Option = Some(Duration::from_millis(500)); const TASK_NAME: &'static str = "Test Task"; - fn periodic_op(&mut self) -> Result { - println!("Periodic Operation!"); - Ok(OpResult::Ok) + fn periodic_op(&mut self, op_code: u32) -> Result { + if op_code == 0 { + println!("Periodic Operation OK!"); + Ok(OpResult::Ok) + } else { + println!("Periodic Operation Fail!"); + Err(ExampleError::new("Example Task Failure")) + } } } fn main() { - let mut exec_task = ExampleTask {}; - let jhandle = thread::spawn(move || match ExampleTask::EXEC_TYPE { - ExecutionType::OneShot => exec_task.periodic_op(), - ExecutionType::Infinite => loop { - exec_task.periodic_op().expect("Periodic Op failed"); - let freq = ExampleTask::TASK_FREQ.unwrap_or_else(|| { - panic!( - "No task frequency specified for task {}", - ExampleTask::TASK_NAME - ) - }); - thread::sleep(freq) - }, - ExecutionType::Cycles(cycles) => { - for _i in 0..cycles - 1 { - exec_task.periodic_op().unwrap(); - let freq = ExampleTask::TASK_FREQ.unwrap_or_else(|| { - panic!( - "No task frequency specified for task {}", - ExampleTask::TASK_NAME - ) - }); - thread::sleep(freq) - } - Ok(OpResult::Ok) - } - }); - let mut exec_task2 = ExampleTask {}; - let jhandle2 = test_thread(exec_task2); + let exec_task = ExampleTask {}; + let jhandle = test_thread(exec_task, 0); + let exec_task2 = ExampleTask {}; + let jhandle2 = test_thread(exec_task2, 1); jhandle .join() .expect("Joining thread failed") @@ -109,24 +64,21 @@ fn main() { .expect("Task 2 failed"); } -fn test_thread< - T: ExecutableWithAssocConst + Send + 'static, - E: Error + Send + 'static, ->( +fn test_thread + Send + 'static, E: Error + Send + 'static>( mut executable: T, + op_code: u32, ) -> JoinHandle> { - // let executable = Arc::new(executable_unprotected); thread::spawn(move || match T::EXEC_TYPE { - ExecutionType::OneShot => executable.periodic_op(), + ExecutionType::OneShot => executable.periodic_op(op_code), ExecutionType::Infinite => loop { - executable.periodic_op().expect("Periodic Op failed"); + executable.periodic_op(op_code)?; let freq = T::TASK_FREQ .unwrap_or_else(|| panic!("No task frequency specified for task {}", T::TASK_NAME)); thread::sleep(freq) }, ExecutionType::Cycles(cycles) => { for _i in 0..cycles - 1 { - executable.periodic_op().unwrap(); + executable.periodic_op(op_code)?; let freq = T::TASK_FREQ.unwrap_or_else(|| { panic!("No task frequency specified for task {}", T::TASK_NAME) });