Poll changes

This commit is contained in:
Zachary Sunforge
2023-06-26 13:54:00 -07:00
parent abfa21ff01
commit 1c25e51a8a
8 changed files with 105 additions and 46 deletions

View File

@ -1,4 +1,6 @@
/// An error that it is likely impossible to recover. This error should only be created in
use crate::transducer::InvalidValue;
/// An error that it is likely impossible to recover from. This error should only be created in
/// situations where attempts to recover have already been attempted and have failed. Error handling
/// should consist of attempting to alert another system for maintenance and attempting to shut down
/// any systems that depend on the correct functionality of the component having errors.
@ -9,4 +11,31 @@
pub enum CriticalError {
/// Critical communication failed and retries are either impossible or also failed.
Communication,
InvalidValue(InvalidValue),
}
impl CriticalError {
pub fn emergency_procedure(self, procedure: impl FnOnce(CriticalError) -> !) -> ! {
procedure(self);
}
}
/// [Result] where error type is [CriticalError].
pub trait CriticalErrResult: Copy {
type Value: Copy;
/// Execute emergency procedure in the event of a critical, the emergency procedure cannot
/// return. It should usually terminate the program, potentially rebooting the device in some sort of recovery mode.
fn err_emproc(self, procedure: impl FnOnce(CriticalError) -> !) -> Self::Value;
}
impl<T: Copy> CriticalErrResult for Result<T, CriticalError> {
type Value = T;
fn err_emproc(self, procedure: impl FnOnce(CriticalError) -> !) -> Self::Value {
match self {
Ok(val) => val,
Err(error) => error.emergency_procedure(procedure),
}
}
}

View File

@ -1,5 +1,5 @@
#![no_std]
#![feature(async_fn_in_trait)]
#![feature(async_fn_in_trait, never_type)]
pub mod transducer;
pub mod cell;

View File

@ -1,7 +1,6 @@
use crate::CriticalError;
pub trait Poll {
type Value: Copy;
type Error: Copy;
async fn poll(&self) -> Result<Self::Value, CriticalError>;
async fn poll(&self) -> Result<Self::Value, Self::Error>;
}

View File

@ -51,3 +51,10 @@ impl<T: Copy> From<Cell<T>> for State<T> {
State::new(state_cell)
}
}
// ---------------------------------------------------------------------------------------------------------------------
// ----- Error ------------------------
// ---------------------------------------------------------------------------------------------------------------------
/// Indicates the transducer value is statically known to be impossible.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct InvalidValue;

View File

@ -1,9 +1,8 @@
use crate::transducer::InvalidValue;
pub trait Output {
type Value: Copy;
//TODO: Should this be maybe async?
fn output(&mut self, setting: Self::Value) -> Result<(), InvalidOutputSetting>;
fn output(&mut self, setting: Self::Value) -> Result<(), InvalidValue>;
}
/// Indicates the setting given for an [Output] is statically known to be an impossible setting to achieve.
pub struct InvalidOutputSetting;