forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall_between_rust_and_python.rs
95 lines (82 loc) · 2.56 KB
/
call_between_rust_and_python.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use rustpython::vm::{
pyclass, pymodule, PyObject, PyPayload, PyResult, TryFromBorrowedObject, VirtualMachine,
};
pub fn main() {
let interp = rustpython::InterpreterConfig::new()
.init_stdlib()
.init_hook(Box::new(|vm| {
vm.add_native_module(
"rust_py_module".to_owned(),
Box::new(rust_py_module::make_module),
);
}))
.interpreter();
interp.enter(|vm| {
vm.insert_sys_path(vm.new_pyobj("examples"))
.expect("add path");
let module = vm.import("call_between_rust_and_python", 0).unwrap();
let init_fn = module.get_attr("python_callback", vm).unwrap();
init_fn.call((), vm).unwrap();
let take_string_fn = module.get_attr("take_string", vm).unwrap();
take_string_fn
.call((String::from("Rust string sent to python"),), vm)
.unwrap();
})
}
#[pymodule]
mod rust_py_module {
use super::*;
use rustpython::vm::{builtins::PyList, convert::ToPyObject, PyObjectRef};
#[pyfunction]
fn rust_function(
num: i32,
s: String,
python_person: PythonPerson,
_vm: &VirtualMachine,
) -> PyResult<RustStruct> {
println!(
"Calling standalone rust function from python passing args:
num: {},
string: {},
python_person.name: {}",
num, s, python_person.name
);
Ok(RustStruct {
numbers: NumVec(vec![1, 2, 3, 4]),
})
}
#[derive(Debug, Clone)]
struct NumVec(Vec<i32>);
impl ToPyObject for NumVec {
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
let list = self.0.into_iter().map(|e| vm.new_pyobj(e)).collect();
PyList::new_ref(list, vm.as_ref()).to_pyobject(vm)
}
}
#[pyattr]
#[pyclass(module = "rust_py_module", name = "RustStruct")]
#[derive(Debug, PyPayload)]
struct RustStruct {
numbers: NumVec,
}
#[pyclass]
impl RustStruct {
#[pygetset]
fn numbers(&self) -> NumVec {
self.numbers.clone()
}
#[pymethod]
fn print_in_rust_from_python(&self) {
println!("Calling a rust method from python");
}
}
struct PythonPerson {
name: String,
}
impl<'a> TryFromBorrowedObject<'a> for PythonPerson {
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &'a PyObject) -> PyResult<Self> {
let name = obj.get_attr("name", vm)?.try_into_value::<String>(vm)?;
Ok(PythonPerson { name })
}
}
}