Is it possible to use a C++ library from Rust when the library (e.g. Boost) uses templates (generics)?
Rust can generate C dynamic libraries ( . so files) as well as static libraries ( . a files), which can be easily wrapped in Go bindings and Python bindings and used in code written in those languages. You can refer to the full code of the C library written in Rust in the clib folder of the GitHub repository.
Rust's generics are much more principled than templates, but they are dependent on types conforming to specific APIs. If a type does not implement the required interface, then it is impossible to use the associated functions, even if they may be perfectly valid.
Yes, but it may not be practical.
The D programming language is one of the very few providing some degree of C++ interoperability; you can read more about it on dlang.
Note the limitation for the template section:
Note that all instantiations used in D code must be provided by linking to C++ object code or shared libraries containing the instantiations.
which essentially means that you must use C++ code to cause the instantiation of the templates with the right types, and then the D compiler will link against those instantiations.
You could do the same for Rust. Without compiler support, this means mangling the names manually. In the FFI section, you will find the link attribute:
#[link(name = "snappy")]
extern {
fn snappy_max_compressed_length(source_length: size_t) -> size_t;
}
which tells the compiler which linked library will provide the symbol, you will also support for various calling conventions and the no_mangle attribute.
You may need to apply #[allow(non_snake_case)]
as appropriate.
Servo uses bindgen to generate Rust bindings for C and C++ code; I am unclear on the level of C++ support, and somewhat doubtful that it can handle templates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With