How to create a new static variable per monomorphization?

I need to create a static variable per generic monomorphization (its type depends on the generic argument) and I promise it has no interior mutability, as apparently it is the problem with statics monomorphization.

Is there some alternative for me on stable rust? Some crate, perhaps?

The actual problem is that I have this struct:

struct MerkleTree<
    T: FieldElement,
    const N_LEAVES_LOG: usize,
    const WORDS_PER_PAGE: usize,
> {
   // ...
}

And for each different T, N_LEAVES_LOG and WORDS_PER_PAGE, there is a [[T; 4]; N_LEAVES_LOG] value that I need to compute. It is fixed for the combination of generic arguments, and I'd rather just do it once. Using const would be ideal, but the complex functions I use to compute the values are too hard to write as const functions (if possible at all).

Both consts and immutable statics need to be computed by const functions, so statics don't help you here. And whether something is possible to write as const is unrelated to difficulty. It's still a complete programming language, you just aren't able to do things like I/O and allocations.

1 Like

Both consts and immutable statics need to be computed by const functions, so statics don't help you here.

Indeed, so I'll have to break my promise to never internal mutate. Instead I promise I'll mutate it only once with OnceCell, and the value I write is always the same for the same variable definition.

And whether something is possible to write as const is unrelated to difficulty. It's still a complete programming language, you just aren't able to do things like I/O and allocations.

Nor I can use for, nor mutable references, nor any non-const function, not even the ones that are provably pure, but the original programmer forgot to mark as const. How is that not more difficult?

1 Like

It will probably be more difficult, all I mean is that you should be able to tell just by inspecting the required operations whether or not it is possible. For-loops and mutable refs are avoidable.

This would be fine, but there's still no way to make generic statics. Const or typemap are probably your best solutions.

Thanks! I would never find TypeMap by myself!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.