2015-04-18 18:17:25 +00:00
|
|
|
#![crate_type = "dylib"]
|
2015-05-24 18:06:08 +00:00
|
|
|
#![feature(plugin)]
|
|
|
|
#![plugin(interpolate_idents)]
|
2015-04-18 18:17:25 +00:00
|
|
|
|
|
|
|
#[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
|
|
|
|
2015-10-25 16:55:29 +00:00
|
|
|
py_module_initializer!(hello, |py, m| {
|
|
|
|
try!(m.add("__doc__", "Module documentation string", py));
|
|
|
|
try!(m.add("run", py_fn!(run), py));
|
|
|
|
try!(m.add("val", py_fn!(val()), py));
|
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)
|
|
|
|
}
|
|
|
|
|