3 ms·
Can Rust instantiate a C++ template? How do you interop across interfaces that use templates?
by haberman 5y ago
Can Rust instantiate a C++ template? How do you interop across interfaces that use templates?
- pcwalton 5y agoYes! The tutorial for cxx shows how you can use common templated types like std::vector: https://cxx.rs/index.html https://cxx.rs/index.html More details are in the crate documentation: https://docs.rs/cxx/1.0.49/cxx/ https://docs.rs/cxx/1.0.49/cxx/ Note that the philosophy of cxx isn't to support all of C++'s semantics (templates make this impossible in the limit as Rust has no SFINAE rule for example), but rather to support the important use cases that arise in practice. It's also worth noting that, these days, bindgen has support for instantiating C++ templates, though it operates at a much lower level. Compared to cxx, bindgen is oriented more toward being comprehensive than being easy.
- vlovich123 5y agoHave you gotten a chance to use cxx in a large “production” “legacy” c++ codebase? It’s great but there’s quite a few roadblocks that I encountered even trying something I would have considered that should be “easy”. I could easily imagine that Chromium would have an even tougher time. Some highlights: * if you’re not careful, you’ll end up with linkage issues if you link in multiple crates. This means 1 uber create that has all your Rust dependencies. This can mess with parallelism in your build since Cargo isn’t coordinating with the broader system. * speaking of build systems, chromium probably wouldn’t use create meaning extra build system integration, especially since they don’t use Bazel/Blaze. * cxx still can’t pass across certain types (Arc and Option being the big ones) and is missing a general story for extending it with custom types. * cxx can frequently cause you to have to reach to unsafe anyway because the lifetime semantics you want to express cannot be done in pure Rust because the other half lives in c++. Don’t get me wrong. Rust is fantastic and a joy to use. Dismissing the challenges a project like Chromium has as “they just want to stick to c++” isn’t a fair look at the situation. Lots of smart people work on these projects and are keenly aware of Rust. Additionally managers and executives are keenly aware of the security benefits so there’s definitely downward pressure to try to adopt these (and large efforts spent investigating how to enable this whole maintaining developer velocity).
- haberman 5y agoThanks for the references, I'll definitely have to check those out.