rust-peg icon indicating copy to clipboard operation
rust-peg copied to clipboard

Add options to specify cache type

Open stevefan1999-personal opened this issue 1 year ago • 4 comments

Part 3 of #294 series, and this PR focuses on letting the user to specify their own cache type under context such as no_std or when the user needs to explicitly state the kind of cache type they wanted to use. This means users can now supply LinearMap in heapless - Rust (docs.rs), SgMap in scapegoat - Rust (docs.rs), or even Simsys/fchashmap: A fixed capacity no_std hashmap (github.com)

As I tried before I want to consolidate the cache interface into one facade trait stated here but I quickly found that I would have to use a Box to wrap the dynamic trait -- which is not acceptable in no_std environment. This means we have to resort to using "duck typing" that we have to specify concrete type in the cache type definition. The cache type must follow the following "trait protocol":

pub trait GrammarRuleCache<T> {
    fn insert(&mut self, k: usize, v: T);
    fn get(&self, k: usize) -> Option<&T>;
}

And the type must implement Default

This PR is based on #336 and #337 as a prerequisite.

stevefan1999-personal avatar Apr 05 '23 09:04 stevefan1999-personal