1use petr_core::SelectorWord;
2
3pub type TypedDenseOpFn<State, Context, Error> =
9 fn(&mut State, &Context) -> Result<(), Error>;
10
11pub struct TypedDenseOperationTable<State, Context, Error> {
17 entries: [TypedDenseOpFn<State, Context, Error>; 256],
18}
19
20impl<State, Context, Error> core::fmt::Debug
21 for TypedDenseOperationTable<State, Context, Error>
22{
23 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
24 f.debug_struct("TypedDenseOperationTable")
25 .field("slots", &self.entries.len())
26 .finish()
27 }
28}
29
30impl<State, Context, Error> Clone
31 for TypedDenseOperationTable<State, Context, Error>
32{
33 fn clone(&self) -> Self {
34 Self { entries: self.entries }
35 }
36}
37
38impl<State, Context, Error> TypedDenseOperationTable<State, Context, Error> {
39 pub fn new(fallback: TypedDenseOpFn<State, Context, Error>) -> Self {
41 Self { entries: [fallback; 256] }
42 }
43
44 pub fn register(
46 &mut self,
47 selector: u8,
48 operation: TypedDenseOpFn<State, Context, Error>,
49 ) {
50 self.entries[selector as usize] = operation;
51 }
52
53 pub fn register_word(
55 &mut self,
56 selector: SelectorWord,
57 operation: TypedDenseOpFn<State, Context, Error>,
58 ) {
59 self.register(selector.function_id(), operation);
60 }
61
62 pub fn execute(
64 &self,
65 selector: u8,
66 state: &mut State,
67 context: &Context,
68 ) -> Result<(), Error> {
69 (self.entries[selector as usize])(state, context)
70 }
71
72 pub fn execute_word(
74 &self,
75 selector: SelectorWord,
76 state: &mut State,
77 context: &Context,
78 ) -> Result<(), Error> {
79 self.execute(selector.function_id(), state, context)
80 }
81}