What does HackerNews think of cbindgen?

A project for generating C bindings from Rust code

Language: Rust

You can backport Rust standard library to C using https://github.com/eqrion/cbindgen .
Using Rust code in D should be relatively easy: expose C API, compile it into a shared library and use it on the D side as any other library. cbindgen (https://github.com/eqrion/cbindgen) can help with generation of header files. Static linking is possible as well.

Using D code in Rust probably will be much more painful and I am not sure if it can be done 100% safely...

Mozilla's uses a combination of https://github.com/eqrion/cbindgen and https://github.com/rust-lang/rust-bindgen depending on the direction of interop. These tools don't provide the safety guarantees of https://github.com/dtolnay/cxx but the also both predate cxx and C++ doesn't have much in terms of safety guarantees to begin with so the bar is pretty low.
https://github.com/libp2p/rust-libp2p

There's the Rust repository, and the auto-generated documentation can be found at https://docs.rs/libp2p/0.11.0/libp2p/

I'm curious what about a C implementation is the requirement for you? Is it the ability for to link against your C/C++ code, or for reading and understanding the structures and algorithms with the simplicity of C parlance, or another reason?

If the first one, it is possible to create a C shared-library "cdylib" with Rust (included in cargo by default), and using cbindgen[1] you can generate the headers to use the c-api.

It doesn't look like anybody has done that yet, unfortunately. But know that it should be a lot easier to create C/C++ compatibility than doing a rewrite or port.

[1] https://github.com/eqrion/cbindgen

EDIT:

Or as the sibling comment points out, there ARE C implementations out there, so you can ignore this post.

Rust works pretty well with C, and you could use cbindgen[0] to generate c headers for rust functions marked as extern and it would work when linked together. However, rustls doesn't seems to be providing any such function. Most of its API is rust-based, using stuff that isn't directly available in C (Option, Vec, etc.). So you would have to write at least some rust code that calls the library, and have your C code calls the rust code that you write.

On the flip side, calling C code in rust is pretty easy - you have the entire libc available, and rust-bindgen[1] generate bindings for you, which in my experience works very well. So, you may also consider writing your program in rust… If that's possible.

  [0]: https://github.com/eqrion/cbindgen
  [1]: https://github.com/rust-lang/rust-bindgen
Are you aware of cbindgen[0]? It's not exactly what you want, but it's certainly far closer than wasm-bindgen is.

[0]: https://github.com/eqrion/cbindgen