use petr_core::SelectorWord;
pub type TypedDenseOpFn<State, Context, Error> =
fn(&mut State, &Context) -> Result<(), Error>;
pub struct TypedDenseOperationTable<State, Context, Error> {
entries: [TypedDenseOpFn<State, Context, Error>; 256],
}
impl<State, Context, Error> core::fmt::Debug
for TypedDenseOperationTable<State, Context, Error>
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("TypedDenseOperationTable")
.field("slots", &self.entries.len())
.finish()
}
}
impl<State, Context, Error> Clone
for TypedDenseOperationTable<State, Context, Error>
{
fn clone(&self) -> Self {
Self { entries: self.entries }
}
}
impl<State, Context, Error> TypedDenseOperationTable<State, Context, Error> {
pub fn new(fallback: TypedDenseOpFn<State, Context, Error>) -> Self {
Self { entries: [fallback; 256] }
}
pub fn register(
&mut self,
selector: u8,
operation: TypedDenseOpFn<State, Context, Error>,
) {
self.entries[selector as usize] = operation;
}
pub fn register_word(
&mut self,
selector: SelectorWord,
operation: TypedDenseOpFn<State, Context, Error>,
) {
self.register(selector.function_id(), operation);
}
pub fn execute(
&self,
selector: u8,
state: &mut State,
context: &Context,
) -> Result<(), Error> {
(self.entries[selector as usize])(state, context)
}
pub fn execute_word(
&self,
selector: SelectorWord,
state: &mut State,
context: &Context,
) -> Result<(), Error> {
self.execute(selector.function_id(), state, context)
}
}