string-interner icon indicating copy to clipboard operation
string-interner copied to clipboard

Add fallible APIs for `StringInterner`

Open royaltm opened this issue 3 months ago • 2 comments

First I want to say that this is a pretty awesome project. It provides interning for strings only but that's exactly what I needed. Other interning crates are usually more generic, but this always comes with some strings attached (no pun intended).

My point being - this is the only decent interning crate that works for no_std that I was able to find.

That said, string-interner API is designed with an assumption most modern projects make: the memory is the last thing to go. (so is the Rust std/alloc btw. but that's another story and it's currently changing)

Unfortunately in embedded systems this is not the case, and RAM is usually one of the fastest-depleting resources.

You might think it's a bit insane trying to build a JIT MAL compiler for a little MCU board, but that's exactly what I'm doing 🤔.

My humble request is if you can add a fallible API calls to StringInterner so users can take some action on failure instead of just relying on a watchdog to restart the firmware on panic.

The failure being either running out of symbols or RAM whichever comes first.

For example:

fn try_get_or_intern<T: AsRef<str>>(&mut self, string: T) -> Result<<B as Backend>::Symbol, TryReserveError>;
fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>;

#[derive(Debug, Clone, Copy)]
enum TryReserveError {
    NoMoreSymbols
    AllocationError
}

where try_reserve allows to change capacity ahead of time. This may also come with a complementary capacity method and a reserve method that panics.

I'm aware that this might be a big ask, involving substantial internal changes, but I'm ready to contribute myself if you are willing to go forward with this.

At least please let me know your thoughts. Thanks!

royaltm avatar May 26 '24 14:05 royaltm