Hi all,
I'm experimenting a bit with mixing WASM and native Rust. Basically, I'm trying to write some Rust functions that can be executed natively or can instead be called from a guest wasmtime runtime. Based on some runtime parameters I'd like to be able to switch between the two execution methods. The simple solution here seems to just be to use the Linker to import the function from the host. While this should work, it is also imperative for this use case that the function be able to be inlined (in my case they should be inlined every time). The optimizations that can be applied to the full inlined WASM code are really important.
For functions that are already WASM we luckily now have the new inliner for wasmtime and cranelift to save the day. As far as I can tell though it's not possible to generate inline code for native Rust functions you link to. I was wondering if anyone had any suggestions for how to achieve this behavior?
The only solution I've been able to think of, is to write all of the functions I need, then to have a build.rs use the wasm32 Rust toolchain to compile each of them to their own WASM files. Then at runtime I could load the WASM files into the wasmtime runtime and maintain some mapping between the Rust function and it's precompiled WASM snippet. But this seems like a huge headache.
More-or-less what you're describing is what you'll need to do, which is to translate host functions you want to inline to wasm somehow. Wasmtime also supports host intrinsics written in wasm which are unsafe and have special access to the host for this purpose, but that still requires them to be modeled as wasm/components
Thanks for the confirmation! I'll have to think more about the best way to go about this then. If nothing else maybe modeling the functions in a component will make it easier to map between the Rust host function and the WASM code.
Last updated: Jan 10 2026 at 20:04 UTC