use crate::{Halt, RawState, State};
impl<Q, H> RawState for Halt<Q, H>
where
Q: RawState,
H: RawState,
{
seal! {}
}
impl<Q, H> Halt<Q, H>
where
Q: RawState,
H: RawState,
{
pub const fn from_halt(state: H) -> Self {
Self::Halt(state)
}
pub const fn from_state(state: Q) -> Self {
Self::Step(state)
}
pub const fn swap(&mut self, other: &mut Halt<Q, H>) {
match (self, other) {
(Self::Step(a), Self::Step(b)) => core::mem::swap(a, b),
(Self::Halt(a), Self::Halt(b)) => core::mem::swap(a, b),
_ => {}
}
}
pub const fn view(&self) -> Halt<&Q, &H> {
match self {
Self::Step(inner) => Halt::Step(inner),
Self::Halt(inner) => Halt::Halt(inner),
}
}
pub const fn view_mut(&mut self) -> Halt<&mut Q, &mut H> {
match self {
Self::Step(inner) => Halt::Step(inner),
Self::Halt(inner) => Halt::Halt(inner),
}
}
pub fn to_owned(&self) -> Halt<Q, H>
where
Q: Clone,
H: Clone,
{
match self {
Self::Step(inner) => Halt::Step(inner.clone()),
Self::Halt(inner) => Halt::Halt(inner.clone()),
}
}
}
impl<Q> Halt<Q, Q>
where
Q: RawState,
{
#[inline]
pub fn into_inner(self) -> Q {
match self {
Self::Step(inner) => inner,
Self::Halt(inner) => inner,
}
}
#[inline]
pub fn halt(self) -> Halt<Q, Q> {
match self {
Self::Step(inner) => Halt::Halt(inner),
_ => self,
}
}
#[inline]
pub fn into_state(self) -> State<Q> {
match self {
Self::Step(inner) => State(inner),
Self::Halt(inner) => State(inner),
}
}
#[inline]
pub fn into_halt_state(self) -> State<Halt<Q>> {
State(self)
}
pub const fn as_halt_state(&self) -> State<Halt<&Q>> {
State(self.view())
}
pub const fn as_mut_halt_state(&mut self) -> State<Halt<&mut Q>> {
State(self.view_mut())
}
pub const fn as_state(&self) -> State<&Q> {
State(self.get())
}
pub const fn as_mut_state(&mut self) -> State<&mut Q> {
State(self.get_mut())
}
pub const fn get(&self) -> &Q {
match self {
Self::Step(inner) => inner,
Self::Halt(inner) => inner,
}
}
pub const fn get_mut(&mut self) -> &mut Q {
match self {
Self::Step(inner) => inner,
Self::Halt(inner) => inner,
}
}
pub const fn replace(&mut self, state: Q) -> Q {
core::mem::replace(self.get_mut(), state)
}
pub fn set(&mut self, state: Q) {
*self.get_mut() = state;
}
}
impl<'a, Q, H> Halt<&'a Q, &'a H>
where
Q: RawState,
H: RawState,
{
#[inline]
pub fn cloned(self) -> Halt<Q, H>
where
Q: Clone,
H: Clone,
{
match self {
Self::Step(inner) => Halt::Step(inner.clone()),
Self::Halt(inner) => Halt::Halt(inner.clone()),
}
}
#[inline]
pub fn copied(self) -> Halt<Q, H>
where
Q: Copy,
H: Copy,
{
match self {
Self::Step(&inner) => Halt::Step(inner),
Self::Halt(&inner) => Halt::Halt(inner),
}
}
}
impl<'a, Q, H> Halt<&'a mut Q, &'a mut H>
where
Q: RawState,
H: RawState,
{
#[inline]
pub fn cloned(self) -> Halt<Q, H>
where
Q: Clone,
H: Clone,
{
match self {
Self::Step(inner) => Halt::Step(inner.clone()),
Self::Halt(inner) => Halt::Halt(inner.clone()),
}
}
#[inline]
pub fn copied(self) -> Halt<Q, H>
where
Q: Copy,
H: Copy,
{
match self {
Self::Step(&mut inner) => Halt::Step(inner),
Self::Halt(&mut inner) => Halt::Halt(inner),
}
}
}
impl<Q> AsRef<Q> for Halt<Q>
where
Q: RawState,
{
fn as_ref(&self) -> &Q {
self.get()
}
}
impl<Q> AsMut<Q> for Halt<Q>
where
Q: RawState,
{
fn as_mut(&mut self) -> &mut Q {
self.get_mut()
}
}
impl<Q> core::ops::Deref for Halt<Q>
where
Q: RawState,
{
type Target = Q;
fn deref(&self) -> &Self::Target {
self.get()
}
}
impl<Q> core::ops::DerefMut for Halt<Q>
where
Q: RawState,
{
fn deref_mut(&mut self) -> &mut Self::Target {
self.get_mut()
}
}
impl<Q> Default for Halt<Q>
where
Q: Default + RawState,
{
fn default() -> Self {
Self::Step(Default::default())
}
}
impl<Q> From<State<Q>> for Halt<Q>
where
Q: RawState,
{
fn from(State(state): State<Q>) -> Self {
Self::Step(state)
}
}