This seems doable for simple cases, but libraries like Boost push templates to their limits, so they're not the easy cases.
Basically it would be a "extern C++". The FFI to C++ would hopefully have as low overhead as "extern C" while making it easier for C++ codebases to gradually adopt Rust.
This is already supported (via Bindgen).
However, it would also be extremely hard; I don't think any programming language has ever created such a tight bridge to C++. The existing approaches I've seen are:
- Swig, rust-bindgen, etc.: Pass through auto-generated C ABI wrappers; support for generics is limited and requires declaring up front what types you want to instantiate them with.
- D: You can bind directly to C++ if you rewrite your C++ header in D, generics and all... including the implementations of all inline functions.
Both very limited, especially in template-happy modern C++.
The whole point of UWP was to improve COM to make it even better for language interop, increasing the kind of language features that can get exposed as COM libraries.
Also the number one reason that if Rust wants to succeed as system language on Windows it needs to have first class support for COM/UWP.
Just make Rust use the C++ ABI.
C++ code is compiled in separate translation units as C++ by the C++ compiler. The linker statically links calls to C++ code from object files created by the C++ compiler. That's it. Rust doesn't need to know how to deal with templates because templates are all instantiated by the time this happens. This means you have to write a lot of your fancy C++ logic in separate C++ source files (not headers), but that's the whole point, to use already written code.
The only thing Rust has to do is pass pointers to static code and follow the calling and name mangling convention. Zero calling overhead.
A big reason C++ took off was backward compatibility with C. Network effects. Today C++ has the role that C had in the 80s.
No one uses any other compiler but LLVM for Rust anyway, so who cares about compatibility with MSVC and others. This will also force adoption of LLVM, which can be a good incentive for LLVM to support it.