core fixes

This commit is contained in:
John Smith
2022-11-29 19:22:33 -05:00
parent f7582fabb2
commit 5c0a500971
60 changed files with 98 additions and 1160 deletions

View File

@@ -0,0 +1,102 @@
use super::*;
pub use rust_fsm;
pub use rust_fsm::*;
pub type StateChangeCallback<T> = Arc<
dyn Fn(<T as StateMachineImpl>::State, <T as StateMachineImpl>::State) + Send + Sync + 'static,
>;
struct CallbackStateMachineInner<T>
where
T: StateMachineImpl,
T::State: Copy + Unpin + core::fmt::Debug,
{
state: T::State,
callback: Option<StateChangeCallback<T>>,
eventual: EventualValueClone<T::State>,
}
impl<T> core::fmt::Debug for CallbackStateMachineInner<T>
where
T: StateMachineImpl,
T::State: Copy + Unpin + core::fmt::Debug,
{
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
write!(f, "CallbackStateMachineInner(state: {:?})", self.state)
}
}
#[derive(Debug, Clone)]
pub struct CallbackStateMachine<T>
where
T: StateMachineImpl,
T::State: Copy + Unpin + core::fmt::Debug,
{
inner: Arc<Mutex<CallbackStateMachineInner<T>>>,
}
impl<T> CallbackStateMachine<T>
where
T: StateMachineImpl,
T::State: Copy + Unpin + core::fmt::Debug,
{
pub fn new() -> Self {
Self {
inner: Arc::new(Mutex::new(CallbackStateMachineInner {
state: T::INITIAL_STATE,
callback: None,
eventual: EventualValueClone::new(),
})),
}
}
pub fn set_state_change_callback(&self, callback: StateChangeCallback<T>) {
self.inner.lock().callback = Some(callback);
}
// pub fn clear_state_change_callback(&self) {
// self.inner.lock().callback = None;
// }
// pub fn state_eventual_instance(&self) -> (T::State, EventualValueCloneFuture<T::State>) {
// let inner = self.inner.lock();
// (inner.state, inner.eventual.instance())
// }
pub async fn consume(&self, input: &T::Input) -> Result<Option<T::Output>, ()> {
let current_state = self.inner.lock().state;
if let Some(new_state) = T::transition(&current_state, input) {
let output = T::output(&current_state, input);
let old_state = current_state;
let (callback, eventual) = {
let mut inner = self.inner.lock();
inner.state = new_state;
let eventual =
core::mem::replace(&mut inner.eventual, EventualValueClone::<T::State>::new());
(inner.callback.clone(), eventual)
};
if let Some(cb) = callback {
cb(old_state, new_state);
}
eventual.resolve(new_state).await;
Ok(output)
} else {
Err(())
}
}
pub fn state(&self) -> T::State {
self.inner.lock().state
}
}
impl<T> Default for CallbackStateMachine<T>
where
T: StateMachineImpl,
T::State: Copy + Unpin + core::fmt::Debug,
{
fn default() -> Self {
Self::new()
}
}

View File

@@ -1,6 +1,7 @@
// mod bump_port;
mod async_peek_stream;
mod async_tag_lock;
mod callback_state_machine;
mod clone_stream;
mod eventual;
mod eventual_base;
@@ -110,6 +111,7 @@ cfg_if! {
// pub use bump_port::*;
pub use async_peek_stream::*;
pub use async_tag_lock::*;
pub use callback_state_machine::*;
pub use clone_stream::*;
pub use eventual::*;
pub use eventual_base::{EventualCommon, EventualResolvedFuture};

View File

@@ -467,7 +467,7 @@ pub async fn test_get_random_u64() {
let t2 = get_timestamp();
let tdiff = ((t2 - t1) as f64) / 1000000.0f64;
info!(
"running network interface test with {} iterations took {} seconds",
"running get_random_u64 with {} iterations took {} seconds",
count, tdiff
);
}
@@ -482,7 +482,7 @@ pub async fn test_get_random_u32() {
let t2 = get_timestamp();
let tdiff = ((t2 - t1) as f64) / 1000000.0f64;
info!(
"running network interface test with {} iterations took {} seconds",
"running get_random_u32 with {} iterations took {} seconds",
count, tdiff
);
}