#[derive(Debug)]
pub enum State<'a, T>
where
T: Ord,
{
IsolatedEmpty,
IsolatedWithData(String),
NonTerminalEmpty,
NonTerminalWithData(Vec<u8>),
SinkEmpty,
SinkWithData(char),
SourceEmpty,
SourceWithData(&'a mut T),
}
pub enum StateEntry<'state, 'a, T>
where
T: Ord,
{
IsolatedEmpty,
IsolatedWithData(&'state mut String),
NonTerminalEmpty(NonTerminalEmpty<'state, 'a, T>),
NonTerminalWithData(NonTerminalWithData<'state, 'a, T>),
SinkEmpty,
SinkWithData(&'state mut char),
SourceEmpty(SourceEmpty<'state, 'a, T>),
SourceWithData(SourceWithData<'state, 'a, T>),
}
impl<'state, 'a, T> ::core::convert::From<&'state mut State<'a, T>>
for StateEntry<'state, 'a, T>
where
T: Ord,
{
fn from(value: &'state mut State<'a, T>) -> Self {
match value {
State::IsolatedEmpty => StateEntry::IsolatedEmpty,
State::IsolatedWithData(it) => StateEntry::IsolatedWithData(it),
State::NonTerminalEmpty => {
StateEntry::NonTerminalEmpty(NonTerminalEmpty(value))
}
State::NonTerminalWithData(_) => {
StateEntry::NonTerminalWithData(NonTerminalWithData(value))
}
State::SinkEmpty => StateEntry::SinkEmpty,
State::SinkWithData(it) => StateEntry::SinkWithData(it),
State::SourceEmpty => StateEntry::SourceEmpty(SourceEmpty(value)),
State::SourceWithData(_) => StateEntry::SourceWithData(SourceWithData(value)),
}
}
}
impl<'a, T> State<'a, T>
where
T: Ord,
{
#[allow(clippy::needless_lifetimes)]
pub fn entry<'state>(&'state mut self) -> StateEntry<'state, 'a, T> {
self.into()
}
}
pub struct NonTerminalEmpty<'state, 'a, T>(
&'state mut State<'a, T>,
)
where
T: Ord;
pub struct NonTerminalWithData<'state, 'a, T>(
&'state mut State<'a, T>,
)
where
T: Ord;
pub struct SourceEmpty<'state, 'a, T>(
&'state mut State<'a, T>,
)
where
T: Ord;
pub struct SourceWithData<'state, 'a, T>(
&'state mut State<'a, T>,
)
where
T: Ord;
#[allow(clippy::needless_lifetimes)]
impl<'state, 'a, T> ::core::convert::AsRef<Vec<u8>>
for NonTerminalWithData<'state, 'a, T>
where
T: Ord,
{
fn as_ref(&self) -> &Vec<u8> {
match &self.0 {
State::NonTerminalWithData(it) => it,
_ => ::core::panic!("entry struct was instantiated with a mismatched state"),
}
}
}
#[allow(clippy::needless_lifetimes)]
impl<'state, 'a, T> ::core::convert::AsMut<Vec<u8>>
for NonTerminalWithData<'state, 'a, T>
where
T: Ord,
{
fn as_mut(&mut self) -> &mut Vec<u8> {
match &mut self.0 {
State::NonTerminalWithData(it) => it,
_ => ::core::panic!("entry struct was instantiated with a mismatched state"),
}
}
}
#[allow(clippy::needless_lifetimes)]
impl<'state, 'a, T> ::core::convert::AsRef<&'a mut T> for SourceWithData<'state, 'a, T>
where
T: Ord,
{
fn as_ref(&self) -> &&'a mut T {
match &self.0 {
State::SourceWithData(it) => it,
_ => ::core::panic!("entry struct was instantiated with a mismatched state"),
}
}
}
#[allow(clippy::needless_lifetimes)]
impl<'state, 'a, T> ::core::convert::AsMut<&'a mut T> for SourceWithData<'state, 'a, T>
where
T: Ord,
{
fn as_mut(&mut self) -> &mut &'a mut T {
match &mut self.0 {
State::SourceWithData(it) => it,
_ => ::core::panic!("entry struct was instantiated with a mismatched state"),
}
}
}
#[allow(clippy::needless_lifetimes)]
impl<'state, 'a, T> NonTerminalEmpty<'state, 'a, T>
where
T: Ord,
{
pub fn sink_empty(self) {
match ::core::mem::replace(self.0, State::SinkEmpty) {
State::NonTerminalEmpty => {}
_ => ::core::panic!("entry struct was instantiated with a mismatched state"),
}
}
}
#[allow(clippy::needless_lifetimes)]
impl<'state, 'a, T> NonTerminalWithData<'state, 'a, T>
where
T: Ord,
{
pub fn sink_with_data(self, next: char) -> Vec<u8> {
match ::core::mem::replace(self.0, State::SinkWithData(next)) {
State::NonTerminalWithData(it) => it,
_ => ::core::panic!("entry struct was instantiated with a mismatched state"),
}
}
}
#[allow(clippy::needless_lifetimes)]
impl<'state, 'a, T> SourceEmpty<'state, 'a, T>
where
T: Ord,
{
pub fn non_terminal_empty(self) {
match ::core::mem::replace(self.0, State::NonTerminalEmpty) {
State::SourceEmpty => {}
_ => ::core::panic!("entry struct was instantiated with a mismatched state"),
}
}
}
#[allow(clippy::needless_lifetimes)]
impl<'state, 'a, T> SourceEmpty<'state, 'a, T>
where
T: Ord,
{
pub fn non_terminal_with_data(self, next: Vec<u8>) {
match ::core::mem::replace(self.0, State::NonTerminalWithData(next)) {
State::SourceEmpty => {}
_ => ::core::panic!("entry struct was instantiated with a mismatched state"),
}
}
}
#[allow(clippy::needless_lifetimes)]
impl<'state, 'a, T> SourceWithData<'state, 'a, T>
where
T: Ord,
{
pub fn non_terminal_empty(self) -> &'a mut T {
match ::core::mem::replace(self.0, State::NonTerminalEmpty) {
State::SourceWithData(it) => it,
_ => ::core::panic!("entry struct was instantiated with a mismatched state"),
}
}
}
#[allow(clippy::needless_lifetimes)]
impl<'state, 'a, T> SourceWithData<'state, 'a, T>
where
T: Ord,
{
pub fn to_non_terminal_with_data(self, next: Vec<u8>) -> &'a mut T {
match ::core::mem::replace(self.0, State::NonTerminalWithData(next)) {
State::SourceWithData(it) => it,
_ => ::core::panic!("entry struct was instantiated with a mismatched state"),
}
}
}