> I was looking for something like: static h:HashMap<K,C> =
> {(K,V),(K,V).....}.
> Is this possible at all?

Unfortunately this is more difficult than it seems. One of the nice
things about the standard HashMap implementation is that it keys the
hashes with random values (stored in each hash map). If this were a
compile-time constant, that randomness would be lost (along with the
randomness having to be generated at compile time). The more pressing
issue, however, is that there is no "initialization phase" in rust,
there are only compile-time constants. If you can't calculate the
actual layout in memory of a static struct (as in it requires a
function call), then currently there's no way to stick it in a global.

Similarly, internally hash maps use ~ vectors which are allocated on
the heap, another value which is currently not able to be allocated at
compile time.

> Any potential equivalents in the std/ext library to
> just writing a custom map of my own to do the same?

You would have to write your own version, but remember that it would
have to be a compile-time constant. Each of the key/value pairs would
have to be constant, and you would have to use slices instead of
allocated vectors. What you probably want is a long-lived HashMap that
it initialized somewhere in your code and maybe passed around.

One other option would be to use thread local storage to squirrel away
the hash map after your program initializes and then to read it from
there. And the last other option you may wish to pursue is to put the
map in a `static mut` slot. This would require unsafe code to both
access/read, and it would also require unsafe code to cast the hash
map to a store-able value in a static mut (which also has to be
statically initialized). You could wrap this all in a safe-interface
most likely, but it's not guaranteed.
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to