#how to realistically combine C, python and rusty
38 messages · Page 1 of 1 (latest)
You can compile rust into a python module so you can call rust from python. You can also call python code from rust. Same with C. Which method is best depends on what's calling what, and what situation you're in.
actually you cannot run python modules in C
but run C-custom-libraries in Python
how would you run C or Python in Rust
and how would you run Rust in Python
@wheat wagon
I dont mean you know "start a script", but treat parts as one program
?crate pyo3
This has most of the rust-python interop stuff
C in Rust
If you have a C library, then it exposes certain functions and types. For the functions, write anextern "C" {}block (or the appropriate ABI, if it's not "C") and mimic their C signatures in Rust, then, call them as normal. For the types, create an equivalent Rust type with#[repr(C)].
Rust in C
Declare publicextern "C"functions. C can call them. For types, same deal: the C side will have to create types mimicking yours.
Rust in Python
Use PyO3.
If the concepts of "ABI" or "layout" confuse you, you probably want to go learn them first, they're absolutely required (unless you're using PyO3). I believe the rustonomicon explains them?
There's also crates that generate bindings automatically like https://github.com/rust-lang/rust-bindgen for C. If you're using a popular library it probably already has a bindings crate.
but I mean like I have "chunk" in C and other "chunk" in python
I dont want to rewrite these "chunks" to rust but simply use them when writing additional parts in rust
What do these chunks do?
What do they do though?
well to be more prices the python chuck is reponsible for implelement different AI algorithms or some other big algorithm mean while C is responsible for low level programming
the point is to divide parts of the same program into languages which are most efficient in "physical programming"
low level program = C
AI algorithms = python
also cpu performance/speed is not the issue
the issue is "most friendly way of programming for the programmer"
Imagine this senarious
personally, python has better AI modules than C; thus, I want to use python in these cases
personally, C has better low level programming options; thus, i want to use C in these cases
personally, rusty (I am new) could have this or that library better; thus, i would like to use rust without rewriting everything what has been done
Well FFI isn't friendly to anyone, so make the boundaries between languages as small as possible. Sounds like you're doing the usual thing with python as a high-level language, so you'd make your rust into a python module with pyo3. And then probably call C from rust, and just reexport anything you need from C in rust.
Yes, having python be the main controller of everything makes your life much easier
Especially with cpython
It doesn't like to behave when embedded into a bigger application
i was thinking python would be "external part"
C the most internal, but I wonder where rust would fit it
What sort of stuff is C going to be doing?
the easiest and simplest, get llvm IR of a program and pass info to python or rust which would analyze this shit according to multiplicity of difffernt algorithms and later just get info how to modify the llvm IR
custom-llvm compiler, the point is that C has shit-friendly libraries meanwhile python has alot friendly modules for different algorithms which really speed up the process of getting results
so I could basically make llvm AI-pass to analyze data faster in python than in C
just to see the results
I think if I were you, I'd just use python and c. Hard to use rust FFI when you barely know rust. It also doesn't sound like there's much reason to be using rust.
@wheat wagon but i was thinkin in the future to implement something small to "train my rust skills"
something like "yea it was meant to be in python, but now ill do it in rust to study rust", but also knowing that this part will use python and c
I would say just play around with pyo3 and c-rust ffi
