#how to realistically combine C, python and rusty

38 messages · Page 1 of 1 (latest)

fossil igloo
#

Someone here told me that It is possible to combine the three
like python running C-libraries

wheat wagon
#

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.

fossil igloo
#

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

wheat wagon
#

?crate pyo3

tender wolfBOT
#

Bindings to Python interpreter

Version

0.18.1

Downloads

11 631 803

wheat wagon
#

This has most of the rust-python interop stuff

frank perch
# fossil igloo actually you cannot run python modules in C but run C-custom-libraries in Python...

C in Rust
If you have a C library, then it exposes certain functions and types. For the functions, write an extern "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 public extern "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?

wheat wagon
fossil igloo
#

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

wheat wagon
#

What do these chunks do?

fossil igloo
#

they are two parts of the same program

#

so chuckC uses chuckPython and vice versa

wheat wagon
#

What do they do though?

fossil igloo
#

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

narrow wedge
#

Pyo3 ferrisOwO

#

We use pyo3 at work to make python packages from rust code and c libraries ferrisOwO

fossil igloo
#

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

wheat wagon
#

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.

narrow wedge
#

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

fossil igloo
#

i was thinking python would be "external part"
C the most internal, but I wonder where rust would fit it

wheat wagon
#

What sort of stuff is C going to be doing?

fossil igloo
#

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

wheat wagon
#

That doesn't sound simple at all

#

You're writing an LLVM interpreter?

fossil igloo
#

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

wheat wagon
#

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.

fossil igloo
#

@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

narrow wedge
#

I would say just play around with pyo3 and c-rust ffi