Re:Calling Rust from Python
by guitarbill on Sunday October 08, 2023 @05:31PM
(#37814602)
Honestly though, I would likely still choose to implement the logic in Rust as a crate/library in a workspace, the (C)Python extensions as a PyO3 dylib/separate crate, and a C FFI dylib as yet another crate.
The benefit is that you only need to ship the CPython extension, you can build it with cargo, and everything's in one place. I've maintained bindings for Python and C# to native libraries, and it's usually a pain for various reasons.
The C API might be able to be used anywhere, but that doesn't mean it's ergonomic, easy to use, or safe.
Re:Calling Rust from Python
by forrestthewoods on Sunday October 08, 2023 @05:54PM
(#37814735)
> The C API might be able to be used anywhere, but that doesn't mean it's ergonomic, easy to use, or safe.
Definitely true. That said I think there is an under appreciated beautiy to a clean C API.
It’s easy to “upgrade” a C API with per-language/environment bindings. It can be near impossible to “downgrade” a high-level API to C.
Re:Calling Rust from Python
by yazaddaruvala on Monday October 09, 2023 @03:55AM
(#37818065)
And Rust just uses the C ABI, so wouldn’t your comments include “in Rust”?
Well it depends. C is the lingua franca of Computer Science. These days I write a lot of code that needs to be accessible to a variety of languages and environments. Python, C#, C++, Matlab, Unity, Unreal, etc.
Write a C API and your code can be used anywhere. Now if you know you only ever need to support Python then sure look into PyO3. But if you have mixed use you can’t be the flexibility of a C API.