2015-04-18 18:17:25 +00:00
|
|
|
#![crate_type = "dylib"]
|
|
|
|
|
|
|
|
#[macro_use] extern crate cpython;
|
|
|
|
|
2015-08-02 22:06:15 +00:00
|
|
|
use cpython::{PyObject, PyResult, Python, PyTuple, PyDict};
|
2015-04-18 18:17:25 +00:00
|
|
|
|
2016-03-05 22:20:53 +00:00
|
|
|
py_module_initializer!(hello, inithello, PyInit_hello, |py, m| {
|
2015-10-26 22:52:18 +00:00
|
|
|
try!(m.add(py, "__doc__", "Module documentation string"));
|
|
|
|
try!(m.add(py, "run", py_fn!(run)));
|
|
|
|
try!(m.add(py, "val", py_fn!(val())));
|
2015-04-18 18:17:25 +00:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
2015-10-25 16:55:29 +00:00
|
|
|
fn run(py: Python, args: &PyTuple, kwargs: Option<&PyDict>) -> PyResult<PyObject> {
|
2015-04-18 18:17:25 +00:00
|
|
|
println!("Rust says: Hello Python!");
|
2015-10-25 16:55:29 +00:00
|
|
|
for arg in args.as_slice() {
|
2015-04-18 18:17:25 +00:00
|
|
|
println!("Rust got {}", arg);
|
|
|
|
}
|
2015-08-02 22:06:15 +00:00
|
|
|
if let Some(kwargs) = kwargs {
|
2015-10-25 16:55:29 +00:00
|
|
|
for (key, val) in kwargs.items(py) {
|
2015-08-02 22:06:15 +00:00
|
|
|
println!("{} = {}", key, val);
|
|
|
|
}
|
|
|
|
}
|
2015-04-18 18:17:25 +00:00
|
|
|
Ok(py.None())
|
|
|
|
}
|
|
|
|
|
2015-10-25 16:55:29 +00:00
|
|
|
fn val(_: Python) -> PyResult<i32> {
|
2015-04-18 18:17:25 +00:00
|
|
|
Ok(42)
|
|
|
|
}
|
|
|
|
|