error handling fixes

This commit is contained in:
Robin Müller 2022-05-16 11:24:59 +02:00
parent 061fcf2d97
commit 529d2ff67d
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
3 changed files with 47 additions and 67 deletions

View File

@ -1,2 +1,3 @@
pub mod events;
pub mod executable;
pub mod objects;

27
src/core/executable.rs Normal file
View File

@ -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<Duration>;
const TASK_NAME: &'static str;
fn periodic_op(&mut self, op_code: u32) -> Result<OpResult, Self::Error>;
}
trait ExecutableWithAssociatedFuncs {
type Error;
fn exec_type(&self) -> ExecutionType;
fn task_freq(&self) -> Option<Duration>;
fn task_name(&self) -> &'static str;
fn periodic_op(&mut self) -> Result<OpResult, Self::Error>;
}

View File

@ -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<Duration>;
const TASK_NAME: &'static str;
fn periodic_op(&mut self) -> Result<OpResult, Self::Error>;
}
trait Executable {
type Error;
fn exec_type(&self) -> ExecutionType;
fn task_freq(&self) -> Option<Duration>;
fn task_name(&self) -> &'static str;
fn periodic_op(&mut self) -> Result<OpResult, Self::Error>;
}
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<Duration> = Some(Duration::from_millis(500));
const TASK_NAME: &'static str = "Test Task";
fn periodic_op(&mut self) -> Result<OpResult, ExampleError> {
println!("Periodic Operation!");
Ok(OpResult::Ok)
fn periodic_op(&mut self, op_code: u32) -> Result<OpResult, ExampleError> {
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<Error = E> + Send + 'static,
E: Error + Send + 'static,
>(
fn test_thread<T: Executable<Error = E> + Send + 'static, E: Error + Send + 'static>(
mut executable: T,
op_code: u32,
) -> JoinHandle<Result<OpResult, E>> {
// 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)
});