Initial commit.
This commit is contained in:
commit
a8ec4664e4
|
@ -0,0 +1,3 @@
|
|||
/target
|
||||
/Cargo.lock
|
||||
build/
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "rust_add_lib"
|
||||
version = "0.1.0"
|
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "rust_add_lib"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
|
@ -0,0 +1,19 @@
|
|||
# Calling a rust function from Python
|
||||
|
||||
- `cargo build` builds a `./target/debug/librust_add_lib.so` with a `rust_add` function in it.
|
||||
- `LDFLAGS='-L target/debug/' python setup.py build` builds a Python module using `rust_add_lib`.
|
||||
|
||||
Then the module can be imported and used from Python:
|
||||
|
||||
```bash
|
||||
$ LD_LIBRARY_PATH=target/debug PYTHONPATH='./build/lib.linux-x86_64-cpython-311/' python
|
||||
Python 3.11.6 (main, Oct 8 2023, 05:06:43) [GCC 13.2.0] on linux
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> import add
|
||||
>>> add.add(.1, .2)
|
||||
[src/lib.rs:3] left = 0.1
|
||||
[src/lib.rs:3] right = 0.2
|
||||
[src/lib.rs:4] left + right = 0.30000000000000004
|
||||
0.30000000000000004
|
||||
>>>
|
||||
```
|
|
@ -0,0 +1,39 @@
|
|||
#define PY_SSIZE_T_CLEAN
|
||||
#include <Python.h>
|
||||
|
||||
double rust_add(double, double); // from rust, from librust_lib.so
|
||||
|
||||
|
||||
static PyObject *python_add(PyObject *self, PyObject *args)
|
||||
{
|
||||
double float_left;
|
||||
double float_right;
|
||||
double result;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "dd", &float_left, &float_right))
|
||||
return NULL;
|
||||
|
||||
result = rust_add(float_left, float_right);
|
||||
|
||||
return PyFloat_FromDouble(result);
|
||||
}
|
||||
|
||||
static PyMethodDef AddMethods[] = {
|
||||
{"add", python_add, METH_VARARGS,
|
||||
"Just add from rust."},
|
||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
static struct PyModuleDef addmodule = {
|
||||
PyModuleDef_HEAD_INIT,
|
||||
"add", /* name of module */
|
||||
NULL , /* module documentation, may be NULL */
|
||||
-1, /* size of per-interpreter state of the module,
|
||||
or -1 if the module keeps state in global variables. */
|
||||
AddMethods
|
||||
};
|
||||
|
||||
PyMODINIT_FUNC PyInit_add(void)
|
||||
{
|
||||
return PyModule_Create(&addmodule);
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
from distutils.core import setup
|
||||
from distutils.extension import Extension
|
||||
|
||||
setup(name='add',
|
||||
version='1.0',
|
||||
ext_modules=[Extension('add', ['python_add.c'], libraries=['rust_add_lib'])],
|
||||
)
|
|
@ -0,0 +1,5 @@
|
|||
#[no_mangle]
|
||||
pub extern "C" fn rust_add(left: f64, right: f64) -> f64 {
|
||||
dbg!(left, right);
|
||||
dbg!(left + right)
|
||||
}
|
Loading…
Reference in New Issue