by shakow · 5y ago · 0 comments
I of course, think that C FFI is as good as I personally need it to be. It's as good or better than most other languages that are not C.
I think at least for non-mutable stuff, C++ string_view (approximately rust &str), span (approximately rust slices), and the like should make at least some operations as simple as pointer casts, but having tooling to automate the boilerplate would be nice, and they're also new/not that widely supported (std::span is from C++ 20). And mutable stuff, if you want to be able to write idiomatic/ergonomic code on both sides, seems like a much bigger lift.
Compare that to C, where the story is often as good as "import the header and you're done".
I'm not sure if there's a good path to a long-term solution, but I've been pushing to get nice C APIs in all the libraries I'm invested in, specifically because of this issue.
So while .NET type type doesn't cover all of C++, it does cover enough not to have clunky C interfaces, and nowadays many Windows APIs only have such COM/UWP interfaces as public API.
Microsoft is in the process of adding Rust to their COM/WinRT infrastructure.
One of the beauties of COM is that it is interface based component model, so it goes quite well with languages that have interfaces/traits as feature.
[0] I really don't care if in C or a different language, but I'm not aware of relevant programming subcultures where that simplicity of interfacing is such an important pillar to their culture and their success.
SWIG is what people really want.
IMHO going through C APIs is fine, because C++ interoperability is much harder and a moving target, just because the language surface is so much bigger than plain C.
See Zig as an example for excellent C interoperability.
As soon as a C/C++ compiler toolchain and build tools need to be involved in a Rust project you're basically loosing all advantages of Cargo and are back to the relative brittleness of cross-platform C/C++ builds.
(I was only thinking of calling)