Advice on how to expose hashmap via C ABI and LLVM

Hi, this is a slightly off-topic and design-oriented question, but I am doing this in rust and i think this community probably has good advice.

I'm currently working on my own programming language. It is compiled to LLVM IR, and borrows some functionality in rust exposed via extern C functions.

I'm introducing some form of generics in my language, specifically a HashMap. In an ideal world, I would like to not write a hashmap implementation myself, and would rather expose Rust's HashMap implementation to the language.

I can't think of a way to make this happen. I understand that I can expose even generic functions via the C ABI to some extent, but I believe I would need some way to force rust to generate a generic variant for every Key, Value type of the hashmap, as well as name them something unique that I can refer to in the LLVM IR.

If I understand correctly, generics in rust involve building a function per parameter combination. E.G. in the case of HashMaps, there is a copy of the Rust stdlib, and that is used to construct the specialized versions.

In order to replicate that for my language, it seems like I would have to implement generics in my own language, then author my own version of a HashMap so that my compiler can generate specialized versions per parameter combination.

Is there any way to accomplish using rust's Hashmap implementation here?

Thanks for any help.

You can use C/Java style "generics". Write on Rust C API like:


#[no_mange]
extern "C" fn hashmap_new(hash_fn: extern "C" fn(*const c_void)->u32) -> *c_void;
#[no_mange]
extern "C" fn create_hashmap(hashmap: *c_void, key: *const c_void) -> *c_void;

I suppose idea is clear, key and data is pointer to c_void, and user should provide hash function.
Just like qsort from libc or how Java works with generics under the hood (all is Object).

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