Wasmer server-side WebAssembly runtime comes in multiple flavors. Written in Rust, it is embedded in different languages, such as C, Python, Go, Ruby,

Novel way to Develop, Test and Document C libraries from Rust

submited by
Style Pass
2021-07-15 19:30:05

Wasmer server-side WebAssembly runtime comes in multiple flavors. Written in Rust, it is embedded in different languages, such as C, Python, Go, Ruby, PHP, Java, and many more (discover all the integrations).

As many of our embeddings, the C embedding is implemented in Rust itself. Rust is perfectly capable of exposing a C compatible ABI (learn more by reading The Rustnomicon Book, the Foreign Function Interface Chapter). Moreover, Rust comes with a first-class tooling for testing and documenting projects. We wanted to leverage those tools to:

Why? Because they are simple, they work everywhere, and every Rust programmer knows them. They are part of the foundation of the Rust ecosystem.

Well, first, thanks for asking dear reader! And second, what an excellent idea! Good news: With a little modification, that's possible. We have written the inline-c Rust crate to fulfill this precise purpose. Add inline-c = "0.1" to your [dev-dependencies] in your Cargo.toml file, and let's see it in action:

It's a little bit more verbose because we have to write int main() { … } etc. That's on purpose, because we might want to add some #include, or more C code around it. It is also possible to assert on the results of the C program. Let's see another example of how to add assertions on stdout for example:

Leave a Comment