From d9dc7febb040de8dbf6c775d28b5edd967613bae Mon Sep 17 00:00:00 2001 From: Joe McCain III Date: Sun, 1 Sep 2024 15:02:42 -0500 Subject: [PATCH] update Signed-off-by: Joe McCain III --- core/src/state/mod.rs | 42 ++++----- core/src/state/states/binary.rs | 154 -------------------------------- 2 files changed, 15 insertions(+), 181 deletions(-) delete mode 100644 core/src/state/states/binary.rs diff --git a/core/src/state/mod.rs b/core/src/state/mod.rs index b35c6e7..910c202 100644 --- a/core/src/state/mod.rs +++ b/core/src/state/mod.rs @@ -3,7 +3,7 @@ Contrib: FL03 */ #[doc(inline)] -pub use self::{halt::*, state::State, states::*}; +pub use self::{halt::*, state::State}; pub(crate) mod state; @@ -14,22 +14,18 @@ mod impls { pub mod impl_ops; pub mod impl_repr; } - -pub(crate) mod states { - #[doc(inline)] - pub use self::binary::*; - - pub(crate) mod binary; -} - pub(crate) mod prelude { pub use super::halt::Haltable; pub use super::state::State; - pub use super::states::*; + #[cfg(feature = "std")] pub use super::AnyState; + pub use super::MaybeState; } +#[cfg(feature = "std")] /// A type alias for a [State] whose inner value is the dynamically sized type of a boxed [`Any`](core::any::Any). -pub type AnyState = State>; +pub type AnyState = State>; +/// A type alias for a [State] whose inner value is a [core::mem::MaybeUninit] of generic type `Q`. +pub type MaybeState = State>; /// [RawState] is a trait describing objects capable of being used as states in our library. /// The trait contains a single associated trait, the context, or inner value of the state. @@ -39,9 +35,9 @@ pub trait RawState { private!(); - fn into_inner(self) -> Self::Q; + fn get(self) -> Self::Q; - fn get(&self) -> &Self::Q; + fn get_ref(&self) -> &Self::Q; fn get_mut(&mut self) -> &mut Self::Q; @@ -49,19 +45,10 @@ pub trait RawState { } #[doc(hidden)] -pub trait Stateful { - type State: RawState; +pub trait Apply { + type Output; - fn get(self) -> Q; - - fn get_mut(&mut self) -> &mut Q; - - fn set(&mut self, state: Q); - - fn map(self, f: F) -> Option - where - F: FnOnce(Q) -> U, - Self: Sized; + fn apply(self, f: F) -> Self::Output where F: FnOnce(Q) -> R; } /* @@ -74,11 +61,11 @@ macro_rules! impl_raw_state { seal!(); - fn into_inner(self) -> Q { + fn get(self) -> Q { self.$($field)* } - fn get(&self) -> &Q { + fn get_ref(&self) -> &Q { &self.$($field)* } @@ -102,3 +89,4 @@ impl_raw_state! { Halt(0), State(0), } + diff --git a/core/src/state/states/binary.rs b/core/src/state/states/binary.rs deleted file mode 100644 index 939ced3..0000000 --- a/core/src/state/states/binary.rs +++ /dev/null @@ -1,154 +0,0 @@ -/* - Appellation: state - Contrib: FL03 -*/ -use crate::State; - -#[derive( - Clone, - Copy, - Debug, - Eq, - Hash, - Ord, - PartialEq, - PartialOrd, - strum::EnumDiscriminants, - strum::EnumIs, - strum::VariantNames, -)] -#[cfg_attr( - feature = "serde", - derive(serde::Deserialize, serde::Serialize), - serde(rename_all = "lowercase"), - strum_discriminants( - derive(serde::Deserialize, serde::Serialize), - serde(rename_all = "lowercase") - ) -)] -#[repr(C)] -#[strum(serialize_all = "lowercase")] -#[strum_discriminants( - name(BinState), - derive( - Hash, - Ord, - PartialOrd, - strum::AsRefStr, - strum::Display, - strum::EnumCount, - strum::EnumIs, - strum::EnumIter, - strum::EnumString, - strum::VariantNames - ) -)] -pub enum BinaryState { - Invalid(I), - Valid(V), -} - -impl BinaryState { - pub fn invalid(state: I) -> Self { - Self::Invalid(state) - } - - pub fn valid(state: V) -> Self { - Self::Valid(state) - } - - pub fn invalidate(self, state: Q) -> BinaryState { - match self { - Self::Invalid(_) => BinaryState::Invalid(state), - Self::Valid(_) => BinaryState::Invalid(state), - } - } - - pub fn kind(&self) -> BinState { - match self { - Self::Invalid(_) => BinState::Invalid, - Self::Valid(_) => BinState::Valid, - } - } -} - -impl BinaryState { - pub fn into_inner(self) -> State { - match self { - Self::Invalid(q) => State(q), - Self::Valid(q) => State(q), - } - } - - pub fn state(&self) -> (BinState, &Q) { - (self.kind(), self.as_ref()) - } -} - -impl AsRef for BinaryState { - fn as_ref(&self) -> &Q { - match self { - Self::Invalid(q) => q, - Self::Valid(q) => q, - } - } -} - -impl AsMut for BinaryState { - fn as_mut(&mut self) -> &mut Q { - match self { - Self::Invalid(q) => q, - Self::Valid(q) => q, - } - } -} - -impl core::borrow::Borrow for BinaryState { - fn borrow(&self) -> &Q { - self.as_ref() - } -} - -impl core::borrow::BorrowMut for BinaryState { - fn borrow_mut(&mut self) -> &mut Q { - self.as_mut() - } -} - -impl Default for BinState { - fn default() -> Self { - Self::Invalid - } -} - -impl Default for BinaryState -where - I: Default, -{ - fn default() -> Self { - Self::invalid(::default()) - } -} - -impl core::ops::Deref for BinaryState { - type Target = Q; - - fn deref(&self) -> &Self::Target { - self.as_ref() - } -} - -impl core::ops::DerefMut for BinaryState { - fn deref_mut(&mut self) -> &mut Self::Target { - self.as_mut() - } -} - -impl core::fmt::Display for BinaryState -where - Q: core::fmt::Display, -{ - fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { - write!(f, "{}", *self) - } -}