Open aclysma opened 5 years ago
Writing a Rust lib that exposes and/or consumes a C API is extremely easy and only exactly as terrible as any other time a C API is involved. You can even hot reload dynamic libs at runtime and other C tricks just as well in Rust.
This biggest problem is that for most people, if you'd use a C API library at all you'll just use a normal, actual C library that works perfectly with all your existing C tooling.
Everyone else assumes you're writing in C as well. For example, if you have performance problems, Intel has a profile tool you can build into your program by including a header file and you call start and stop macros and it spits out profile data. You can't do that so easily in Rust because it's not that big yet.
I saw this today, they have an interesting approach to generating bindings to PhysX.
https://github.com/EmbarkStudios/physx-rs
I think it’s a useful reference/demo of using a non-trivial C++ lib from Rust.
Yes, also https://m.youtube.com/watch?v=t99L3JHhLc0 offers great patterns that can be applied in the domain of interacting to/exposing existing C libs.
Some more resources I've found:
I was just thinking today, if someone wants to research into this further and get real-world experience, one approach could be to try porting some code in an existing game engine. For example, pick some isolated module in Unreal Engine that seems relatively easier to port and give it a try. This could be something gameplay level, but it could also be something non-shipping like the something in the asset pipeline or project build pipeline.
This is very similar to #29 and there are a lot of great comments that are relevant to this there. However, I think #29 is along the lines of "how can an end-user of an engine use rust with existing engines" and I'd like to explore "how can teams maintaining existing game codebases migrate to rust over time"
In industry, established studios are generally using a large existing codebase. Any new language or technology often gets introduced slowly over time, and it's adopted incrementally. (Along the lines of this tweet: https://twitter.com/AndreaPessino/status/1021534287202402305)
I think more demonstrations and proof-of-concepts that Rust can link against and reuse C code would be beneficial to the community. I've personally been using the imgui and gpc crates to call out to a couple libraries in C. (In some ways cargo makes it easier to pull existing C code into a rust project than it would be in most C/C++ environments.)
I think it would also be helpful to consider the use case of linking Rust code into an existing codebase. This could be C++ linking against a rust binary, or something like C# pinvoke into a DLL.
In short, adopting Rust doesn't require throwing out existing code, but I'm not sure this has been well-messaged outside the Rust community.
I have a few questions: