Const generics - lists

I have a struct which is meant to store, with minimal memory overhead, some data.

This struct may be either generated at build time and compiled in, or generated at runtime and loaded into the program.

The struct has a field that stores a list of tokens. I used generics to allow my tokens to be either String in the owned scenario or &'static str in the const scenario, but I can't find a way to unify the struct to work with lists of tokens in both.

Here's a playground with two structs: Rust Playground

The list will never grow once created, and in both cases it's read only.

Is it possible to generalize it into a single one?

Not without overhead, these two types have very different memory layouts. At a performance hit, you could use Cow<'static, [PatternElement<Cow<'static str>>]> to represent the data, but this has a bit of overhead on access.

1 Like

You can call .into_boxed_slice() on a String, and then Box::leak() it, so it'll give you a &'static str, and then you can keep merrily using &'static str everywhere.

Of course that leaks, so it's best to limit this to one-time-only global things. It's theoretically possible to free this memory if you can somehow know which &'static str was from leaked string, by calling Box::from_raw on it.

Even in non-hacky owned case, if the strings don't need to grow, then Box<str> is a bit smaller to store (2usize) than String (3usize).

@kornel - can you encode your idea in playground? I can't seem to follow it :frowning:

Like this, playground

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.