error handling fixes
This commit is contained in:
parent
061fcf2d97
commit
529d2ff67d
@ -1,2 +1,3 @@
|
|||||||
pub mod events;
|
pub mod events;
|
||||||
|
pub mod executable;
|
||||||
pub mod objects;
|
pub mod objects;
|
||||||
|
27
src/core/executable.rs
Normal file
27
src/core/executable.rs
Normal 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>;
|
||||||
|
}
|
84
src/main.rs
84
src/main.rs
@ -1,35 +1,10 @@
|
|||||||
|
use launchpad::core::executable::{Executable, ExecutionType, OpResult};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::thread::JoinHandle;
|
use std::thread::JoinHandle;
|
||||||
use std::time::Duration;
|
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 {}
|
struct ExampleTask {}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -57,48 +32,28 @@ impl Error for ExampleError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ExecutableWithAssocConst for ExampleTask {
|
impl Executable for ExampleTask {
|
||||||
type Error = ExampleError;
|
type Error = ExampleError;
|
||||||
const EXEC_TYPE: ExecutionType = ExecutionType::OneShot;
|
const EXEC_TYPE: ExecutionType = ExecutionType::OneShot;
|
||||||
const TASK_FREQ: Option<Duration> = Some(Duration::from_millis(500));
|
const TASK_FREQ: Option<Duration> = Some(Duration::from_millis(500));
|
||||||
const TASK_NAME: &'static str = "Test Task";
|
const TASK_NAME: &'static str = "Test Task";
|
||||||
|
|
||||||
fn periodic_op(&mut self) -> Result<OpResult, ExampleError> {
|
fn periodic_op(&mut self, op_code: u32) -> Result<OpResult, ExampleError> {
|
||||||
println!("Periodic Operation!");
|
if op_code == 0 {
|
||||||
|
println!("Periodic Operation OK!");
|
||||||
Ok(OpResult::Ok)
|
Ok(OpResult::Ok)
|
||||||
|
} else {
|
||||||
|
println!("Periodic Operation Fail!");
|
||||||
|
Err(ExampleError::new("Example Task Failure"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut exec_task = ExampleTask {};
|
let exec_task = ExampleTask {};
|
||||||
let jhandle = thread::spawn(move || match ExampleTask::EXEC_TYPE {
|
let jhandle = test_thread(exec_task, 0);
|
||||||
ExecutionType::OneShot => exec_task.periodic_op(),
|
let exec_task2 = ExampleTask {};
|
||||||
ExecutionType::Infinite => loop {
|
let jhandle2 = test_thread(exec_task2, 1);
|
||||||
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);
|
|
||||||
jhandle
|
jhandle
|
||||||
.join()
|
.join()
|
||||||
.expect("Joining thread failed")
|
.expect("Joining thread failed")
|
||||||
@ -109,24 +64,21 @@ fn main() {
|
|||||||
.expect("Task 2 failed");
|
.expect("Task 2 failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_thread<
|
fn test_thread<T: Executable<Error = E> + Send + 'static, E: Error + Send + 'static>(
|
||||||
T: ExecutableWithAssocConst<Error = E> + Send + 'static,
|
|
||||||
E: Error + Send + 'static,
|
|
||||||
>(
|
|
||||||
mut executable: T,
|
mut executable: T,
|
||||||
|
op_code: u32,
|
||||||
) -> JoinHandle<Result<OpResult, E>> {
|
) -> JoinHandle<Result<OpResult, E>> {
|
||||||
// let executable = Arc::new(executable_unprotected);
|
|
||||||
thread::spawn(move || match T::EXEC_TYPE {
|
thread::spawn(move || match T::EXEC_TYPE {
|
||||||
ExecutionType::OneShot => executable.periodic_op(),
|
ExecutionType::OneShot => executable.periodic_op(op_code),
|
||||||
ExecutionType::Infinite => loop {
|
ExecutionType::Infinite => loop {
|
||||||
executable.periodic_op().expect("Periodic Op failed");
|
executable.periodic_op(op_code)?;
|
||||||
let freq = T::TASK_FREQ
|
let freq = T::TASK_FREQ
|
||||||
.unwrap_or_else(|| panic!("No task frequency specified for task {}", T::TASK_NAME));
|
.unwrap_or_else(|| panic!("No task frequency specified for task {}", T::TASK_NAME));
|
||||||
thread::sleep(freq)
|
thread::sleep(freq)
|
||||||
},
|
},
|
||||||
ExecutionType::Cycles(cycles) => {
|
ExecutionType::Cycles(cycles) => {
|
||||||
for _i in 0..cycles - 1 {
|
for _i in 0..cycles - 1 {
|
||||||
executable.periodic_op().unwrap();
|
executable.periodic_op(op_code)?;
|
||||||
let freq = T::TASK_FREQ.unwrap_or_else(|| {
|
let freq = T::TASK_FREQ.unwrap_or_else(|| {
|
||||||
panic!("No task frequency specified for task {}", T::TASK_NAME)
|
panic!("No task frequency specified for task {}", T::TASK_NAME)
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user