rust-cpython icon indicating copy to clipboard operation
rust-cpython copied to clipboard

What is rust-cpython used for and how does it compare to cffi / ctypes / pybind11 / ...?

Open MartinThoma opened this issue 4 years ago • 3 comments

I'm currently reading about how to speed up Python by using C and I found a couple of ways to "integrate" C into Python (bind C to Python? Execute C from Python? ... I'm not sure about the terminology):

  • ctypes:
  • cffi
  • c extensions
  • cython
  • pybind11

Could you explain how rust-cpython relates to those? How does it compare to cffi / ctypes / pybind11 / ...?

(I haven't done anything with Rust so far and only super small projects with C / C++)

Thank you :-)

MartinThoma avatar Feb 16 '20 16:02 MartinThoma

Fundamentally, there are two ways to interact with C from Python:

  1. Write a C library which uses the libpython API. This will produce a compiled library which Python's import can import in the same way as a .py file. (Having C speak Python)
  2. Write Python code which uses either ctypes or cffi to call functions in an ordinary C library. (Having Python speak C)

c extensions (C), pybind11 (C++), cython (a specialized dialect of Python), and rust-cpython (Rust) are all examples of option 1.

ctypes and cffi are examples of option 2.

Of those, the two that give you the most compile-time safety would be cython and rust-cpython.

Cython is the simplest to get into, since you're essentially writing Python and then annotating it with types and other helpers, but it has limited potential for improved performance because of that. rust-cpython allows you to write Rust and then wrap it in API adapters to get maximum performance, but it means that you need to know how to write both Rust and Python.

That said, rust-cpython does have the advantage that, since you can work in safe Rust, you should run into compiler errors rather than runtime crashes or memory corruption if you misunderstand how to use the APIs.

ssokolow avatar Feb 16 '20 16:02 ssokolow

Cool, thank you! I'm still not quite sure how to formulate this with my own words (and put it in a concise way in the README), but I think I got it :-)

I'm not sure if I should now close this issue or leave it open. Do you think the beginning of the README is clear as it is?

MartinThoma avatar Feb 16 '20 20:02 MartinThoma

I do think that "Rust bindings for the python interpreter." would be better as "Rust bindings for the python interpreter, suitable for writing compiled Python extensions or embedding the Python runtime in Rust programs".

As-is, you have to derive that latter part by reading the examples further down, which is less than ideal.

ssokolow avatar Feb 17 '20 11:02 ssokolow