Can I generate C bindings with pyo3? #3081
-
Hey there! I want to make some drivers for rust using i2c/spi, but I want them to have C and Python bindings. To test this idea, I started with a simple example: creating a file and reading it. Here's the code: https://pastebin.com/8wH5Vk5f I managed to bind it to Python using pyo3, but I still need a solution for the C bindings. Do you know if I can find it on pyo3? I came across these articles where the author created a binding for C and then for python: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I think the C bindings would be developed independently of the Python bindings and PyO3. Generally speaking, you would create a Rust-native core library and then put PyO3-based Python bindings on top of it. Similarly, C bindings would be put on top of the same core library. Writing C bindings for Rust is more or less an independent endeavour, but basically you would need to provide |
Beta Was this translation helpful? Give feedback.
With
ctypes
you're restricted to the constructs which exist in the C language, and you likely need to add a wrapper around them as Python code if you want to create an experience in Python which feels like native Python experience.With PyO3 you are building "extension modules" which implement Python language functionality via exactly the same mechanism that fundamental modules like
datetime
are implemented. So you can create a much richer interface for Python users directly from Rust.